Jump to content

Recommended Posts

Hello,

 

I created a simple form to capture info entered into textboxes. It is posted to a php script to write to a mysql database. The script connects to the database without error, but the only thing that gets entered into the database is the date that is declared on the php script page and the autoincrment id.

 

The form can be found at http://www.lvoaf.org/reg.html

 

Here is some code snippets:

 

<h1>New Member Sign Up</h1>

<form id="form1" name="form1" method="post" action="reg_members_php.php">

 

---------------------------------------------

php script

 

<html>

<head>

<title>LVOAF Registration Form</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

<body background="images/hg_background.jpg" text="#000000">

<?php

$host = 'localhost';

$user = 'correct';

$pass = 'password';

$myDB = 'lvoaforg_register';

// Connection to DB is either sucessful or dies

$connect = mysql_connect($host, $user, $pass) or die ("Could not connect to the server\n");

//insert string for first table

$today = date('M d Y');

$table_name = 'member_reg';

//create query string to insert data into table

$query = "INSERT INTO $table_name (today, fname, lname, address, city, state, zip, phone, email, year_grad, school_grad, played) VALUES ('$today', '$fname', '$lname', '$address', '$city', '$state', '$zip', '$phone', '$email', '$year_grad', '$school_grad', '$played')";

?>

<table width="80%" align="center">

<tr>

<td height="233" valign="top" bgcolor="#003399">

<p><img src="images/logo_med.jpg" width="750" height="250"></p>

</td>

</tr>

<tr>

<td valign="top" bgcolor="#0099FF">

<p><br>

<br>

<?php

//Print statement to verify if connection and inserts were made

mysql_select_db($myDB);

if (mysql_query($query, $connect)){

print "Hello!!! $fname, your registration for Bethany's 2010 VBS High Seas Adventure was successful on $today!<br>";

print "We'll be seeing you at Bethany July 12th - 16th!!!<br><br>";

print "Thank you!!!<br><br>";

print "Darcy Bierer - Director of Children's Ministries";

} else {

print " $fname, your registration request failed to register!";

} mysql_close ($connect);

?>

</p>

</td>

</tr>

<tr>

<td valign="top" bgcolor="#0099FF">

<p> </p>

<p>Form Data Entered On: <?php

//Print last name back to screen

print ("$today");

?>

</p>

<p>First Name: <?php

//Print last name back to screen

print ("$fname");

?>

</p>

<p>Last Name: <?php

//Print last name back to screen

print ("$lname");

?>

</p>

<p>Address: <?php

//Print last name back to screen

print ("$address");

?>

</p>

<p>City: <?php

//Print last name back to screen

print ("$city");

?>

</p>

<p>State: <?php

//Print last name back to screen

print ("$state");

?>

</p>

<p>Zip: <?php

//Print last name back to screen

print ("$zip");

?>

</p>

<p> </p>

<p> </p>

<p> </p></td>

</tr>

</table>

</body>

</html>

The book/site/tutorial/class.. where you found code showing how to reference form field data is 10+ years out of date.

 

You must reference the form's post data using - $_POST['the_form_field_name_here'] (including the single-quotes around the field name.)

 

You also need to validate each input value - is it empty? Is it too long for the size of the database field? Does it contain expected content?

 

Lastly, you need to escape string data and cast numerical data as the appropriate numerical data type, right before putting it into the query statement to prevent sql errors if the data contains sql special characters and to prevent sql injection by hackers.

Please use the [code][/code] tags around your code, as it helps make both your post and your code a lot easier to read.

 

I also suggest having a look at a previous post of mine, where I've shown (approximately) how I usually write code that handles form submissions. Notice that I've put all of the PHP code above the HTML code, and how I've used functions along with return to control the flow of the logic. If you compare it to the original code, it should help show what I've done (and hopefully why too). :)

Should you have any questions about the reasons for anything, please feel free to ask. Same with if you have any trouble with applying our tips to your code, if you cannot find anything after searching.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.