更新MySQL仅更新2个字段 - PHP

时间:2015-12-03 17:56:04

标签: php mysql pdo

由于某种原因,我能够更新的只有2个字段是first_name&姓。任何帮助将不胜感激。我试过回声其余的领域,他们似乎很好地完成了它。我也收到通知,查询已成功执行..谢谢!

EDITED

HTML表格

<form action="update.php" class="text-center" method="post"> 
          <div class="form-group">
             <label for="first_name">First Name:</label>
             <input type="first_name" name="first_name" class="form-   control" placeholder="first name">
         </div>
         <div class="form-group">
            <label for="last_name">Last Name:</label>
             <input type="last_name" name="last_name" class="form-control" placeholder="last name">
         </div>
         <div class="form-group">
            <label for="address">Address:</label>
             <input type="address" name="address" class="form-control" placeholder="address">
         </div>
         <div class="form-group">
            <label for="city">City:</label>
             <input type="city" name="city" class="form-control" placeholder="city">
         </div>
         <div class="form-group">
            <label for="state">State:</label>
             <input type="state" name="state" class="form-control" placeholder="state">
         </div>
         <div class="form-group">
            <label for="phone">Phone:</label>
             <input type="phone" name="phone" class="form-control" placeholder="phone">
         </div>
         <div class="form-group">
            <label for="email">Email:</label>
             <input type="email" name="email" class="form-control" placeholder="xxxx@xxxxx.com">
         </div>
         <div class="form-group">
            <label for="category">Category:</label>
             <input type="category" name="category" class="form-control" placeholder="category">
         </div>


           <input class="btn btn-primary" type="submit" name="submit" value="Update">
   </form>

PHP脚本:

<?php
include ('dataBase.php');
include ('functions.php');
$id = $_POST['users'];
echo $id;
$first_name = filter_input(INPUT_POST,'first_name');
$last_name = filter_input(INPUT_POST,'last_name');
$address = filter_input(INPUT_POST,'address');
$city = filter_input(INPUT_POST,'city');
$state = filter_input(INPUT_POST,'state');
$phone = filter_input(INPUT_POST,'phone');
$email = filter_input(INPUT_POST,'email');
$category = filter_input(INPUT_POST,'category');
echo $first_name;

$query = "UPDATE customers SET ";
    $query.= "first_name = '$first_name', ";
    $query.= "last_name = '$last_name' ";
    $query.= "address = '$address' ";
    $query.= "city = '$city' ";
    $query.= "state = '$state' ";
    $query.= "phone = '$phone' ";
    $query.= "email = '$email' ";
    $query.= "category = '$category' ";
    $query.= "WHERE id = $id ";

 echo $address;

   try {
$statement = $db->prepare($query);
$statement->bindValue(':first_name',$first_name);
$statement->bindValue(':last_name',$last_name);
$statement->bindValue(':address',$address);
$statement->bindValue(':city',$city);
$statement->bindValue(':state',$state);
$statement->bindValue(':phone',$phone);
$statement->bindValue(':email',$email);
$statement->bindValue(':category',$category);
$success = $statement->execute();
$statement->closeCursor();
$message = ($statement == False) ? 'Execute Failed' : 'Execute 
Successful';
echo $message;
}
catch (PDOException $e)
{
$error_message = $e->getMessage();
include('database_error.php');
exit();
}
$statement->closeCursor();
echo $address
?>

1 个答案:

答案 0 :(得分:3)

问题是查询实际上没有任何要绑定的参数:

$query = "UPDATE customers SET ";
$query.= "first_name = '$first_name', ";
$query.= "last_name = '$last_name' ";
$query.= "WHERE id = $id ";

在PDO中,您必须使用格式?表示非命名参数,或:paramater_name表示命名参数。您在脚本中进一步使用了命名参数,因此应更新您的查询以反映预期参数。

修复此问题将导致脚本看起来像这样:

<?php
include ('dataBase.php');
include ('functions.php');
$id = $_POST['users'];
echo $id;
$first_name = filter_input(INPUT_POST,'first_name');
$last_name = filter_input(INPUT_POST,'last_name');
$address = filter_input(INPUT_POST,'address');
$city = filter_input(INPUT_POST,'city');
$state = filter_input(INPUT_POST,'state');
$phone = filter_input(INPUT_POST,'phone');
$email = filter_input(INPUT_POST,'email');
$category = filter_input(INPUT_POST,'category');
echo $first_name;

$query = "UPDATE customers SET ";
$query.= "first_name = :firstname, ";
$query.= "last_name = :lastname, ";
$query.= "address = :address, ";
$query.= "city = :city, ";
$query.= "state = :state, ";
$query.= "phone = :phone, ";
$query.= "email = :email, ";
$query.= "category = :category ";
$query.= "WHERE id = :id ";

echo $address;

try {
    $statement = $db->prepare($query);
    $statement->bindValue(':firstname',$first_name);
    $statement->bindValue(':lastname',$last_name);
    $statement->bindValue(':address',$address);
    $statement->bindValue(':city',$city);
    $statement->bindValue(':state',$state);
    $statement->bindValue(':phone',$phone);
    $statement->bindValue(':email',$email);
    $statement->bindValue(':category',$category);
    $statement->bindValue(':id',$id);
    $success = $statement->execute();
    $statement->closeCursor();
    $message = ($statement == False) ? 'Execute Failed' : 'Execute Successful';
    echo $message;
}
catch (PDOException $e)
{
    $error_message = $e->getMessage();
    include('database_error.php');
    exit();
}
$statement->closeCursor();
echo $address
?>

另请注意,我添加了$statement->bindValue(':id',$id);,因为$id未绑定在原始代码中。