如何将此SQL语句转换为MySqli

时间:2016-05-27 18:12:08

标签: php mysql mysqli code-conversion

    <?php session_start();
$conn = new mysqli("localhost", "****", "****", "root_project");
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }
$_SESSION['a'] = $conn->real_escape_string($_SESSION['a']);
$sql="UPDATE loantrans SET bookeep = 'y' WHERE loanID ='".$_SESSION['a']."'";
if ($conn->query($sql)) {
     $message = "The form was sent successfully.";
     echo "<script type='text/javascript'>alert('$message');</script>";
 } else {
     $err= "Error: " . $sql . "<br>" . $conn->error;
     $err2= "There was a problem with sending the form."."<br>".
            "Please contact us"."<br>"."<br>".$err;
     echo "<script type='text/javascript'>alert('$err2');</script>";
 }
$conn->close(); ?>

1 个答案:

答案 0 :(得分:1)

我仍然愿意帮助你。假设你从零开始......:

$conn = new mysqli("localhost", "username", "some_password", "database");
if ($conn->connect_error)
    die("Connection failed: ".$conn->connect_error);

$_SESSION['a'] = $conn->real_escape_string($_SESSION['a']);

$sql = "UPDATE loantrans SET bookeep = 'y' WHERE loanID = '".$_SESSION['a']."' AND bookeep = 'n'";

if ($conn->query($sql))
    echo "Record updated successfully";
else
    echo "Error updating record: " . $conn->error;

$conn->close();

但要注意SQL injections。我想我现在逃脱了特殊的字符串。祝你好运。