Jump to content

Trying to learn how to get form data into a MySQL database


rghollenbeck

Recommended Posts

I'm not trying to get a fancy looking form yet.  I just want my code to be functional first.  The problem seems to be with the query.  The web page DOES connect to the database.  I was able to run SELECT queries okay.  But with the following code, I just end up with a blank page (insert.php) but when I then go back to mysqladmin and SELECT query the database, no record was added.

 

I wonder what I'm doing wrong.

 

inputform.php:

<p>Fields marked <font color="red">*</font> are required.</p>
<form method="post" action="insert.php">

<font color="red">*</font>First Name <input name="fname" type="text" value="">
<font color="red">*</font>Last Name <input name="lname" type="text" value=""><br>
Address1 <input name="address1" type="text" value=""><br>
Address2 <input name="address2" type="text" value="">
City <input name="city" type="text" value="">, State <input name="state" type="text" value=""> 
Zip Code <input name="zipcode" type="text" value="">
Email<input name="email" type="text" value="">
URL <input name="url" type="text" value="">
<?php 

$link = mysql_connect('rghollenbeck.fatcowmysql.com', 'rich', '***********'); 
if (!$link) { 
die('Could not connect: ' . mysql_error()); 
} 

mysql_select_db(guestlist); 

$Users_IP_address = $_SERVER["REMOTE_ADDR"] ;
echo "<input type='hidden' name='ip' value=";
echo $Users_IP_address;
echo ">";
?>

<br>
<input name="submit" type="submit" value="Send">
</form>

 

 

insert.php:

<?php 
$link = mysql_connect('rghollenbeck.fatcowmysql.com', 'rich', '***********'); 
if (!$link) { 
die('Could not connect: ' . mysql_error()); 
} 
mysql_select_db(guestlist); 

mysql_query("INSERT INTO tblMain (fname, lname, address1, address2, city, state, zipcode, email, url, telephone, ip) VALUES ($_POST[fname], $_POST[lname], $_POST[address1], $_POST[address2], $_POST[city], $_POST[state], $_POST[zipcode], $_POST[email], $_POST[url], $_POST[telephone], $_POST[ip])");

?>

 

Thank you.

I added the following code:

 

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

 

and got the following error:

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /hermes/web06/b1946/moo.rghollenbeck/insert.php on line 10

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near . . . at line 1.

 

after I fixed the stupid mistakes, such as forgetting to put the single quote around the field names, etc., other more specific errors began to emerge.  As I fixed them, one by one, eventually the script began to work correctly.  However, it reported a "0" on the page.  I don't know why it is doing that, but I don't care.  I can redirect the page back to the original page, maybe with a javascript alert thanking the visitor for their submission.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.