php mysqli更新集不起作用

时间:2015-12-02 09:46:11

标签: php mysql mysqli prepared-statement

我有一个带有mysql数据库的php脚本。我有准备语句的UPDATE SET查询。但是当我提交表单时,我得到了正确更改的echo更新,但是当我查看数据库时,数据没有更改。

出了什么问题?

提前感谢。

我的代码是:

include 'connect.php';


$sql = "UPDATE names SET name = ?, lastname = ?, address = ?, place = ?, telephone = ?, description = ? WHERE id = ?  ";
$stmt = $link->prepare($sql);
$stmt->bind_param("issssss", $id, $name, $lastname, $address, $place, $telephone, $description);


 $id = $_POST['id'];
 $name = $_POST['name'];
 $lastname = $_POST['lastname'];
 $address = $_POST['address'];
 $place = $_POST['place'];
 $telephone = $_POST['telephone'];
 $description = $_POST['description'];
 $stmt->execute();


if (! $sql) {
    echo "Data not changed";
} else{
    echo "Data correct changed";
    }
$stmt->close();
mysqli_close($link);

表单代码为:

<form  action="update.php" method="POST" >
        <input type="text" name="id"  placeholder="id">
            <br /><br />
        <input type="text" name="name"  placeholder="name">
            <br /><br />
        <input type="text" name="lastname"  placeholder="lastname">
            <br /><br />
        <input type="text" name="address"  placeholder="address">
            <br /><br />
        <input type="text" name="place"  placeholder="place">
            <br /><br />
        <input type="text" name="telephone"  placeholder="telephone">
            <br /><br />
        <textarea  rows="4" cols="50" placeholder="description" name="description"></textarea>
            <br /><br />
        <input type="submit" name="update" value="update">

</form>

2 个答案:

答案 0 :(得分:1)

你的绑定参数not in a sequence需要放置id at last

<?php
$sql = "UPDATE names SET name = ?, lastname = ?, address = ?, place = ?, telephone = ?, description = ? WHERE id = ?  ";
$stmt = $link->prepare($sql);
$stmt->bind_param("ssssssi", $name, $lastname, $address, $place, $telephone, $description,$id);

使用$stmt->affected_rows检查数据是否已更新

$stmt->execute();
//You can get the number of rows affected by your query
$nrows = $stmt->affected_rows;
if (!$nrows) {
     echo "Data not changed";
}

答案 1 :(得分:0)

尝试重新排序此类代码

$id = $_POST['id'];
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$place = $_POST['place'];
$telephone = $_POST['telephone'];
$description = $_POST['description'];

// this one from top put here
$stmt = $link->prepare($sql);
$stmt->bind_param("ssssssi", $name, $lastname, $address, $place, $telephone, $description,$id);

// and then execute sql
$stmt->execute();

if (! $sql) {
    echo "Data not changed";
} else{
    echo "Data correct changed";
}
$stmt->close();
mysqli_close($link);