2015-05-03 35 views

回答

1

你應該使用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();

+0

加一個作出乾淨的解釋。 – Shubanker

+0

我很欣賞Subhanker,謝謝。 –