Jump to content

MySQL error on form submission


webguync

Recommended Posts

I am getting an error when I fill out form information and try and submit to a MySQL DB. The error is:

 

"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 ''919-555-1212)' at line 1"

 

the code for the form is:

 

<form action="formtoemailpro.php" method="post">
<fieldset>
<legend></legend>
<table cellspacing="5">
<tr><td><span class="asterisk">*</span><span class="small"> indicates a required field</td></tr>
<tr><td><span class="asterisk">*</span> First Name:</td><td><input type="text" size="30" name="fname"></td></tr>
<tr><td><span class="asterisk">*</span> Last Name:</td><td><input type="text" size="30" name="lname"></td></tr>
<tr><td><span class="asterisk">*</span> Zip Code:</td><td><input type="text" size="30" name="zip"></td></tr>
<tr><td><span class="asterisk">*</span> Login email:</td><td><input type="text" size="30" name="loginemail"></td></tr>
<tr><td><span class="asterisk">*</span> Login Password:</td><td><input type="password" size="30" name="loginpw"></td></tr>
<tr><td>  Phone:</td><td><input type="text" size="10" name="phone"></td></tr>

<tr><td><input type="hidden" name="date_submitted"></td>
</tr></table></td></tr>

<tr><td> 

</td><td align="right"><input type="image" src="images/Send.jpg" value="Send"></td></tr>
</table>
</fieldset>
</form>

 

and the code to submit the form

 

<?php

//set up database and table names
$db_name ="shadowdata";
$table_name ="RegistrationForm";
$now = date('Y-m-d H:i:s');

//connect to MySQL and select database to use
$connection = @mysql_connect("localhost","username","PW") or die(mysql_error());

$db = @mysql_select_db($db_name,$connection) or die(mysql_error());

//create SQL statement and issue query
$sql = "INSERT INTO $table_name (fname, lname, zip, loginemail, loginpw, phone, date_submitted) VALUES ($_POST[fname], '$_POST[lname]', '$_POST[zip]', '$_POST[loginemail]', '$_POST[loginpw]', '$_POST[phone], '$_POST[date_submitted]')";

$result = @mysql_query($sql,$connection)or die(mysql_error());
/* E-mail stuff here */
$my_email = "[email protected]";
$bcc = "";
$subject = "Comments from contact form";
$message = "You received a mesage from {$_POST['loginemail']}"; //populate as you see fit from data from the form
mail($my_email, $subject, $message);

 

and the DB info:

 


CREATE TABLE `RegistrationForm` (
  `fname` varchar(255) NOT NULL,
  `lname` varchar(255) NOT NULL,
  `zip` int(5) NOT NULL,
  `loginemail` varchar(255) NOT NULL,
  `loginpw` varchar(255) NOT NULL,
  `phone` int(10) NOT NULL,
  `date_submitted` datetime NOT NULL default '0000-00-00 00:00:00',
  `UserID` tinyint(4) NOT NULL auto_increment,
  PRIMARY KEY  (`UserID`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

 

 

Link to comment
https://forums.phpfreaks.com/topic/116327-mysql-error-on-form-submission/
Share on other sites

$sql = "INSERT INTO $table_name (fname, lname, zip, loginemail, loginpw, phone, date_submitted) VALUES ($_POST[fname], '$_POST[lname]', '$_POST[zip]', '$_POST[loginemail]', '$_POST[loginpw]', '$_POST[phone], '$_POST[date_submitted]')";

 

 

Look at $_POST[fname], needs these ' ' around it.

thanks,

 

this is submitting now, but the field I have set up for date_submitted isn't submitting any date and the field I have set up for phone only submits the area code. For instance if I enter 919-555-1212, the only information in the database will be 919. Any ideas on how to fix these few items?

thanks, the only part not working is the date_submitted field. It only displays the default 0000-00-00 00:00:00. I have the field as type datetime , Null No. In the past I have set a variable such as:

 

$date_submitted = date('Y-m-d H:i:s');

 

but not sure how I would incorporate that into my current SQL(?)

 

$sql = "INSERT INTO $table_name (fname, lname, zip, loginemail, loginpw, phone, date_submitted) VALUES ('$_POST[fname]', '$_POST[lname]', '$_POST[zip]', '$_POST[loginemail]', '$_POST[loginpw]', '$_POST[phone]', '$_POST[date_submitted]')";

 

 

if you want to save current timestamp so there is no need for getting date from PHP. you can use now() function in mysql..

 

$sql = "INSERT INTO $table_name (fname, lname, zip, loginemail, loginpw, phone, date_submitted) VALUES ('$_POST[fname]', '$_POST[lname]', '$_POST[zip]', '$_POST[loginemail]', '$_POST[loginpw]', '$_POST[phone]', now())";

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.