2013-03-07 60 views
0

只是在將記錄插入我的MYSQL表時有點問題。數據正常,但是當代碼查詢是否有條目時,我總是收到一個錯誤。PHP通過MYSQLI插入記錄,但總是給出錯誤回聲

$sql = mysqli_query($connect,"INSERT INTO testtable (fnhusband,fnwife,surname,email,address,suburb,city,postcode,region,number,altnumber) 
VALUES ('$fnhusband','$fnwife','$surname','$email','$address','$suburb','$city','$postcode','$region','$number','$altnumber')"); 

if (!mysqli_query($connect,$sql)) 
    { 
    die('Error:' . mysqli_error()); 
    } 
else { 
    echo "1 record added"; 
    mysqli_close($connect); 
    } 
+1

很酷,有什麼錯誤? – Scuzzy 2013-03-07 00:20:38

+0

從查詢檢查中刪除$ connect並向我們顯示結果。 – Stepo 2013-03-07 00:21:24

+3

你實際上是這樣做的:'mysqli_query($ connect,mysqli_query($ connect,「INSERT ....」));',第一個(內部)成功,第二個失敗。 – Wrikken 2013-03-07 00:23:59

回答

2

假設$連接是鏈接:

$connect = new mysqli("localhost", "my_user", "my_password", "world"); 

嘗試改變

$sql = mysqli_query($connect,"INSERT INTO testtable (fnhusband,fnwife,surname,email,address,suburb,city,postcode,region,number,altnumber) 
VALUES ('$fnhusband','$fnwife','$surname','$email','$address','$suburb','$city','$postcode','$region','$number','$altnumber')"); 
$result = mysqli_query($connect,$sql); 

到:

$sql = "INSERT INTO testtable (fnhusband,fnwife,surname,email,address,suburb,city,postcode,region,number,altnumber) 
VALUES ('$fnhusband','$fnwife','$surname','$email','$address','$suburb','$city','$postcode','$region','$number','$altnumber')"; 
$result = $connect->query($sql); 

或程序:

$sql = "INSERT INTO testtable (fnhusband,fnwife,surname,email,address,suburb,city,postcode,region,number,altnumber) 
VALUES ('$fnhusband','$fnwife','$surname','$email','$address','$suburb','$city','$postcode','$region','$number','$altnumber')"; 
$result = mysqli_query($connect, $sql); 
+0

謝謝斯科特。像魅力一樣工作。 – DrDog 2013-03-07 00:54:53