php-mysql Form

php-mysql Form

Posted by: siwilson6t12
Posted on: 2007-02-02 13:40:00

i have made a phpscript the code follows:

<?php
$con = mysql_connect("mysql.virtual-conflict.com","vc2006","******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test1", $con);

$sql="INSERT INTO custinfo (FirstName, street1, street2, city, state, zip, country, phone, email, pw1, pw2, secret_answer)
VALUES
('$_POST[firstname]','$_POST[surname]','$_POST[street1]','$_POST[street2]','$_POST[city]','$_POST[state]','$_POST[zip]','$_POST[country]','$_POST[phone]','$_POST[email]''$_POST[pw1]','$_POST[pw2]','$_POST[secret_answer]')";
{
die('Error: ' . mysql_error());
}
echo "1 record added";mysql_close($con)
?>

and i call this script in the html form. code follows

<form action="cust.php" method="post">
<h1>A little personal info please...</h1>
<h3>(See our <a href="privacy.html">privacy policy.</a>)</h3>
<table border="0">
<tr valign="top">
<td align="left"><h4>First Name:</h4></td>
<td><input name="firstname" value="" size="20" /></td>
</tr>
<tr>
<td align="left"><h4>Last:</h4></td>
<td><input name="surname" value="" size="30"/>
</td>
</tr>
<tr valign="top">
<td align="left"><h4>Street Address:</h4></td>
<td><input name="street1" size="32" maxlength="64" value="" />
<br />
<input name="street2" size="32" maxlength="64" value="" /></td>
</tr>
<tr valign="top">
<td align="left"><h4>City:</h4></td>
<td><input name="city" maxlength="64" size="30" value="" /></td>
</tr>
<tr valign="top">
<td align="left"><h4>State/Providence:</h4></td>
<td><h4>
<input name="state" maxlength="32" size="20" value="" />
</h4></td>
</tr>
<tr valign="top">
<td align="left"><h4>Zip Code:</h4></td>
<td><input name="zip" maxlength="32" size="16" value="" />
</td>
</tr>
<tr>
<td><h4>Country:</h4></td>
<td><select name="country" >
<option selected="selected">united States</option>
<option>United Kingdom</option>
</select></td>
</tr>
<tr valign="top">
<td align="left"><h4>Phone:</h4></td>
<td><input name="phone" value="" />
</td>
</tr>
</table><br />
<table border="0">
<tr valign="top">
<td align="left"><h4>Your current email:</h4></td>
<td><h3><input name="email" value="" />
<br />
(For login Use)</h3>
<br /></td>
</tr>
<tr valign="top">
<td align="left"><h4>Choose password:</h4></td>
<td><h3><input name="pw1" type="password" value="" /><br />
(6+characters 20 Max)</h3>
<br /></td>
</tr>
<tr valign="top">
<td align="left"><h4>Re-type password:</h4></td>
<td><input name="pw2" type="password" value="" />
<br /></td>
</tr>
<tr valign="top">
<td align="left"><h4>What city were you born in?</h4></td>
<td><input name="secret_answer" value="" />
</td>
</tr>
</table>
<br />

<h3>
<br />
<br />
<input type="checkbox" value="1" name="confirm">
I agree to abide by Virtual Conflicts <a href="tos.html" target="new">Terms of Service</a> and <a href="spam.html" target="new">Anti-Spam Policy</a>. Registration of domains requires acceptance of our <a href="register-agreement.html" target="new">Registration Terms</a>.</b><br />
You will also receive our monthly newsletter, which you may opt out of at any time.<br />
<input type="submit" />
<br />
</h3>
</form>

its in a nicely laid out table but when i hit submit it says error nothing more and nothing less

bugging me to hell as i cant find out why

please help

Thanks
Si Wilson

Re: php-mysql Form

Posted by: nathan823
Posted on: 2007-02-02 18:04:00

Can you show us the error message?

In the meanwhile, $_POST[firstname] should be $_POST["firstname"]. same for the rest of $_POST

Re: php-mysql Form

Posted by: siwilson6t12
Posted on: 2007-02-03 04:18:00

the error message is just error. nothing more and nothing less

Re: php-mysql Form

Posted by: nathan823
Posted on: 2007-02-03 18:21:00

ok here is the trick

$sql="INSERT INTO custinfo (FirstName, street1, street2, city, state, zip, country, phone, email, pw1, pw2, secret_answer)
VALUES
('$_POST[firstname]','$_POST[surname]','$_POST[street1]','$_POST[street2]','$_POST[city]','$_POST[state]','$_POST[zip]','$_POST[country]','$_POST[phone]','$_POST[email]''$_POST[pw1]','$_POST[pw2]','$_POST[secret_answer]')";

{
die('Error: ' . mysql_error());
}


you call die function after your sql query. I guess there is missing "if-else" here. and you forgot to excute the sql query. It should be something like
$result = mysql_query($sql); if (!$result){ die('Error: '.mysql_error());}

and your sql query can be simplified as
$sql="INSERT INTO custinfo
VALUES
('$_POST[firstname]','$_POST[surname]','$_POST[street1]','$_POST[street2]','$_POST[city]','$_POST[state]','$_POST[zip]','$_POST[country]','$_POST[phone]','$_POST[email]''$_POST[pw1]','$_POST[pw2]','$_POST[secret_answer]')";


remember to user doulbe codes in $_POST to retrieve the value of the variable as I mentioned in the last post. $_POST["firstname"]

:P

Tags: php mysql