Submitting a form does not add data to MySQL database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
brokenshadows
Forum Newbie
Posts: 7
Joined: Wed Apr 22, 2009 12:16 pm

Submitting a form does not add data to MySQL database

Post by brokenshadows »

To start, let me say that I'm new to both MySQL and PHP. If fact, this is the first time I've tried to build anything from scratch with both. I've worked with databases before and have a good understanding of how they are supposed to work, but I obviously have no idea how to use PHP to connect to one.

If my question has been answered in another post, please accept my apologies and direct me to said post.

My problem is that I have a database that I'm trying to add data into using a simple form. When I fill in the form and hit my Submit button...nothing happens beyond the form fields clearing themselves. After hitting Submit, I pull up the database in phpMyadmin and there is no new data to be seen anywhere.

Here's the code for the form page:

Code: Select all

 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>RMVD</title>
</head>
 
<body>
<form id="contacts" name="contacts" method="post" action"contacts_insert.php">
<p>Parent's First Name:
<input name="first" type="varchar" id="first" />
</p>
<p>Parent's Last Name:
<input name="last" type="varchar" id="last" />
</p>
<p>Second Parent's First Name:
<input name="first2" type="varchar" id="first2" />
</p>
<p>Parent's Last Name:
<input name="last2" type="varchar" id="last2" />
</p>
<p>Home Phone:
<input name="homephone" type="varchar" id="homephone" />
</p>
<p>Cell Phone:
<input name="cellphone" type="varchar" id="cellphone" />
</p>
<p>Work Phone:
<input name="workphone" type="varchar" id="workphone" />
</p>
<p>E-Mail Address:
<input name="email" type="varchar" id="email" />
</p>
<input type="submit" name="Submit" value="Submit" />
</form>
 
 
</body>
</html>
 
And the code for contacts_insert.php

Code: Select all

 
<?php
$host="***removed***";
$user="***removed***";
$password="***removed***";
$database="contacts";
 
mysql_connect($host,$user,$password)or die("Can not connect DB");
@mysql_select_db($database) or die( "Unable to select database");
 
$first=$_POST['first'];
$last=$_POST['last'];
$first2=$_POST['first2'];
$last2=$_POST['last2'];
$homephone=$_POST['homephone'];
$cellphone=$_POST['cellphone'];
$workphone=$_POST['workphone'];
$email=$_POST['email'];
mysql_query("INSERT INTO contacts(first, last, first2, last2, homephone, cellphone, workphone, email) values('$first','$last','$first2','$last2','$homephone','$cellphone','$workphone','$email')");
mysql_close();
?>
 
I'm sure I missed something simple and will subsequently feel like an idiot when it has been pointed out to me...but I'm willing to risk it and would appreciate any help you could offer me.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Submitting a form does not add data to MySQL database

Post by califdon »

Probably your problem is merely the lack of a space after "INSERT INTO contacts", but there are several important issues/warnings here.

So that you can find out what's going on, never hard code your SQL statement into the mysql_query() function. Always assign your SQL string to a variable, which you reference in the mysql_query() function. In this way, you can debug a problem by echoing the SQL string to observe why it isn't working.

Second, use the or die(mysql_error()) construct with such mysql commands as mysql_query(). Like this:

Code: Select all

$sql = "INSERT INTO contacts (first, last, first2, last2, homephone,
    cellphone, workphone, email) 
VALUES ('$first','$last','$first2','$last2','$homephone',
    '$cellphone','$workphone','$email')";
mysql_query($sql) or die(mysql_error()."<br />$sql<br />");
If your query succeeds, the "or die()" doesn't happen, but if it fails, it returns False, which triggers the "or die()" which will then echo the MySQL error message and your actual SQL statement, with the values, and then exit the script ("die").

FInally, and perhaps most importantly, NEVER insert data into a database directly from $_POST variables without first "sanitizing" them! Hackers are constantly looking for web sites that neglect to use such data sanitizing methods as mysql_real_escape_string() to foil hackers who exploit the SQL injection vulnerability. You definitely should read http://www.juniper.net/security/auto/vu ... 18219.html and http://us2.php.net/mysql_real_escape_string.
brokenshadows
Forum Newbie
Posts: 7
Joined: Wed Apr 22, 2009 12:16 pm

