mysql - 错误在哪里? (MySQL)

标签 mysql

我有点不知所措。我试图让这个表单提交到 MYSQL 服务器(托管在我的机器上),但它一直抛出这个错误:

错误:您的 SQL 语法有误;查看与您的 MySQL 服务器版本对应的手册,了解在第 1 行的 '' 附近使用的正确语法

有人愿意看一下您是否能发现错误吗?

这是我的 SQL 代码:

<?php

//Establish value variables
$firstName=$_POST['firstName']; 
$lastName=$_POST['lastName']; 
$email=$_POST['email']; 
$phone=$_POST['phone']; 
$address=$_POST['address']; 
$zipcode=$_POST['zipcode']; 
$state=$_POST['state']; 
$city=$_POST['city']; 
$petname=$_POST['petname']; 
$petType=""; 
$neut="";
 if(isset($_POST['neut'])){ $neut = $_POST['neut']; }
 if (isset($_POST['petType'])){$petType = $_POST['petType']; }

//establish connection
$con = mysql_connect("localhost","root","pwdpwd");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("pet_shop", $con);

//initializing the $error variable + array holding errors
$error = array ();
if (empty($firstName)) { $error[]='You forgot to enter your first name!'; }
if (empty($lastName)) { $error[]='You forgot to enter your last name!'; }
if (empty($email)) { $error[]='You forgot to enter your email!'; }
if (empty($phone)) { $error[]='You forgot to enter your phone number!'; }
if (empty($address)) { $error[]='You forgot to enter your address!'; }
if (empty($city)) { $error[]='You forgot to enter your city!'; }
if (empty($state)) { $error[]='You forgot to enter your state!'; }
if (empty($petname)) { $error[]='You forgot to enter your pet name!'; }


if(!empty($error))
{
die(implode('<br />', $error)); //stops script and prints errors    
}

if(!$error) {

//Insert information into columns
$sql="INSERT INTO grooming (firstName, lastName, email, phonenumber, address, zip, state, city, petname, PetType, NeuteredOrSpayed)";
}

//enter into if everything is okay
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error($con));
  }
  else 
    {
echo "<span>Appointment added!</span>"; 
}


//close connection
mysql_close($con);

?>
</body>
<br />
<a href="grooming.php"><button class="backButtn" id="backButtn">Back</button></a>
</html>

注意:“petType”指的是一组单选按钮,“neut”是一个复选框。

最佳答案

基础INSERT语法是:

INSERT INTO table_name (col_name, ...)
VALUES (...)

您的查询必须如下所示:

$sql = "INSERT INTO grooming (firstName, lastName, email, phonenumber, address, zip, state, city, petname, PetType, NeuteredOrSpayed) VALUES ('$firstname', '$lastname', '$email', '$phone', '$address', '$zipcode', '$state', '$city', '$petname', '$pettype', '$neut')";

如果您为表中的每一列提供值,则可以忽略列列表:

$sql = "INSERT INTO grooming VALUES ('$firstname', '$lastname', '$email', '$phone', '$address', '$zipcode', '$state', '$city', '$petname', '$pettype', '$neut')";

P.S.:与问题无关,但mysql_* functions are deprecated ,并且您的查询容易受到 SQL Injection 的攻击.

关于mysql - 错误在哪里? (MySQL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23304937/

相关文章:

php - 在php中将内容从一个页面传输到另一个页面

java - 如何使用从主机到 docker mysql 服务器的 JDBC 连接

mysql - 什么是从同一个表中选择具有某些 id 而不是其他 id 的行的最佳 MySQL 语句?

插入时的MySQL增量字段

mysql - 具有计算间隔值的 Doctrine 2 DATE_ADD mysql 函数

mysql - 如何在 PostgreSQL 和 MySQL 中获取数据库数据传输统计信息?

mysql - mysql索引优化问题

mysql不同行的时间差

mysql - MySQL中从第n条记录等中选择

MySQL 忘记了自动为外键创建索引?