我试图使用基本的php从html表单中将数据插入到mysql表中。我最初用基本的纯文本输入成功了。但是,当我尝试输入电子邮件ID时,我不断收到此错误:
Could not enter data: 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 '@gmail.com,moohawks )' at line 3
我的php代码如下:
$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
$emailid = $_REQUEST['emailid'];
$team_name = $_REQUEST['team_name'];
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'incredible';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "INSERT INTO users
(firstname,lastname,emailid,teamname)
VALUES ( $firstname,$lastname,$emailid,$team_name )";
mysql_select_db('adb project');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
$message2 = "Entered data successfully\n";
mysql_close($conn);
?>
我的insert语句有什么问题。据我所知,它看起来更正并且非常简单。有什么帮助吗?
答案 0 :(得分:2)
使用每个变量的单引号
更正您的SQL语句$sql = "INSERT INTO users
(firstname,lastname,emailid,teamname)
VALUES ( '$firstname','$lastname','$emailid','$team_name' )";