Forum Moderators: coopster

Message Too Old, No Replies

debugging problem. please help

         

Flolondon

9:07 am on Apr 18, 2005 (gmt 0)

10+ Year Member



<?

$db="mydatabase";
$link = mysql_connect("localhost","root", "");
//$link = mysql_connect("localhost",$_POST['username'],$_POST['password']);
if (! $link)
die("Couldn't connect to MySQL");

mysql_select_db($db , $link) or die("Select Error: ".mysql_error());

if (isset($_POST['name']))
{
$name= $_POST['name'];
}
if (isset($_POST['birthday']))
{
$birthday= $_POST['birthday'];
}

$result=mysql_query("INSERT INTO birthdays ('name', 'birthday') VALUES ('$name','$birthday')")or die("Insert Error: ".mysql_error());
mysql_close($link);
print "Record added\n";
?>
<form method="POST" action="birthdays_insert_form.php">
<input type="submit" value="Insert Another Record">
</form>
</body>
</html>

The debugging system is saying undefined variable for name and birthday.... in the INSERT row. please help.

jd01

9:33 am on Apr 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For some reason your variables aren't being passed in the post.

You can do a couple of things:
1. Change to this:
if (isset($_POST['name']))
{
$name= $_POST['name'];
}
else {
$name="whater ever you want to default to";
}

if (isset($_POST['birthday']))
{
$birthday= $_POST['birthday'];
}
else {
$birthday="whater ever you want to default to";
}

or you could save some lines:
$name= $_POST['name'];
$birthday= $_POST['birthday'];

if(!isset($name)) $name="whater ever you want to default to";
if(!isset($birthday)) $birthday="whater ever you want to default to";

or you could use this:
$name= $_POST['name'];
$birthday= $_POST['birthday'];

if(!isset($name && $birthday)) {
echo "You have not entered a name and/or birthday, please...";
echo "<a href=\"/blah-blah\">Link back to the form</a>";
}

if(!isset()) = true if they are not set.

Any of these or a combination that best fits your needs should work... Don't know why they didn't post without that part of the form.

Justin

henry0

11:46 am on Apr 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you have register_global off?
Do you have error_report ON

To avoid those messages (it was discussed here a few days ago)

use:
instead of
if ($whatever)
{
do that
}

better use:
if(issset ($whatever) &&!empty($whatever))
{
Do that
}

Flolondon

6:29 pm on Apr 18, 2005 (gmt 0)

10+ Year Member



ok thanks guys lets see what happens

realitybytes

7:00 pm on Apr 18, 2005 (gmt 0)

10+ Year Member



try
$sql = "INSERT INTO birthdays ('name', 'birthday') VALUES ('$name','$birthday')";
$result = mysql_query($sql);
if (!$result) {
echo('Insert error '. mysql_error());
}
instead of

$result=mysql_query("INSERT INTO birthdays ('name', 'birthday') VALUES ('$name','$birthday')")or die("Insert Error: ".mysql_error());