2015-11-30 98 views
-1

我正在嘗試設置一個準備好的語句以允許用戶註冊我的網頁。我的POST信息從我的表單正確傳遞到我的提交頁面,並且如果我刪除準備好的語句,我能夠在提交時成功插入?,但是當前代碼出現錯誤。PHP mysqli編寫語句INSERT錯誤

<?php 

if(isset($_POST['submit'])){ 

$uid = 'NULL'; 
$fn = $_POST['fn']; 
$ln = $_POST['ln']; 
$u = $_POST['u']; 
$p = $_POST['p']; 
$dob = $_POST['dob']; 
$sx = $_POST['sx']; 
$pn = $_POST['pn']; 
$a = $_POST['a']; 
$up = $_POST['CURRENT_TIMESTAMP']; 
$c = $_POST['cn']; 
$s = $_POST['s']; 
$z = $_POST['z']; 

require_once('../mysqli_connect.php'); 

$query = "INSERT INTO u (userid, fn, ln, username, p, dob, sx, pn, em, a, up) 
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 

$stmt = mysqli_prepare($mysqli, $query); 
if($stmt){ 
$stmt->bind_param('isssssssssi', $uid, $fn, $ln, $u, $p, $dob, $sx, $pn, $em, $a, $up); 

$stmt->execute(); 

$stmt->close(); 

} 

if (mysqli_query($mysqli, $query)) { 
    $userid = mysqli_insert_id($mysqli); 
    echo "Your user ID is ". $userid; 
} else { 
echo "Error: " . $query . "<br>" . mysqli_error($mysqli); 
} 
// display error if occurs 
var_dump($mysqli); 

mysqli_close($mysqli); 

?> 

這裏是我收到的錯誤代碼:

Error: INSERT INTO u (userid, fn, ln, username, p, dob, sx, pn, em, a, up) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) 
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 

我試圖改變PHP的版本,我目前正在運行5.3,但是當我切換到任何超出我得到的mysqli錯誤類。我已經試過回覆並引用了?的內容,但這似乎也不起作用。我希望有人能夠擴展已有的準備好的聲明,因爲我已經搜索到了高低,並且一直無法找到我的問題源於什麼。所以,我想我的問題是,如何通過準備好的語句正確傳遞變量,以及我需要在附近使用什麼語法?佔位符?

更新代碼:

if(isset($_POST['submit'])){ 

$uid = 'NULL'; 
$fn = $_POST['fn']; 
$ln = $_POST['ln']; 
$u = $_POST['u']; 
$p = $_POST['p']; 
$dob = $_POST['dob']; 
$sx = $_POST['sx']; 
$pn = $_POST['pn']; 
$em = $_POST['em']; 
$a = $_POST['a']; 
$c = $_POST['cn']; 
$s = $_POST['s']; 
$z = $_POST['z']; 


require_once('../mysqli_connect_aimU.php'); 

$query = "INSERT INTO u (userid, fn, ln, username, p, dob, sx, pn, em, a) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 
if (!$stmt = mysqli_prepare($mysqli, $query)) { 
echo "Error: ".$stmt->error; 
exit(); 
} 
if(!$stmt->bind_param('isssssssss', $uid, $fn, $ln, $u, $p, $dob, $sx, $pn, $em, $a)){ 
echo "Error: ".$stmt->error; 
} 
if($stmt->execute()){ 
$userid = $stmt->insert_id; 
echo "Your user ID is ".$userid; 
} else { 
echo "Error: ".$stmt->error; 
} 

$cityid= "SELECT id FROM c WHERE cn = '$c' LIMIT 1"; 
$result = mysqli_fetch_array($cityid); 
if ($result != true) { 
$query = "INSERT INTO c (cn) VALUES (?)"; 
if(!$stmt->bind_param('s', $cn)) { 
echo "insert error dawg".$stmt->error; 
} 
if(!$stmt->execute()){ 
$cityid = $stmt->insert_id; 
echo "Your city ID is".$cityid; 
} else { 
$query = "INSERT INTO ucl (cid, uid) VALUES (?, ?)"; 
if(!$stmt = mysqli_prepare($mysqli, $query)) { 
echo "Error: ".$stmt->error; 
exit(); 
} 
if(!$stmt->bind_param('ss', $cityid, $userid)){ 
echo "Error: ".$stmt->error; 
} 
if (!$stmt->execute()){ 
echo "Error: ".$stmt->error; 
} 
} 
} 
+1

爲什麼要執行相同的查詢兩次? 'if(mysqli_query($ mysqli,$ query)){''$ stmt-> execute();'是假設要發送的。 'query'函數不支持佔位符。 – chris85

+0

我認爲通過運行if mysqli_query我只是確保它成功通過,但是,你是對的,它是非常多餘的,因爲我可以在執行測試。謝謝,這解決了mysql語法錯誤。如何根據檢查表中是否存在州和城市名稱,然後插入(如果不存在)來將其轉換爲多查詢?我也有表格鏈接用戶的城市/州,所以如果結果確實發生,我會採取該ID和用戶ID並插入鏈接。這是包裝在我的初始POST如果語句: – hoolakoola

+0

'$ cityid =「SELECT ID FROM c WHERE cn ='$ c'LIMIT 1」; $ result = mysqli_fetch_array($ cityid);如果($ result!= true){ $ query。=「INSERT INTO c(cn)VALUES(?)」; if(!$ stmt-> bind_param('s',$ cn)){ echo「insert error dawg」。$ stmt-> error; (!$ stmt-> execute()){ } $ cityid = $ stmt-> insert_id; echo「您的城市ID是」。$ cityid; } else { echo「Error:」。$ stmt-> error; } }' – hoolakoola

回答

1

您已經使用準備好的語句,這樣你就不會那麼也需要使用mysqli_query()。您可以/應該在每一步都進行錯誤檢查以幫助識別任何問題。

$query = "INSERT INTO u (userid, fn, ln, username, p, dob, sx, pn, em, a, up) 
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 
if (!$stmt = mysqli_prepare($mysqli, $query)) 
{ 
    echo "Error: ".$stmt->error; 
    exit(); 
} 
if (!$stmt->bind_param('isssssssssi', $uid, $fn, $ln, $u, $p, $dob, $sx, $pn, $em, $a, $up)) 
{ 
    echo "Error: ".$stmt->error; 
    exit(); 
} 
if ($stmt->execute()) { 
    $userid = $stmt->insert_id; 
    echo "Your user ID is ". $userid; 
} else { 
    echo "Error: ".$stmt->error; 
} 
$stmt->close(); 
+0

謝謝,有時它只是需要獲得另一個角度。問題是試圖$ _POST時間戳,你不能這樣做,但我已經將我的數據庫設置爲自動增量,所以我不需要將它包含在準備好的語句中。而且,我意識到我得到的錯誤信息是因爲mysql,而不是php。我對這些概念並不熟悉,並且在過去的幾周裏一直被鎖定在這些概念中,有時會忘記它們不共享常量。即使我成功插入了所有的信息,但我仍然得到了與mysql語法相同的錯誤消息,但不知道爲什麼。 – hoolakoola