(PHP)没有错误,但在mysql数据库中没有编辑

时间:2017-01-27 12:52:35

标签: php html mysql pdo

对于学校我正在创建一个销售动物的网站。我们需要一个管理页面,使所有者能够在网站上添加,编辑和删除动物。我们已经可以添加动物,但我似乎无法编辑现有的动物。我在PHP方面也不是很有经验。我添加了几个检查以查看该语句是否正在执行,一旦我填写表单并单击提交它告诉我'donedone',这应该意味着它有效 - 但表格保持不变。有人可以解释一下我面临的问题吗?这是我第一次通过SO询问。

edit1:而不是“where animal_id =':animal_id'”而不是动物没有。 33,而不是我在我的表单中输入的值,它用':class'和':price'等替换所有值。所以在将表单值绑定到'insert into'stmt时必须出错。

这是数据库连接

<?php

$server = 'localhost';
username = 'username';
$password = '*********';
$database = 'databasename';

$conn = new PDO("mysql:host=$server;dbname=$database;", $username,  $password);

?>

这是edit_animal文件:

<?php

session_start();

if( isset($_SESSION['user']) ){
  header("Location: /");
}

ini_set('display_errors', '0');   
error_reporting(E_ALL | E_STRICT);

require 'database.php';

$message = '';

 if(!empty($_POST['animal_name']) && !empty($_POST['official_name']) &&    !empty($_POST['average_lifespan']) && !empty($_POST['length']) && !empty($_POST['class']) && !empty($_POST['housing']) && !empty($_POST['animal_price']) && !empty($_POST['diet']) && !empty($_POST['description'])):

$sql = "UPDATE animal_test SET animal_name =':animal_name', animal_price =  ':animal_price', official_name =':official_name', average_lifespan = ':average_lifespan' , length =':length' , class =':class', housing = ':housing',  description=':description', diet =':diet' WHERE animal_id =':animal_id'";
  $stmt = $conn->prepare($sql);

  $stmt->bindParam(':animal_id', $_POST['animal_id']);
  $stmt->bindParam(':animal_name', $_POST['animal_name']);
  $stmt->bindParam(':official_name', $_POST['official_name']);
  $stmt->bindParam(':average_lifespan', $_POST['average_lifespan']);
  $stmt->bindParam(':length', $_POST['length']);
  $stmt->bindParam(':class', $_POST['class']);
  $stmt->bindParam(':housing', $_POST['housing']);
  $stmt->bindParam(':animal_price', $_POST['animal_price']);
  $stmt->bindParam(':description', $_POST['description']);
  $stmt->bindParam(':diet', $_POST['diet']);
  $result = $stmt->execute();

  if (!$stmt) {
    echo "Jammer";
  }
  if( $stmt){
   echo "done";
 }
 if ($result) {
  echo "done";
 }
  else{
    $message = 'Sorry there must have been an issue with editing your  animale';

}

endif;

?>

<!DOCTYPE html>
<html>
    <head>
        <title>Admin - Exotic Animal Shop</title>
        <link rel="stylesheet" type="text/css" href="admin_page.css">
    </head>
    <body>         

        <h2>Add a product</h2>

        <form action="<?php echo $_SERVER['PHP_SELF']; ?>"method="POST">



          <div class=add_product_div>

            <div id=animal_name_div>
              <p>Animal name</p>
              <input id=animal_name type="text" placeholder="Animal name" name="animal_name">
            </div>

            <div id=animal_id_div>
              <p>Animal ID</p>
              <input id=animal_id type="number" placeholder="Animal ID" name="animal_id" min="0">
            </div>

            <div id=price_div>
              <p>Price</p>
              <input id=animal_price type="number" placeholder="Price" name="animal_price" min="0">
            </div>

            <div id=official_name_div>
              <p>Official name</p>
              <input id=official_name type="text" placeholder="Official name" name="official_name">
            </div>

            <div id=current_age_div>
              <p>Average lifespan</p>
              <input id=average_lifespan type="text" placeholder="Average lifespan" name="average_lifespan">
            </div>
          </div>


          <div class=add_product_div>

            <div id=length_div>
              <p>Max length</p>
              <input id=length type="text" placeholder="Max length" name="length">
            </div>

            <div id=class_div>
              <p>Class</p>
              <input id=class type="text" placeholder="Class" name="class">
            </div>

            <div id=housing_div>
              <p>Housing</p>
              <input id=housing type="text" placeholder="Housing" name="housing">
            </div>

            <div id=animal_diet>
              <p>Diet</p>
              <input id=diet type="text" placeholder="Diet" name="diet">
            </div>
          </div>

            <div class=add_product_div>
              <p>Description</p>
              <textarea placeholder="Description" id="description" name="description" rows="20" cols="40"></textarea>
            </div>


            <div id=submit_div>
              <input id=submit type="submit" value="Submit">
            </div>

        </form>
        <form action="upload.php" method="post" enctype="multipart/form-data">
          <p>Select image to upload</p>
          <input type="file" name="userfile" id="userfile">
          <input type="submit" value="Upload Image" name="submit">
        </form>
</body>

1 个答案:

答案 0 :(得分:0)

你准备好的陈述不应该有&#34;&#39;&#34;替换字段周围的标记。

参见参考here

将其更改为类似于此的内容:

$sql = "UPDATE animal_test SET animal_name =:animal_name, 
  animal_price =  :animal_price, official_name =:official_name, 
  average_lifespan = :average_lifespan , length =:length,
  class =:class, housing = :housing,  description=:description,
  diet =:diet WHERE animal_id =:animal_id";

另一种解决方案是按编号使用位置占位符(&#34;?&#34;)和 bindParam 。请参阅参考here

$sql = "UPDATE animal_test SET animal_name =?, 
  animal_price =  ?, official_name =?, 
  average_lifespan = ?, length =?,
  class =?, housing = ?,  description=?,
  diet = ? WHERE animal_id = ?";

$stmt->bindParam(1, $_POST['animal_name']);
$stmt->bindParam(2, $_POST['animal_price']);

... and so on