虽然它是正确的,但mysql中的更新语句不起作用?

时间:2015-04-10 14:46:11

标签: php mysql

$sql = "UPDATE reservations SET status = '$this->status',remaining_time ='$this->remain',cost = '$this->cost' WHERE id = '$this->id'";

此代码无效,但正确

我正在使用面向对象的php。 $this->id是链接从另一个页面传递的变量。

当我运行代码时,它告诉我它是成功的但是没有受影响的行。

以上一行是以下代码的一部分:

<?php
class edit {
    private $status;
    private $remain;
    private $cost;
    private $id;
    public function edit_data() {
        $this->status = strtoupper(strip_tags($_POST['status']));
        $this->remain = strip_tags($_POST['remain']);
        $this->cost   = strip_tags($_POST['cost']);
        $submit       = $_POST['submit'];
        $this->id     = $_GET['edit'];
        $con = mysql_connect("localhost","root","")
        or die("Failed to connect to the server: " . mysql_error());
        mysql_select_db("Users")
        or die("Failed to connect to the database: " . mysql_error());
        if($submit) {
            if($this->status and $this->remain and $this->cost) {
                $sql = "UPDATE reservations SET status = '".$this->status."',remaining_time ='".$this->remain."',cost = '".$this->cost."' WHERE id = '".$this->id."'";
                $query = mysql_query($sql,$con);
                if(!$query) {
                    echo("Could not update data: " . mysql_error());
                }   
                echo "<h4>Customer reservation data has been updated successfully.</h4>";
                echo "Number of affected rows: " . mysql_affected_rows();
            }   
            else {
                echo "Please fill in all fields.";
            }
        }
        mysql_close($con);
    }   
}
$edit = new edit();
echo $edit->edit_data();
?>

3 个答案:

答案 0 :(得分:1)

你确定你的连接吗? $sql = "UPDATE reservations SET status = '$this->status',remaining_time ='$this->remain',cost = '$this->cost' WHERE id = '$this->id'"; 打印$sql以查看值。

如果您的数据库已更新,您将收到0个受影响的行。

答案 1 :(得分:0)

我不完全确定,但试试这个,

"UPDATE reservations SET status = '".$this->status."',remaining_time ='".$this->remain."',cost = '".$this->cost."' WHERE id = '".$this->id."'";

答案 2 :(得分:0)

您的表格似乎不包含满足条件的值。

您可以通过执行简单查询来检查。

   $sql = "select * from reservations where id='$this->id'";