-1

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?

Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254
  • 1
    Well ... does the "insert.php" exist in the same folder where this file is? And stop downvoting questions of beginners, its just ridiculous when ppl ask for advice, clearly put some effort into the project themselves and then there are ppl which just downvote everything because its "too easy". Maybe he just overlooked something, you never know. – Realitätsverlust Dec 11 '13 at 13:19
  • well .. this error doesn't have much to do with the code .. try to make sure that insert.php file is present in the same directory as of this file .. – Qarib Haider Dec 11 '13 at 13:22
  • @Y U NO WORK did I do something wrong (your downvoting comment)?? I have put a lot of work into this, being very new to this I am certain I am overlooking this because I am not aware of them. I apologize if my question is silly but to me...its not. – user3085177 Dec 11 '13 at 14:05
  • The files are listed in the same folder together – user3085177 Dec 11 '13 at 14:05
  • the URL message means you are not navigating to the right page, or that you do not have your web server configured correctly. What web server are you using, and what URL are you trying to connect to? – Jeremy Holovacs Dec 11 '13 at 14:19
  • I am using xampp and connecting to the local server – user3085177 Dec 11 '13 at 14:22
  • Yes but what is the URL you are connecting to? OK and you just totally changed the nature of the question – Jeremy Holovacs Dec 11 '13 at 14:30

2 Answers2

0

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? )

Community
  • 1
  • 1
0

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";
}
Joke_Sense10
  • 5,341
  • 2
  • 18
  • 22