我的更新表单不会更新以前的数据

时间:2013-02-27 12:51:43

标签: php mysql sql-update prepared-statement

我认为有一种更清洁的方式,但我试图让我能够以更新形式编辑数据。问题是更新查询不起作用。任何人都可以指出所犯的错误。不管怎么说,还是要谢谢你。

public function editData()
{
    if (isset($_GET['pid']))
    {
        $target = $_GET['pid'];
        if($edit = $this->db->prepare("SELECT id, title, price, description, address, includ, duration FROM trips WHERE id=?"))
        {
            $edit->bind_param('i', $target);
            $edit->execute();
            $edit->store_result();
            $edit->bind_result($id, $name, $price, $description, $address, $includ, $duration);
            $edit->fetch();
            $this->editForm($id, $name, $price, $description, $address, $includ, $duration);
        }
        else
        {
            echo "something went wrong";
        }
    }
}

public function editForm($id, $name, $price, $description, $address, $includ, $duration)
{
    $add .= '<form name="invent" method="post"  action="index.php?res=resources&adm=admin&page=inventory.php&pid=$id" class="ínvent" enctype="multipart/form-data">';
    $add .= '<fieldset>';
    $add .= '<legend>Add products</legend>';
    $add .= "<label for='name'></label>";
    $add .= "<input type='text' name='user' value='".$name."' />";
    $add .= '</br>';
    $add .= "<label for='price'>price</label>";
    $add .= "<input type='number' name='price' value='".$price."'  />";
    $add .= '</br>';
    $add .= "<label for='description'>description</label>";
    $add .= "<textarea name='description' rows='10' cols= '80'>'".$description."' </textarea>";
    $add .= '</br>';
    $add .= "<label for='address'>address</label>";
    $add .= "<input name='address'  type='text' value='".$address."'>";
    $add .= '</br>';
    $add .= "<label for='include'>Including</label>";
    $add .= "<input name='include'  type='text' value='".$includ."'>";
    $add .= '</br>';
    $add .= "<label for='duration'>duration</label>";
    $add .= "<input name='duration'  type='text' value='".$duration."'>";
    $add .= '</br>';
    $add .= "<label for='img'>img</label>";
    $add .= "<input name='image'  type='file'>";
    $add .= '</br>';
    $add .="<input type='submit' name='update'/>";
    $add .= '</fieldset>';
    $add .= '</form>';
    echo $add;

    if(isset($_POST['update']))
    {   
        $update_name = $_POST['user'];
        $update_price = $_POST['price'];
        $update_description = $_POST['description'];
        $update_address = $_POST['address'];
        $update_includ = $_POST['include'];
        $update_duration = $_POST['duration'];

        if ($update = $this->db->prepare("UPDATE trips SET title = ?, price = ? , description = ?, address = ?, includ = ?, duration = ?  WHERE id=?"))
        {
            $update->bind_param("ssssssi", $update_name, $update_price, $update_description, $update_address, $update_includ, $update_duration, $id);
            $update->execute();
        }
        else
        {
            echo "couldnt update";
        }
        header("Location: index.php?res=resources&adm=admin&page=inventory.php");
    }
}

1 个答案:

答案 0 :(得分:0)

我看到的一个问题是你在更新检查例程中调用“header(...)”之前发出“echo”,因此页面不会按预期重定向

引用“请记住,在发送任何实际输出之前,必须通过普通HTML标记,文件中的空行或PHP来调用header()。” see PHP header manual