哪里出错? (MYSQL)

时间:2014-04-26 00:03:04

标签: mysql

我在这里有点不知所措。我正在尝试将此表单提交给MYSQL服务器(托管在我的计算机上),但它不断抛出此错误:

Error: 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 '' at line 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'是一个复选框。

2 个答案:

答案 0 :(得分:4)

基本的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的攻击。<​​/ p>

答案 1 :(得分:2)

您忘了设置值

$sql="INSERT INTO <tableName> (columnNames) VALUES (valuesHere)