PDA

View Full Version : newbie question on php and forms



luverboy4u
08-12-2008, 05:06 PM
I have the below code which is suppose to post back to itself but I can't get it to get the data from the form to be inserted into the database correctly and then get the data that was just inserted into the database and displayed it in a table.. I keep getting Query error. Can someone help me out?


<?php

//Database Connection
$msdb = mysql_connect("localhost", "user", "pass");
$db = mysql_select_db("test", $msdb) or die(mysql_error());



$fname = $_POST['fname'];
$lname = $_POST['lname'];
$phone = $_POST['phone'];

if (!isset($_POST['send']))
{

?>

<html>
<title></title>
<head></head>

<body TOPMARGIN="0" LEFTMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0">
<form id = "maintenanceForm" Name = "maintenanceForm" action="<?=$_SERVER['PHP_SELF']?>" method="POST" >
<fieldset>
<input type="hidden" id ="send" name="send" value="true">
<table cellpadding="5" cellspacing="0" border="0" align="center">
<tr>
<td align="right" class="formLabel"><B>First Name</B>:</td>
<td align="left"><INPUT id ="fnamel" NAME="fname" VALUE=""><BR></td>
</tr>
<tr>
<td align="right" class="formLabel"><label for="name"><B>Last Name</B></label>:</td>
<td align="left"><INPUT id = "lname" NAME="lname" VALUE=""><BR>
</div></td>
</tr>
<tr>
<td align="right" class="formLabel"><B>Phone</B>:</td>
<td align="left"><INPUT id ="phone" NAME="phone" VALUE=""><BR>
</td>
<td align="right">&nbsp;</td>
<td align="left"><INPUT TYPE='submit' id = "submit" NAME='submit' VALUE='Send'></td>
</tr>
</table>
</form>
<?php

}
else
{


$insertSql = "INSERT INTO members (firstname, lastname, phone) VALUES ('$fname', '$lname', '$phone')";

$result = mysql_query($insertSqL) or die("Insert Query failed: " . mysql_error());
$selectSQL = "SELECT FROM memebers where id = ". mysql_insert_id();
$result = mysql_query($selectSQL)
or die("Select Query failed: " . mysql_error());
echo "<TABLE BORDER='1'>";
echo "<TR>";
echo "<TH>First Name</TH><TH>Last Name</TH><TH>Phone</TH>";
echo "</TR>";

while ($row = mysql_fetch_array($result))
{
echo "<TR>";
echo "<TD>", $row['fname'], "</TD><TD>", $row['lname'], "</TD><TD>", $row['phone'], "</TD>";
echo "</TR>";
}

echo "</TABLE>";

mysql_close($connection);
}
?>

</body>
</html>

rangana
08-13-2008, 12:51 AM
Change your PHP into:


<?php
//Database Connection
$msdb = mysql_connect("localhost", "user", "pass");
$db = mysql_select_db("test", $msdb) or die(mysql_error());
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$phone = $_POST['phone'];
if (isset($_POST['send']))
{
$insertSql = "INSERT INTO members (firstname, lastname, phone) VALUES ('".$fname."','".$lname."','".$phone."')";
$result = mysql_query($insertSql) or die("Insert Query failed: " . mysql_error());
$selectSQL = "SELECT * FROM members where id = ". mysql_insert_id();
$result = mysql_query($selectSQL) or die("Select Query failed: " . mysql_error());
echo "<TABLE BORDER='1'>";
echo "<TR>";
echo "<TH>First Name</TH><TH>Last Name</TH><TH>Phone</TH>";
echo "</TR>";
while ($row = mysql_fetch_array($result))
{
echo "<TR>";
echo "<TD>", $row['firstname'], "</TD><TD>", $row['lastname'], "</TD><TD>", $row['phone'], "</TD>";
echo "</TR>";
}

echo "</TABLE>";

mysql_close($msdb);
}
?>


Hope that helps.

kuau
08-13-2008, 02:31 AM
Dear Luverboy (haha):

I know this was not your question, but just so you know, this is a perfect example of when tables are NOT needed. You can easily replace your table with a <div> and use css to position your form. The fields will all line up nicely if you simply assign a width to the label in your css. Using tables when not necessary is a bad habit to avoid.

Just my 2 cents. :)

Dirt_Diver
08-13-2008, 03:42 AM
kay I have a code on the site now that works but it's only for a username and password.

I have modified it to add other fields. I don't get any error codes when creating a member but it's not writing the data to the database anymore. Only thing I changed was this info:


"INSERT INTO users (first_name, last_name, email, gender, city, state, country, username, password)
VALUES ('".$_POST[first_name]."','".$_POST[last_name]."','".$_POST[email]."','".$_POST[gender]."','".$_POST[city]."','".$_POST[state]."','".$_POST[country]."','".$_POST[username]."','".$_POST[pass]."','".$_POST[pass2]."')";
//VALUES ('".$_POST['username']."', '".$_POST['pass']."')"; //reference Code
$add_member = mysql_query($insert);


