2017-05-09 61 views
-1

我正在使用PHP为自学课程中的学校做一些数据库项目,所以我对语法知之甚少。我试图根据给定的ID来更改数据库的一行。PHP - 基于另一个变量从数据库中获取变量

继承人的我想要的一些伪代码来实现:

if(databaseRowWithThis$id name == x) 
    UPDATE database SET name = '$name' WHERE id='$id'; 

如果这是混乱的,请告诉我,我会尽力把它清除掉。 在此先感谢。

回答

2
PHP

与语法

$conn = mysqli_connect($server,$login,$pw,$database); // connection info 

$sql = "UPDATE yourtablename SET name= ? WHERE id=?"; // placeholders for parameters 
$stmt = $conn->prepare($sql); // prepare the query 
if($stmt){ 
    $stmt->bind_param("is",$id,$name); // bind the parameters to the ?, i for integers, s for string, must be in exact order as the query 
    $stmt->execute(); // execute 
    $stmt->close(); // close statement 
} 
$conn->close(); // close the connection