2012-09-06 160 views
0

好的,所以我正在爲房地產公司建立一個數據庫,並且剛剛在3到4個月前開始使用PHP。我有這個錯誤,「你的SQL語法有錯誤;查看與你的MySQL服務器版本相對應的手冊,在'VALUES'('3605 2nd Street','','gfnfgnbfgn',' MD','21230','dfgbfgnbg','','','at line 2「。MySQL的語法錯誤,似乎無法弄清楚

這是涉及的代碼,有一個表單,我使用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的救星!

+1

你打開SQL注入。 –

+0

用循環填充查詢,而不是全部用手。它更乾淨,更短,不易出錯。 –

+1

'mysql_ *'已棄用。改爲使用PDO或MySQLi。 –

回答

1

您需要用``引號將所有列名稱中包含空格。

像這樣:

`Owner Address` 

最好的事情很可能是給所有的列名帶有空格的重命名,以取代以下劃線的空間。

0

這個例子:

select apt # from mytable 

將被解釋爲select apt,一切,包括和之後的#直到EOL將被解釋爲註釋。你會注意到SO的語法突出顯示也挑選了它。

這會工作:

select `apt #` from mytable 

甚至:

select `apt #` from `mytable` 
相關問題