【问题标题】:Basic Input from html into mysql database从 html 到 mysql 数据库的基本输入
【发布时间】:2013-11-28 03:18:04
【问题描述】:

我试图使用基本的 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);
?>

我的插入语句有什么问题。据我所知,它看起来正确且相当简单。有什么帮助吗?

【问题讨论】:

    标签: php html mysql database forms


    【解决方案1】:

    更正您的 SQL 语句如下,并为每个变量加上单引号

    $sql = "INSERT INTO users 
           (firstname,lastname,emailid,teamname)
           VALUES ( '$firstname','$lastname','$emailid','$team_name' )";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-15
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      相关资源
      最近更新 更多