Page 1 of 1

Trouble with Inserting Data to Mysql via PHP Form

Posted: Sat Aug 28, 2010 3:04 pm
by Adsummy
Hello, I am new to php and mysql. I am having difficulties with using a php form on my website to submit data to a mysql database.

This is the code I have for the form on the webpage:

Code: Select all

<form action="insert.php" method="post">
   Firstname: <input type="text" name="firstname" />
   Lastname: <input type="text" name="lastname" />
   Age: <input type="text" name="age" />
   <input type="submit" value="Submit" />
</form>
This is all the code from insert.php that the form calls for:

Code: Select all

<?php

$con = mysql_connect("localhost","christia_form","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("christia_sermons", $con);

$sql="INSERT INTO Persons (Firstname, Lastname, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con);
?>
When I enter and submit the form data on my webpage, the form refreshes itself, but nothing else happens. However, if I use the 'get' method instead of the 'post' method, and insert the php code directly in the form tag on the webpage (ie. <form action="PHP CODE" method="get">), then it will submit the data to the database, but it creates other problems, like every time the page is refreshed, it will automatically submit an entry to the database. To me, it seems like the form is not calling up insert.php, but I have tried everything that I know of to no avail. Please help!

Re: Trouble with Inserting Data to Mysql via PHP Form

Posted: Sat Aug 28, 2010 7:51 pm
by Jonah Bron
First, I would like to thank you for the following things:
  • A descriptive subject line
  • Using the syntax tags around your code
  • Making an effort yourself
  • Laying out your question clearly
This is truly a utopian forum post.

Now as to your problem.

What is the file name of the page with the form?

Also, lets fix some major security concerns with your code:

Code: Select all

<?php

$con = mysql_connect("localhost","christia_form","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("christia_sermons", $con);

// check if post variables are present
if (!isset($_POST['firstname'], $_POST['lastname'], $_POST['age'])) {
    die('No data present');
}

// prevent SQL injection
$firstname = mysql_real_escape_string($_POST['firstname']);
$lastname = mysql_real_escape_string($_POST['lastname']);
$age = mysql_real_escape_string($_POST['age']);

//neatly put the filtered variables into the SQL
$sql=sprintf('INSERT INTO Persons
(Firstname, Lastname, Age)
VALUES ("%s", "%s", "%s")', $firstname, $lastname, $age);

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con);
?>

Re: Trouble with Inserting Data to Mysql via PHP Form

Posted: Sat Aug 28, 2010 9:34 pm
by Adsummy
Thank you so much for addressing my problem, and for your help with addressing the security issues. I implemented the changes you suggested. However, I still am having the same problem. It still will not insert to the database. I also double checked to be sure the Mysql user has appropriate permissions.

If it helps, the webpage where the form is located is http://www.christianlightfellowship.com/audio.php. It is at present a test form submitting to a test database table. The php code that the form calls for is http://www.christianlightfellowship.com/insert.php. If there is any other information that would be helpful, please let me know. I really am at my wit's end, but I am determined to conquer this issue. Any help at all is greatly appreciated.

Re: Trouble with Inserting Data to Mysql via PHP Form

Posted: Sat Aug 28, 2010 11:29 pm
by Jonah Bron
Try changing the method on the form to "get" like <form action="insert.php" method="get">

And then replace all of the "$_POST" s in the code with "$_GET".

Re: Trouble with Inserting Data to Mysql via PHP Form

Posted: Sun Aug 29, 2010 5:19 am
by Adsummy
Okay, I tried changing the method from "post" to "get" and all the references of "$_POST" to "$_GET". I had actually tried it before, but I tried again, and still no go. I also tried inserting the contents of insert.php directly into the form action tag but apparently with the security update to the code, it returns "No Data Present" into the page source code and basically all the html after that point disappears so the form doesn't even show up on my webpage. Any other suggestions?

Re: Trouble with Inserting Data to Mysql via PHP Form

Posted: Sun Aug 29, 2010 2:40 pm
by Jonah Bron
Inserting the code into the action variable totally does not do what you think it should. The action attribute simply indicates where to send the post/get data when the submit button gets pushed. There is another issue here. Whenever I submit the form, instead of going to insert.php, it goes to the same page. There are a couple of things that could cause this problem:

1. There is some javascript interfering (this can be ruled out. The problem still exists with js disabled)
2. There is more code in insert.php than you have provided
3. Some retarded URL rewriting
4. Some other sort of weird server settings

Unless it is the second, you need to talk to your host about this issue, because nothing, the form or the code in insert.php, would cause something like this.

Re: Trouble with Inserting Data to Mysql via PHP Form

Posted: Sun Aug 29, 2010 3:44 pm
by Adsummy
Am I understanding you right that when I hit the submit button it should leave the current webpage and go to insert.php? That might be a dumb question but this is my first initiation into php and mysql, and although I am learning a lot, I still do not understand how it all should behave.

Okay, so option 1 is ruled out, and option 2 is also ruled out, because there is no more code there than what I shared earlier, so I will contact my hosting company to discuss the issue. You have been incredibly helpful, and I appreciate it a lot. I'll definitely post here if I discover the solution.

Re: Trouble with Inserting Data to Mysql via PHP Form

Posted: Mon Aug 30, 2010 8:22 am
by Adsummy
Okay, I found the problem. I deleted this line from the top of the page (right below the doctype declaration):

<form xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

And it appears to be working correctly. Thank you, Jonah, for your assistance in helping to pinpoint the problem.