0

No matter how many sites I go to to try and get this to work, it still doesn't. I am unable to have the data that has been entered end up in the database. The form submits, but doesn't come back with an error. When looking in phpMyAdmin, there are no records. I've tweaked it a million times with no luck. Can a second set of eyes show me what I'm doing wrong?

Thanks!!
Tim

<body>
<form action="insert.php" method="post"><br>
Date: <input type="text" name="date" id="date"><br>
Time: <input type="text" name="time" id="time"><br>
City: <input type="text" name="city" id="city"><br>
Fire Dept: <input type="text" name="fire" id="fire"><br>
Address: input type="text" name="addy" id="addy"><br>
Call Type/Level <input type="text" name="level" id="level"><br>
Description: <input type="text" name="desc" id="desc"><br>
Units: <input type="text" name="units" id="units"><br>
Submitted by: <input type="text" name"who" id="who"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

<?php
if (isset($_POST['submit'])){

$db=mysql_connect("", "", "");
$mydb=mysql_select_db("rm911_incidents");

$date=$_Post['date'];
$time=$_Post['time'];
$city=$_Post['city'];
$fire=$_Post['fire'];
$addy=$_Post['addy'];
$level=$_Post['level'];
$desc=$_Post['desc'];
$units=$_Post['units'];
$who=$_Post['who'];

$sql = "INSERT INTO incidents(date,time,city,fire,addy,level,desc,units,who)
VALUES
('$date','$time','$city','$fire','$addy','$level','$desc','$units','$who')";
$result = mysql_query($sql);
if($result)
{
echo "<br>Input data is successful";
}
else
{
echo ("<br>Input data has failed");
}
}
?>

Okay, so I have fixed the error that Leo mentioned (Thank you!), however that was not the problem either. The errors that I am getting using the error reporting that you provided are: Undefined index: xxx... (xxx being every field name in the db). I have an 'id' field in the db, auto_increment-ing - did I forget to set that as the index?

6
  • 1
    Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. Commented Apr 13, 2013 at 19:36
  • what does the script output when you run it? does it show the failed message? Commented Apr 13, 2013 at 19:36
  • what does it say on your server logs. Need to see an error. At a rough skim through it all I didn't notice anything but old deteriorated php coding. Instead of Mysql_ use pdo. Commented Apr 13, 2013 at 19:38
  • First step is checking the web server log to find errors there. Did you actually left the connection parameters as empty strings or deleted them for the post? We really need more information to help you here. Semi-related tip: Use PDO with prepared statements, not mysql_query. Commented Apr 13, 2013 at 19:39
  • Interesting! It'd be nice if YouTube posters updated their tutorials with this info... Also, no failed message, no errors come back - the form resets itself as if it was ready for another entry. And yes, I just removed the connection info for the post, it is there in real life. Commented Apr 13, 2013 at 20:06

6 Answers 6

3

The POST data is stored in the $_POST array, not the $_Post array. You should get a Notice on the undefined variable in your server log or in the browser (if PHP messages are sent to the browser).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! That is definitely an error, but not the one that seems to be causing the fail
1

The problem is in your post array name $_Post

$date=$_Post['date'];
$time=$_Post['time'];
$city=$_Post['city'];
$fire=$_Post['fire'];
$addy=$_Post['addy'];
$level=$_Post['level'];
$desc=$_Post['desc'];
$units=$_Post['units'];
$who=$_Post['who'];

Instead of $_Post you should use $_POST. Also, you can do

var_dump($_POST);

at the top of php file, so you will be able to see what the form sends to your script.

Regards

Comments

0

changed a few things, this should work.. if it doesnt is most likely an issue with your db

<html>
<body>
<form action="insert.php" method="POST"><br> <!-- method POST-->
Date: <input type="text" name="date" id="date"><br>
Time: <input type="text" name="time" id="time"><br>
City: <input type="text" name="city" id="city"><br>
Fire Dept: <input type="text" name="fire" id="fire"><br>
Address: <input type="text" name="addy" id="addy"><br>
Call Type/Level <input type="text" name="level" id="level"><br>
Description: <input type="text" name="desc" id="desc"><br>
Units: <input type="text" name="units" id="units"><br>
Submitted by: <input type="text" name="who" id="who"><br> <!-- name"who" changed -->
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

<?php
if (isset($_POST['submit'])) {

$db=mysql_connect("", "", "") or die ("Cant connect");
$mydb=mysql_select_db("rm911_incidents") or die ("Cant find db");

$date = $_POST['date']; //_POST not _Post
$time = $_POST['time'];
$city = $_POST['city'];
$fire = $_POST['fire'];
$addy = $_POST['addy'];
$level = $_POST['level'];
$desc = $_POST['desc'];
$units = $_POST['units'];
$who = $_POST['who'];

$sql = "INSERT INTO incidents ( date , time, city , fire , addy , level , desc , units , who ) 
VALUES ( '$date' , '$time' , '$city' , '$fire' , '$addy' , '$level' , '$desc' , '$units' , '$who' )";
$result = mysql_query($sql) or die ("Cant insert");
if($result)
{
echo "<br>Input data is successful";
}
else
{
echo "<br>Input data has failed";
}
}
?>

2 Comments

Thank you very much for the fixes. However, it still will not insert data into the db. Where do I go from here?
check your db structure is set up correctly, post screen shot if you can of the db structure
0

Also you're missing an opening square bracket:

Address: input type="text" name="addy" id="addy"><br>

Should be:

Address: <input type="text" name="addy" id="addy"><br>

1 Comment

Thanks - It's there in the real code, I must've deleted it by accident when learning the coding for this!
0

Try doing this in your php file at the very top after the <?php:

//error reporting set to all
error_reporting(E_ALL);
ini_set('display_errors', 'On');

Also place your php code before your html.

You should see errors being outputted and be able to debug it.

Post the errors here if you need help.

2 Comments

Okay, so I have fixed the error that Leo mentioned (Thank you!), however that was not the problem either. The errors that I am getting using the error reporting that you provided are: Undefined index: xxx... (xxx being every field name in the db). I have an 'id' field in the db, auto_increment-ing - did I forget to set that as the index?
can you post that in your question?
0

can you check if Submitted by: <input type="text" name"who" id="who"><br> is causing error.. it should be Submitted by: <input type="text" name="who" id="who"><br>

If this doesnt help.. Try running a static insert query in your php file and check if its working...

Regards, Leo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.