dongtuoji5396 2015-10-07 06:55
浏览 19
已采纳

php代码没有更新数据库记录

I am doing a basic inventory system I was trying to edit and update record by clicking on edit button which pass the id to the process file through the url, there I pic the id and I was able to change and update the data, now the problem is the product category, before it was appearing in just a text box and I have to type the new product to replace the old one but this could make spelling or other mistakes, to avoid that I am trying to have drop down list which is loading products from database and I will choose the new one to replace but it's not working, process file picking the id from html file and query I have checked also it's working fine but data is not being updated.

 <html>

       <form name="form" action="edit_record.php" method="POST" enctype="multipart/form-data">        

          <select name="pro_update" class="formTxtInputmany">        
             Product Name: <option>SELECT PRODUCT</option>                
             <?php 
             $sql = "SELECT pro_id,pro_name FROM products ORDER BY pro_name ASC";
             $query = $conn->query($sql);
             $result = $query->fetchAll(PDO::FETCH_ASSOC);
             $arrlength = count($result);

               for ($x = 0; $x < $arrlength; $x++){?>            
                    <option value="<?php echo $result[$x]['pro_id'].','.$result[$x]['pro_name']; ?>">
                      <?php echo $result[$x]['pro_name']; ?>
                   </option><?php 
               } ?>  

          </select>
          <input type="submit" value="UPDATE THE RECORD">
       </form>        
</html>

    // process file     

       <?php

       if(isset($_POST['pro_update'])){    
          $pro_update = $_POST['pro_update'];

          if ($pro_update == "SELECT PRODUCT"){        
               echo "please choose producct";        
          }else{        

            $values = explode(',', $pro_update);

            $pro_id = (int)$values[0];
            $pro_update = $values[1];

            $updateQuery = "UPDATE products SET pro_name = $pro_update
            WHERE pro_id = $pro_id";
            $query = $conn->prepare($updateQuery);
           $query->execute(array($pro_update));  

          }    
       }
  • 写回答

4条回答 默认 最新

  • drgbpq5930 2015-10-07 07:07
    关注

    you are using prepare but not actualy preparing your query.In using prepare you must use placehoders in your query, you might need to use something like this

     $updateQuery = "UPDATE products SET pro_name = :proname
        WHERE pro_id = :pro_id";
        $query = $conn->prepare($updateQuery);
        $query->execute(array(':pro_update'=>$pro_update,':pro_id'=>$pro_id)); 
    

    since there are two types of place holders the named and the positional placehodler(?) . You may also use something like this

     $updateQuery = "UPDATE products SET pro_name = ?
            WHERE pro_id = ?";
            $query = $conn->prepare($updateQuery);
            $query->execute(array($pro_update,$pro_id)); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度