Got any clues as to what I did wrong?

kuau
08-13-2008, 03:44 AM
You left the single quotes out...


$_POST['first_name']

rangana
08-13-2008, 04:26 AM
You should make use of mysql error reporting.
Try to add highlighted everytime you run a query so as to realize where/what the error is:


$add_member = mysql_query($insert) or die(mysql_error());



It is your responsibility to die() if necessary….. - PHP Manual

kuau
08-13-2008, 04:29 AM
Dear Rangana: How do you add the yellow highlight? I tried using the A with the line under it and it didn't work. Thanks. e :)

rangana
08-13-2008, 04:32 AM
It's this image: http://dynamicdrive.com/forums/images/editor/icode.gif.

You can however make use of the bbcode: .

Hope that helps.

kuau
08-13-2008, 04:37 AM
Oh, cool! I thought that #i was for italics. Thanks :)

Dirt_Diver
08-13-2008, 10:53 AM
You should make use of mysql error reporting.
Try to add highlighted everytime you run a query so as to realize where/what the error is:


$add_member = mysql_query($insert) or die(mysql_error());


When I run the error report I get this phase error
"Parse error: parse error, unexpected T_LOGICAL_OR in..."

Dirt_Diver
08-13-2008, 11:19 AM
You left the single quotes out...


$_POST['first_name']


okay I have added the single quotes and I still can't post to the server. Here is the code along with my form. I think it's something in my form...


$insert = "INSERT INTO users (first_name, last_name, email, gender, city, state, country, username, password)
VALUES ('".$_POST['first_name']."', '".$_POST['last_name']."', '".$_POST['email']."', '".$_POST['gender']."', '".$_POST['city']."', '".$_POST['state']."', '".$_POST['country']."', '".$_POST['username']."', '".$_POST['pass']."', '".$_POST['pass2']."')";
//VALUES ('".$_POST['username']."', '".$_POST['pass']."')"; //reference Code
$add_member = mysql_query($insert);


<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table width="379" border="0">
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td><span class="formw_signup"><strong>First Name:*</span></strong></td>
<td><input name="first_name" type="text" id="first_name" maxlength="50"></td>
</tr>
<tr>
<td><span class="formw_signup"><strong> Last Name:*</span></strong></td>
<td><input name="last_name" type="text" id="last_name" maxlength="50"></td>
</tr>
<tr>
<td><span class="formw_signup"><strong><span class="formw_signup"><strong>Email Address:*</span></strong></td>
<td><input name="email" type="text" id="email" maxlength="50"></td>
</tr>
<tr>
<td><span class="formw_signup"><strong><span class="formw_signup"><strong>Mailing Address:</span></strong></td>
<td><input name="address" type="text" id="address" maxlength="40"></td>
</tr>
<tr>
<td><span class="formw_signup"><strong>City:*</span></strong></td>
<td><input name="city" type="text" id="city" maxlength="25"></td>
</tr>
<tr>
<td><span class="formw_signup"><strong>State:*</span></strong></td>
<td><input name="state" type="text" id="state" maxlength="20"></td>
</tr>
<tr>
<td><span class="formw_signup"><strong>Postal Code:</span></strong></td>
<td><input name="zip" type="text" id="zip" maxlength="10"></td>
</tr>
<tr>
<td><span class="formw_signup"><strong>Country:*</span></strong></td>
<td><input name="country" type="text" id="country" maxlength="25"></td>
</tr>
<tr>
<td><span class="formw_signup"><strong>Gender:*</span></strong></td>
<td><select name="gender" id="gender">
<option>Choose a Gender</option>
<option value="M">M</option>
<option value="F">F</option>
</select></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td><span class="formw_signup"><strong>Website Address:</span></strong></td>
<td><input name="website" type="text" id="website" value="http://" maxlength="100"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="201"><span class="formw_signup"><strong>Username:* (3-15 charcters)</span></strong></td><td width="153">
<input type="text" name="username" maxlength="15">
</td></tr>
<tr>
<td><span class="formw_signup"><strong> Password:*</strong></td><td>
<input type="password" name="pass" maxlength="25">
</td></tr>
<tr>

<td><span class="formw_signup"><strong>Confirm Password:*</strong></td>
<td><input type="password" name="pass2" maxlength="25"></td>
</tr>
<tr>
<td colspan="2"><div align="center"><strong><span class="formw_signup">* Required field</span> </strong></div></td></tr>
<tr>
<th class="formw_signup">&nbsp;</th>
<th class="formw_signup">&nbsp;</th>
</tr>
<tr><th colspan=2><input type="submit" name="submit" value="Sign Up"></th></tr> </table>
</form>

Dirt_Diver
08-14-2008, 02:10 AM
ahhh yes got it thank you. just to be clear I also had pass2 in there and didn't need it. Thanks for the help