Forum Moderators: coopster
$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.
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
$result=mysql_query("INSERT INTO birthdays ('name', 'birthday') VALUES ('$name','$birthday')")or die("Insert Error: ".mysql_error());