gpt4 book ai didi

mysql - 错误在哪里? (MySQL)

转载 作者:行者123 更新时间:2023-12-01 00:21:58 28 4
gpt4 key购买 nike

我有点不知所措。我试图让这个表单提交到 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com