I am creating a database using php and viewing the tables in html. Within my coding I scripted an "add/insert" function to insert within the table with a submit button. When the submit button is selected it takes me to the error message listed above. I am not sure what I have changed, I ran this code last night (on a different computer) and all worked fine!?! Anyone have any ideas??
Here is the code for my insert scripts for the TABLE, INSERT info, add.html and insert.php
<?php
$con=mysqli_connect("localhost","root","","Franchise_Call_Log");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Create table
$sql="CREATE TABLE caller_info(caller_id int(11) unsigned auto_increment primary key not null,
Firstname varchar(35) not null, Lastname varchar(35) not null,
Franchise varchar(25) not null)";
// Execute query
if (mysqli_query($con,$sql))
{
echo "Table caller_info created successfully";
}
else
{
echo "Error creating table: " . mysqli_error($con);
}
?>
<?php
$con=mysqli_connect("localhost","root","","Franchise_Call_Log");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"INSERT INTO caller_info (Firstname, Lastname, franchise)
VALUES ('Peter', 'Griffin',Minneapolis)");
mysqli_query($con,"INSERT INTO caller_info (Firstname, Lastname, Franchise)
VALUES ('Maggie', 'DeJesus',Virginia)");
mysqli_close($con);
?>
<html>
<body>
<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname">
Lastname: <input type="text" name="lastname">
Franchise: <input type="text" name="franchise">
<input type="submit">
</form>
</body>
</html>
<?php
$con=mysqli_connect("localhost","root","","Franchise_Call_Log");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO caller_info (Firstname, Lastname, Franchise)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[franchise]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
*UPDATE I was able to fix the issue above by changing the names of my files but now running everything again I am running into this message:
Notice: Undefined index: Firstname in C:\xampp\htdocs\Final Tests\insert.php on line 11
Notice: Undefined index: Lastname in C:\xampp\htdocs\Final Tests\insert.php on line 11
Notice: Undefined index: Franchise in C:\xampp\htdocs\Final Tests\insert.php on line 11 1 record added
Although its just a notice, when selecting the tables the new record is not there. There is a disconnect somewhere that I am not catching. Any thoughts?
You use
$sql="INSERT INTO caller_info (Firstname, Lastname, Franchise)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[franchise]')";
But where you refer to the $_POST values ( ex: $_POST[xyz] ), you are not quoting the xyz name. It should be:
$sql="INSERT INTO caller_info (Firstname, Lastname, Franchise)
VALUES
('{$_POST['firstname']}','{$_POST['lastname']}','{$_POST['franchise']}')";
I've added the {}-brackets around the $_POST['xyz'] so php can correctly identify the variable. Ofcourse you are now subject to SQL-injection ( read more at How can I prevent SQL injection in PHP? )
If your submitting the form to the same page then leave the action field blank:
<form action="" method="post">
Specify name for submit button:
<input type="submit" name="submit">
and insert the value into database if the submit is set(submit is pressed):
if(isset($_POST['submit'])){
$sql="INSERT INTO caller_info (Firstname, Lastname, Franchise)
VALUES('".$_POST['firstname']."','".$_POST['lastname']."',
'".$_POST['franchise']."')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
}