-7
我試圖更新由php中的任何計算的表單值recored的MySQL表,但它不起作用。請你幫我,謝謝。如何通過計算表單值更新MySQL表?
我試圖更新由php中的任何計算的表單值recored的MySQL表,但它不起作用。請你幫我,謝謝。如何通過計算表單值更新MySQL表?
你應該使用mysqli和準備好的語句,就像這樣。
我希望這是足夠的,你不給我這麼工作...
<?php
//Get the form value and ID of the database record to update
$value = $_POST['value']; // Value submitted by a form element (replace this with whatever you want to change)
$id = $_POST['id']; //ID, could be of the user etc. (this will be a primary key inside the database) (does not have to be submitted via POST, I assume you know this already)
//Establish a new mysql connection
$mysqli = new mysqli($db_host,$db_user,$db_pass,$db_name);
//Set up a query
$query = "UPDATE table SET column_one=? WHERE id=?";
//Prepare the statement
$stmt = $mysqli->prepare($query);
//Bind the parameters
// 'si' = in the order of submitted valurs (column_one=? and id=?) (column_one is s and id is i, s is for string, i is for integer) (this defines what types of variables we are sending)
$stmt->bind_param('si', $value, $id);
//Execute the query
if($stmt->execute()){
//Get the amount of affected rows
$affected = $stmt->affected_rows(); //Should only be 1, but if your ID or whatever you're using to define which parts of the DB to update is not unique, then it can go higher ofc.
//Show success
echo "Database updated, $affected rows affected";
}else{
//Show error
echo "Error, say that this is shown, on stack overflow, as there's obviously something wrong.";
}
//Close the stmt/mysqli stuff
$stmt->close();
$mysqli->close();
加一個作出乾淨的解釋。 – Shubanker
我很欣賞Subhanker,謝謝。 –
請告訴我們,到目前爲止你已經嘗試了什麼。 – belwood
SO中的人們就像醫生一樣,我們無法看到患者(代碼)就開出任何藥物(修復)。 – Shubanker