kindly, what is wrong with this my code.
The error message prompt on line 15 is this line:
$sql="INSERT INTO Contacts (Surnname, Firstname) VALUES('$_POST[sname]','$_POST[fname]')";
My db connection and table selection are ok. however when I click on the this file(combined2.php), I can see the below error messages above the form. Do I need to predefine sname and fname as something at the start of the code?
the error message is:
Notice: Undefined index: sname in C:\wamp\www\DBdisplay\combined2.php on line 15
Notice: Undefined index: fname in C:\wamp\www\DBdisplay\combined2.php on line 15
Code: Select all
<html>
<body>
<?php
//mysql connection
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//select db
mysql_select_db("contactdb1", $con);
//add record
$sql="INSERT INTO Contacts (Surnname, Firstname) VALUES('$_POST[sname]','$_POST[fname]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
?>
<form name="form1" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
Surnname: <input type="text" name="sname" />
Firstname: <input type="text" name="fname" />
<input type="submit" name="btnSendForm" value="Send" />
</form>
</body>
</html>