hello am new here. i have a difficult problem i have been trying to solve for the past few months. i just installed lamp on ubuntu 10.08 and started building my first database application. i intend to build a form. the form is called sign.php and the script is defined below.

<h2>Sign my Guest Book!!!</h2>
<form method=post action="create_entry.php">
<b>Name:</b>
<input type=text size=40 name=name>
<br>
<b>Location:</b>
<input type=text size=40 name=location>
<br>
<b>Email:</b>
<input type=text size=40 name=email>
<br>
<b>Home Page URL:</b>
<input type=text size=40 name=url>
<br>
<b>Comments:</b>
<textarea name=comments cols=40 rows=4 wrap=virtual></textarea>
<br>
<input type=submit name=submit value="Sign!">
<input type=reset name=reset value="Start Over">
</form>

now the action of the sign.php form is the create_entry.php script whose script is found below:

<?php
include("dbconnect.php");
if ($submit == "Sign!")
{
$query = "insert into guestbook
(name,location,email,url,comments) values
('$name', '$location', '$email', '$url', '$comments')"
;
mysql_query($query) or
die (mysql_error());
?>
<h2>Thanks!!</h2>
<h2><a href="view.php">View My Guest Book!!!</a></h2>
<?php
}
else
{
include("sign.php");
}
?>

found within the create_entry.php script is the dbconnect.php whose script is found below;

mysql_connect("localhost", "root","mobolaji") or
die ("Could not connect to database");
mysql_select_db("guestbook") or
die ("Could not select database");


now i have created the database guestbook and also a table called guestbook within this database in mysql but everytime i run the sign.php form and i input entries into the form, i get this;

mysql_connect("localhost", "root","mobolaji") or die ("Could not connect to database"); mysql_select_db("guestbook") or die ("Could not select database");

i have experienced this same problem in windows and i was told to go into the config.inc.php file and edit the username and password. that worked in windows but in ubuntu it didnt. i need help seriously. thanks

Recommended Answers

All 10 Replies

Please remember to wrap your code in [ code ] tags. What exactly do you mean by "everytime i run the sign.php form and i input entries into the form, i get this:" . Does the db connection file have <?php ?> tags??

<?php mysql_connect("localhost", "root","mobolaji") or die ("Could not connect to database"); mysql_select_db("guestbook") or die ("Could not select database"); ?>

*Edit:- If the file was wrapped in php tags you would get the error: "Could not select database" (as that's what you've told it to output with the die function). The error should not print the whole contents of the file.

thanks for your quick response. i have opened up the dbconnect.php file and made the changes. i have now written it as:

<?php mysql_connect("localhost", "root","mobolaji") or die ("Could not connect to database"); mysql_select_db("guestbook") or die ("Could not select database"); ?>

what i mean is that the form sign.php i created is not sending input values to the mssql database guestbook.

although i have made the change, anytime i insert values into the form sign.php and i click sign, the sign.php appears again on the browser and the values i inserted into the sign.php form dont appear in the mssql guesbook table i created. help!!!

<?php
include("dbconnect.php");
if ($submit == "Sign!")
{
$query = "insert into guestbook
(name,location,email,url,comments) values
('$name', '$location', '$email', '$url', '$comments')"
;
mysql_query($query) or
die (mysql_error());
?>
<h2>Thanks!!</h2>
<h2><a href="view.php">View My Guest Book!!!</a></h2>
<?php
}
else
{
include("sign.php");
}
?>

Okay, the main problem is within this file. You're telling it to insert variables that don't contain anything! You must get the input values using $_POST or $_GET.

E.g.

//check the user clicked the submit button
if(isset($_POST['submit']))
{
    // get the user-supplied values from the input fields. Echo out these values to make sure they exist!
    $name = $_POST['name'];
    $location = $_POST['location'];
    $email = $_POST['email'];
    $url = $_POST['url'];
    $comments = $_POST['comments'];

    //then once you know the values exist, issue your mysql insert query.
    $query = mysql_query("INSERT INTO guestbook (name,location,email,url,comments) VALUES ('$name','$location','$email','$url','$comments')") or die ("query failed". mysql_error());
}

Note, I haven't tested this. So read up on $_GET and $_POST, make sure the variables are holding the input data, then do the query. Hope that helps! Nonshatter

thanks i just made the changes noshatter. this is what my create_entry.php file looks like


<?php
include("dbconnect.php");
//check the user clicked the submit button
if(isset($_POST))
{
// get the user-supplied values from the input fields. Echo out these values to make sure they exist!
$name = $_POST;
$location = $_POST;
$email = $_POST;
$url = $_POST;
$comments = $_POST;

//then once you know the values exist, issue your mysql insert query.
$query = mysql_query("INSERT INTO guestbook (name,location,email,url,comments) VALUES ('$name','$location','$email','$url','$comments')") or die ("query failed". mysql_error());
}
?>
<h2>Thanks!!</h2>
<h2><a href="view.php">View My Guest Book!!!</a></h2>
<?php
}
else
{
include("sign.php");
}
?>
after submitting instead of seeing a browser page that says thanks view my guest book instead i get a blank browser page. also querying the guestbook table in the mysql guestbook database alos produces no new entries. help!!!

ok.... Put this in your create_entry.php file. Add some text to the input fields on the sign.php page and click submit. If you see what you typed after the create_entry page loads, then you know the values are there. Then we can concentrate on getting it into your database

<?php
if (isset($_POST['submit']))
{
	$name = $_POST['name'];
	$location = $_POST['location'];
    $email = $_POST['email'];
    $url = $_POST['url'];
    $comments = $_POST['comments'];
	echo $location ."<br>";
	echo $email ."<br>";
	echo $url ."<br>";
	echo $comments ."<br>";
}
?>
<h2>Thanks!!</h2>
<h2><a href="view.php">View My Guest Book!!!</a></h2>

I just tested this.

yes i see it that worked. thanks. please what next?

Good. Now try adding this data to your database:

<?php
if (isset($_POST['submit']))
{
    $name = $_POST['name'];
    $location = $_POST['location'];
    $email = $_POST['email'];
    $url = $_POST['url'];
    $comments = $_POST['comments'];

    $query = mysql_query("INSERT INTO guestbook (name,location,email,url,comments) VALUES ('$name', '$location', '$email', '$url', '$comments');") or die("Could not execute". mysql_error());
}
?>

thanks for your response. yes this worked and it submitted all the entries of the form into the database. but after clicking the sign php form, its meant to display the create_entry.php form which will show thanks view my guestbook or show the sign.php form again.

in order words, how do i insert the below script into the newly modified script.

<h2>Thanks!!</h2>
<h2><a href="view.php">View My Guest Book!!!</a></h2>
<?php
}
else
{
include("sign.php");
}
?>

<?php
if (isset($_POST['submit']))
{
    $name = $_POST['name'];
    $location = $_POST['location'];
    $email = $_POST['email'];
    $url = $_POST['url'];
    $comments = $_POST['comments'];
 
    $query = mysql_query("INSERT INTO guestbook (name,location,email,url,comments) VALUES ('$name', '$location', '$email', '$url', '$comments');") or die("Could not execute". mysql_error());

?>
<html>
<head></head>
<body>
     <h2>Thanks!!</h2>
     <h2><a href="view.php">View My Guest Book!!!</a></h2>
<?php
}
else
{
    include("sign.php");
}
?>
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.