Re: Submitting a form does not add data to MySQL database

Post by brokenshadows »

ok, I commented out my own script, put yours in, and still nothing...and no errors.

I'm about to go read those two links you posted, but so far it's still broken with no indication as to why...
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Submitting a form does not add data to MySQL database

Post by califdon »

brokenshadows wrote:ok, I commented out my own script, put yours in, and still nothing...and no errors.

I'm about to go read those two links you posted, but so far it's still broken with no indication as to why...
OK, show us your new contacts_insert.php script (omit your original, commented-out part). And (I didn't pay close enough attention before), remove the "@" preceding your mysql_select_db() function--what that does is tell PHP to suppress any error messages that might come from that line, which is fine for a fully-tested production script, but it may be hiding the very error message that you need, while in the debugging phase.

I would, at least for debugging purposes, insert a line like this somewhere in your script:

Code: Select all

echo "This is coming from new_contacts.php";
As you move on from this first attempt at PHP/MySQL, you will nearly always want to have some browser output as part of a script that updates your database in any way, perhaps only a "Record added" message or something. And in the debugging phase, you absolutely MUST send something to the browser, to find out what's going on. Even if absolutely nothing shows up, THAT in itself is a powerful diagnostic, because it is usually caused by there being a PHP SYNTAX error that the web server can't figure out, so it just doesn't send any output to the browser at all. Whenever you expect to see some output from a PHP script and there is absolutely none, it's a sure sign that you have a syntax error in your PHP code. If you can't find it by inspection, you can selectively comment out blocks of PHP code and try running it again, in order to isolate the problem to one or another block of code.
brokenshadows
Forum Newbie
Posts: 7
Joined: Wed Apr 22, 2009 12:16 pm

Re: Submitting a form does not add data to MySQL database

Post by brokenshadows »

Code: Select all

 
<?php
$host="***server address removed***";
$user="rivermilljags";
$password="***password removed***";
$database="rivermilljags";
echo "Echo 1";
mysql_connect($host,$user,$password)or die("Can not connect DB");
mysql_select_db($database) or die( "Unable to select database");
echo "Echo 2";
$first=$_POST['first'];
$last=$_POST['last'];
$first2=$_POST['first2'];
$last2=$_POST['last2'];
$homephone=$_POST['homephone'];
$cellphone=$_POST['cellphone'];
$workphone=$_POST['workphone'];
$email=$_POST['email'];
echo "Echo 3";
$sql = "INSERT INTO contacts (first, last, first2, last2, homephone, cellphone, workphone, email) VALUES ('$first','$last','$first2','$last2','$homephone', '$cellphone','$workphone','$email')";
mysql_query($sql) or die(mysql_error()."<br />$sql<br />");
mysql_close();
echo "Echo 4";
?>
 
none of my echo's report back, it just resets the form...which is what I want it to do in the end, so that information can be added in repeatedly...
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Submitting a form does not add data to MySQL database

Post by califdon »

Your form is not resetting, so much as it is just returning you to the same html page, which is, after all, just a blank form.

Inasmuch as I don't spot any syntax error in what you posted, I would suspect that your web server is simply not serving PHP scripts. Do you have ANY other PHP script that DOES run? Try a file that contains nothing but:

Code: Select all

<?php
phpinfo();
?>
which should print several browser screens worth of configuration info about PHP, MySQL, etc. Does this work?

Are you running this on a local web server? If so, which server, Apache or Microsoft? Or are you running this on a hosting service or what? (Hmm, you do understand that PHP only works when it is being run on a web server, I hope. In case you missed that point, that would explain everything! You can't just use a browser to open a local file, that simply doesn't work.)
brokenshadows
Forum Newbie
Posts: 7
Joined: Wed Apr 22, 2009 12:16 pm

Re: Submitting a form does not add data to MySQL database

Post by brokenshadows »

yep, it works...did you want me to cut and paste the info it returned?

I'm using godaddy hosting. I used to have my own webserver test box in my office, but it was repurposed a few months ago...and then of course I end up having a need for it :banghead:

PHP 4.3.11, MySQL version 5.0.18, and Apache server according to the info page
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Submitting a form does not add data to MySQL database

Post by califdon »

brokenshadows wrote:yep, it works...did you want me to cut and paste the info it returned?

I'm using godaddy hosting. I used to have my own webserver test box in my office, but it was repurposed a few months ago...and then of course I end up having a need for it :banghead:

PHP 4.3.11, MySQL version 5.0.18, and Apache server according to the info page
Oh, I use godaddy hosting on 8 domains and usually do my development on the live sites. To me, it's as easy as developing on my local machine (although I'm running Apache/Mysql/PHP on my Windows machine, too) and then having to change db info, etc. My inquiry was just a shot in the dark.

