繁体   English   中英

MySQL语法错误,似乎无法弄清楚

[英]MySQL Syntax Error, can't seem to figure it out

好的,所以我正在为房地产公司建立数据库,大约3或4个月前才刚开始使用PHP。 我遇到此错误,“您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册,以在'VALUES('3605 2nd Street','','gfnfgnbfgn',' MD”,“ 21230”,“ dfgbfgnbg”,“,”,“第2行”。

这是涉及的代码,有一个表单,我使用POST方法将信息中继到此页面。

<?php
session_start();
require('connect.php');
$address = $_POST["Address"];
$apt = $_POST["Apt #"];
$city = $_POST["City"];
$state = $_POST["State"]; 
$zip = $_POST["Zip"];
$tenant1 = $_POST["Tenant1"];
$tenant1phone = $_POST["Tenant 1 Phone"];
$tenant1email= $_POST["Tenant 1 Email"];
$tenant2 = $_POST["Tenant 2"];
$tenant2phone = $_POST["Tenant 2 Phone"];
$tenant2email= $_POST["Tenant 2 Email"];
$tenant3 = $_POST["Tenant 3"];
$tenant3phone = $_POST["Tenant 3 Phone"];
$tenant3email= $_POST["Tenant 3 Email"];
$daterented = $_POST["Date Rented"];
$rent = $_POST["Rent"];
$leaseexp= $_POST["Lease Expiration"];
$datedue = $_POST["Date Due"];
$vacant = $_POST["Vacant"];
$rentpaid= $_POST["Rent Paid"];
$amountdue = $_POST["Amount Due"];
$fee = $_POST["Fee"];
$secdeposit= $_POST["Security Deposit"];
$leadcert = $_POST["Lead Cert"];
$leadcertdate= $_POST["Lead Cert Date"];
$repairlimit= $_POST["Repair Limit"];
$key = $_POST["Key #"];
$bedrooms= $_POST["Bedrooms"];
$bathrooms = $_POST["Bathrooms"];
$heat = $_POST["Heat"];
$appliances= $_POST["Appliances"];
$owner = $_POST["Owner"];
$owneraddress = $_POST["Owner Address"];
$ownercity = $_POST["Owner City"];
$ownerstate= $_POST["Owner State"];
$ownerzip = $_POST["Owner Zip"];
$ownerphone1 = $_POST["Owner Phone 1"];
$ownerphone2 = $_POST["Owner Phone 2"];
$ownerother= $_POST["Owner Other"];
$owneremail = $_POST["Owner Email"];

$sql = ("INSERT INTO info (Address, Apt #, City, State, Zip, Tenant 1, Tenant 1 Phone,     
Tenant 1 Email, Tenant 2, Tenant 2 Phone, Tenant 2 Email, Tenant 3, Tenant 3 Phone,   
Tenant 3 Email, Date Rented, Rent, Lease Expiration, Date Due, Vacant, Rent Paid,   
Amount Due, Fee, Security Deposit,  Lead Cert, Lead Cert Date, Repair Limit, Key #, 
Bedrooms, Bathrooms,  Heat, Appliances, Owner, Owner Address, Owner City, Owner State, 
Owner Zip, Owner Phone 1, Owner Phone 2, Owner Other, Owner Email)

VALUES ('$address', '$apt', '$city', '$state', '$zip', '$tenant1', '$tenant1phone',   
'$tenant1email', '$tenant2', '$tenant2phone', '$tenant2email', '$tenant3', 
'$tenant3phone', '$tenant3email', '$daterented', '$rent', '$leaseexp', '$datedue', 
'$vacant', '$rentpaid', '$amountdue', '$fee', '$secdeposit', '$leadcert', 
'$leadcertdate', '$repairlimit', '$key', '$bedrooms', '$bathrooms', '$heat', 
'$appliances', '$owner', '$owneraddress', '$ownercity', '$ownerstate', '$ownerzip', 
'$ownerphone1', '$ownerphone2', '$ownerother', '$owneremail')");

var_dump($sql);
mysql_query($sql)or die(mysql_error());
if(mysql_affected_rows()>=1){
echo "<p>Entry Added</p>";
echo "<a href='index.php'>Home</a>";
}else{
echo "<p>Entry Not Updated</p>";
}

?>

我在此页面上几乎有完全相同的问题,该页面编辑了一行

