問題描述
我有一個 70/80 字段表單,我需要將其插入到表中,因此我不是手動創建一個巨大的插入語句,而是首先根據表單中輸入的名稱在我的數據庫中創建了一個表,這里是代碼我用來創建/更改表格
I have a 70/80 field form that I need to insert into a table so instead of manually creating one huge insert statement I firstly have created a table in my db from the names of the inputs in the form here is the code that I use to create/alter the table
function createTable($array, $memberMysqli)
{
foreach ($array as $key => $value)
{
//echo "<p>Key: ".$key." => Value: ".$value . "</p>";
$query = "ALTER TABLE questionnaire ADD ".$key." text";
if($stmt = $memberMysqli->prepare($query))
{
$success = $stmt->execute();
}
}
echo "<h1>Array count: ". count($array) ."</h1>" ;
}
這很好用并且完全按照我想要的方式改變了表格.現在要插入表單值來執行此操作,我執行一個基本的單字段插入存儲行的 id,然后循環更新該行的所有后期變量.這是我的代碼:
This works fine and altered the table exactly how I wanted it. Now to insert the form values to do this I do a basic one field insert store the id of the row and then have loop all the post variables updating that row. Here is my code for that:
$stmt = $memberMysqli->prepare("INSERT INTO questionnaire(userid) VALUES (?)");
$stmt->bind_param('s', $_POST['userid']);
$stmt->execute();
$rowid = $stmt->insert_id;
$stmt->close();
$memberMysqli->autocommit(FALSE);
function updateColumn($memberMysqli, $query, $uid, $value)
{
if ($value)
{
$stmt = $memberMysqli->prepare($query);
//Throws bind param error here
$stmt->bind_param("ss", $value, $uid);
$stmt->execute();
}
}
function loopInputs($array, $memberMysqli, $rowid)
{
foreach ($array as $key => $formvalue)
{
var_dump($key);
updateColumn($memberMysqli, "UPDATE questionnaire SET $key = ? WHERE id = ?", $rowid, $formvalue);
}
}
loopInputs($_POST, $memberMysqli, $rowid);
$memberMysqli->commit();
$memberMysqli->close();
這會引發綁定參數錯誤,我不知道為什么.
This throws a bind param error and I have no idea why.
推薦答案
O,讓我們嘗試一個規范的答案.
O, let's try a canonical answer.
調用成員函數
(或期望參數1為mysqli_result,布爾值給定
用于程序風格)本身不是錯誤,而只是一個癥狀,對于其他一些問題.
這個錯誤消息意味著沒有在應該創建的地方創建對象.
Call to a member function
(or expects parameter 1 to be mysqli_result, boolean given
for the procedural style) is not an error itself but just a symptom, for some other problem.
This very error message means that no object was created where should.
因此 - 創建 $stmt
對象時出現問題.
很可能是查詢有問題.因此,我們需要跟蹤該錯誤.
So - there was a problem with creating an $stmt
object.
Most likely it's a problem with the query.
So, we need to track that error down.
除非明確詢問,否則 Mysqli 不會告訴您發生了什么.因此,您必須始終檢查與服務器交互的每個 mysqli 函數的結果,如果結果為 FALSE - 檢查 $mysqli->error
.
Mysqli won't tell you what's going on unless asked explicitly. So, you have to always check the result of every mysqli function interacting with server and if result is FALSE - check $mysqli->error
.
將 mysqli 錯誤消息轉換為 PHP 錯誤也很重要,讓它根據站點范圍的錯誤報告設置進行處理.
It is also very important to convert mysqli error message into PHP error, to let it go according site-wide error reporting settings.
如果您在整個應用程序代碼中使用 mysqli_query() 而不將其封裝到某個輔助類中,trigger_error()
是引發 PHP 錯誤的好方法,因為它還會告訴您文件和發生錯誤的行號
If you are using mysqli_query() all over the application code without encapsulating it into some helper class, trigger_error()
is a good way to raise a PHP error, as it will tell you also the file and the line number where error occurred
因此,您所有的prepare()
、execute() 和query()
調用都必須這樣編寫:
So, all your prepare()
, execute() and query()
calls have to be written this way:
$stmt = $mysqli->prepare($query) or trigger_error($mysqli->error."[$query]");
或程序風格
$res = mysqli_query($mysqli,$query) or trigger_error(mysqli_error($mysqli)."[$query]");
在你所有的腳本中
從那時起,您將被告知原因,即未創建對象的原因.(如果你對這個 or
語法感到好奇,我已經在這里解釋過)請注意,錯誤消息中還包含查詢,讓您可以直觀地檢查它并在另一個環境中進行測試.
in all your scripts
and since then you will be notified of the reason, why the object weren't created.
(If you're curious of this or
syntax, I've explained it here)
Note that query also included in the error message, to let you inspect it visually and test in another environment.
但是,如果您將查詢封裝到某個類中,觸發器錯誤中的文件和行將毫無用處,因為它們將指向調用本身,而不是導致某些問題的應用程序代碼.所以,在運行封裝的mysqli命令時,還得用另一種方式:
However, if you're encapsulating your query into some class, file and line from trigger error will be quite useless as they will point to the call itself, not the application code that caused certain problem. So, when running mysqli commands encapsulated, another way have to be used:
$result = $mysqli->query($sql);
if (!$result) {
throw new Exception($mysqli->error." [$query]");
}
as Exception 將為您提供堆棧跟蹤,它將引導您找到調用錯誤查詢的位置.
as Exception will provide you with a stack trace, which will lead you the the place from which an erroneous query were called.
請注意,您必須能夠看到一般的 PHP 錯誤.在實時站點上,您必須查看錯誤日志,因此設置必須
Note that you have to be able to see PHP errors in general. On a live site you have to peek into error logs, so, settings have to be
error_reporting(E_ALL);
ini_set('display_errors',0);
ini_set('log_errors',1);
在本地開發服務器上,在屏幕上出錯是可以的:
while on a local development server it's all right to make errors on screen:
error_reporting(E_ALL);
ini_set('display_errors',1);
當然,您永遠不應該在語句前使用錯誤抑制運算符 (@).
and of course you should never ever use error suppression operator (@) in front of your statements.
這篇關于Mysqli更新拋出調用成員函數bind_param()錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!