OK, then, here's what I would do: you've got to figure out why phpinfo.php works and your other script doesn't. So you start sectioning off your script, using block comments (/* to start a comment, */ to end a comment). I'd start by starting a comment just after your "Echo 1" line and ending just before your closing ?>. Don't even start with your html form, just call your contacts_insert.php script, at this point. If THAT doesn't produce the echo, there's something wrong with your file! Like it is in the wrong directory, or the file permissions are wrong, or something like that. If it echoes "Echo 1", then move the start of the comments block down after your "Echo 3" line, etc. Wherever it fails to show anything on the screen, you'll then know that there's some kind of a problem in the code that you've exposed. Frankly, I don't see anything wrong in your code (aside from the aforementioned security issue), so the only thing I can think of is possibly a stray nonprinting character in your file, or something odd like that.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Submitting a form does not add data to MySQL database

Post by califdon »

Actually, I just copied your script and ran it locally and it doesn't appear that anything's wrong with it. Of course, it couldn't connect to the database, but it did run and produce Echo 1 and the error message "Can not connect DB". So there's something wrong with your file! See if it's in the right place on the server, if so, and considering that it's a short file, open a new file and enter the code again, in case there's something screwy on that first blank line or something like that.
brokenshadows
Forum Newbie
Posts: 7
Joined: Wed Apr 22, 2009 12:16 pm

Re: Submitting a form does not add data to MySQL database

Post by brokenshadows »

the files are both in the same sub-directory on the server...so I'm pretty sure that's not it. Someone in another forum pointed out that I was missing an = after "action" in said file, but that didn't fix it either. I'll try re-coding the file and see what happens...
brokenshadows
Forum Newbie
Posts: 7
Joined: Wed Apr 22, 2009 12:16 pm

Re: Submitting a form does not add data to MySQL database

Post by brokenshadows »

it's fixed

in my form code I had name before type

example

Code: Select all

<input name="first" type="varchar" id="first" />
instead of

Code: Select all

<input type="varchar" name="first" id="first" />
putting the type first fixed it...now when I hit submit, the data is injected into the database and the page returns all 4 echos

**GLEE**
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Submitting a form does not add data to MySQL database

Post by califdon »

I'm embarrassed to admit that I overlooked the missing = after the action parameter, too. That would explain it, since that's what calls your script. But it wouldn't have any effect on your testing by calling the script directly. So now you're back to testing it by calling it directly and using comments to partition it into parts that work and parts that don't work. You can also do things like create another one-liner:

Code: Select all

<?php
echo "Hello";
?>
if that doesn't work, I'm stumped. If that works, start adding your code to that file and see where it stops.

My guess is that it will now work. Often when we have a frustrating experience, we get ourselves confused about what we've done or haven't done.
brokenshadows
Forum Newbie
Posts: 7
Joined: Wed Apr 22, 2009 12:16 pm

Re: Submitting a form does not add data to MySQL database

Post by brokenshadows »

:D you must have been typing that while I was typing my previous response...it's working now, thanks a bunch for all your help...it was a heck of a lot better than the other forum I asked the same question on...their idea of "help" was basically RTFM :crazy:
Post Reply