<?php
session_start();
require('connect.php');
$id = $_GET["id"];
$address = $_POST["Address"];
$apt = $_POST["Apt #"];
$city = $_POST["City"];
$state = $_POST["State"];
$zip = $_POST["Zip"];
$tenant1 = $_POST["Tenant 1"];
$tenant1phone = $_POST["Tenant 1 Phone"];
$tenant1email= $_POST["Tenant 1 Email"];
$tenant2 = $_POST["Tenant 2"];
$tenant2phone = $_POST["Tenant 2 Phone"];
$tenant2email= $_POST["Tenant 2 Email"];
$tenant3 = $_POST["Tenant 3"];
$tenant3phone = $_POST["Tenant 3 Phone"];
$tenant3email= $_POST["Tenant 3 Email"];
$daterented = $_POST["Date Rented"];
$rent = $_POST["Rent"];
$leaseexp= $_POST["Lease Expiration"];
$datedue = $_POST["Date Due"];
$vacant = $_POST["Vacant"];
$rentpaid= $_POST["Rent Paid"];
$amountdue = $_POST["Amount Due"];
$fee = $_POST["Fee"];
$secdeposit= $_POST["Security Deposit"];
$leadcert = $_POST["Lead Cert"];
$leadcertdate= $_POST["Lead Cert Date"];
$repairlimit= $_POST["Repair Limit"];
$key = $_POST["Key #"];
$bedrooms= $_POST["Bedrooms"];
$bathrooms = $_POST["Bathrooms"];
$heat = $_POST["Heat"];
$appliances= $_POST["Appliances"];
$owner = $_POST["Owner"];
$owneraddress = $_POST["Owner Address"];
$ownercity = $_POST["Owner City"];
$ownerstate= $_POST["Owner State"];
$ownerzip = $_POST["Owner Zip"];
$ownerphone1 = $_POST["Owner Phone 1"];
$ownerphone2 = $_POST["Owner Phone 2"];
$ownerother= $_POST["Owner Other"];
$owneremail = $_POST["Owner Email"];

$sql = "UPDATE info
SET 
Address='$address', 
Apt #='$apt', 
City='$city',
State='$state', 
Zip='$zip',
Tenant 1='$tenant1', 
Tenant 1 Phone='$tenant1phone',
Tenant 1 Email='$tenant1email',
Tenant 2='$tenant2', 
Tenant 2 Phone='$tenant2phone',
Tenant 2 Email='$tenant2email', 
Tenant 3='$tenant3', 
Tenant 3 Phone='$tenant3phone',
Tenant 3 Email='$tenant3email',  
Date Rented='$daterented',
Rent='$rent', 
Lease Expiration='$leaseexp',
Date Due='$datedue', 
Vacant='$vacant',
Rent Paid='$rentpaid', 
Amount Due='$amountdue',
Fee='$fee', 
Security Deposit='$secdeposit',
Lead Cert='$leadcert', 
Lead Cert Date='$leadcertdate',
Repair Limit='$repairlimit', 
Key #='$key',
Bedrooms='$bedrooms', 
Bathrooms='$bathrooms',
Heat='$heat', 
Appliances='$appliances',
Owner='$owner', 
Owner Address='$owneraddress', 
Owner City='$ownercity',
Owner State='$ownerstate', 
Owner Zip='$ownerzip',
Owner Phone 1='$ownerphone1', 
Owner Phone 2='$ownerphone2', 
Owner Other='$ownerother',
Owner Email='$owneremail'
WHERE id='$id'";
var_dump($sql);
mysql_query($sql)or die(mysql_error());
if(mysql_affected_rows()>=1){
echo "<p>Entry Updated</p>";
echo "<a href='index.php'>Home</a>";
}else{
echo "<p>Entry Not Updated</p>";
}
?>

我已经看过15遍了,似乎找不到语法错误。 我看的越多,它看起来就越像一堵巨大的文字墙。 也许更有经验的人可以看看它,并立即看到它。 我在不同的站点中使用了几乎相同的代码,没有任何问题,唯一的不同是,它具有更多的变量。 预先感谢,这个站点及其上的优秀人们一直是学习PHP的救星!

您需要在所有列名的中间用引号将其引起来并带有空格。

像这样:

`Owner Address`

最好的办法可能是用空格重命名所有列名,用下划线替换空格。

这个例子:

select apt # from mytable

将被解释为select apt ,包括#和EOL之后的所有内容都将被解释为注释。 您会注意到SO的语法突出显示也可以做到这一点。

这将工作:

select `apt #` from mytable

甚至:

select `apt #` from `mytable`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM