dounuo9921 2017-02-10 19:59
浏览 74
已采纳

当我将表单上的提交按到我的数据库时,说无法连接

I have been trying to create a form that updates to my database but... when I click "update" just says could not connect !!! WHY??? I know it hasn't updated because when I go on to my database nothing has changed... :-( .

<html>

   <head>
      <title></title>
   </head>

   <body>
      <?php

         if(isset($_POST['update'])) {


            $dbhost = 'localhost';
            $dbuser = '***';
            $dbpass = '******';

            $conn = mysqli_connect($dbhost, $dbuser, $dbpass);

            if(! $conn ) {
               die('Could not connect: ' . mysqli_error());
            }

            $Userid = $_POST['UserID'];
            $TableID = $_POST['tableID'];
            $Life_points = $_POST['Life_points'];
            $xp_points = $_POST['xp_points'];

            $sql = "UPDATE points"." SET TableID = $TableID". "WHERE UserID=  $UserID"."WHERE life_points= $Life_points"."WHERE  xp_points= $xp_points" ;

mysqli_select_db('womath');

            $retval = mysqli_query( $sql, $conn );

            if(! $retval ) {
               die('Could not update data: ' . mysqli_error());
            }
            echo "Updated data successfully
";

            mysqli_close($conn);
         }
         else {


            ?>
               <form method = "post" action = "<?php $_PHP_SELF ?>">
                  <table width = "400" border =" 0" cellspacing = "1" 
                     cellpadding = "2">

                     <tr>
                        <td width = "100">UserID</td>
                        <td><input name = "UserID" type = "int" 
                           id = "UserID"></td>
                     </tr>
                  <tr>
                     <td width = "100">TableID</td>
                     <td><input name = "TableID" type = "int" 
                        id = "TableID"></td>
                  </tr>
                  <tr>
                     <td width = "100">life_points</td>
                     <td><input name = "life_points" type = "int" 
                        id = "life_points"></td>
                  </tr>

                     <tr>
                        <td width = "100">xp_points</td>
                        <td><input name = "xp_points" type = "int" 
                           id = "xp_points"></td>
                     </tr>

                     <tr>
                        <td width = "100"> </td>
                        <td> </td>
                     </tr>

                     <tr>
                        <td width = "100"> </td>
                        <td>
                           <input name = "update" type = "submit" 
                              id = "update" value = "Update">
                        </td>
                     </tr>

                  </table>
               </form>
               <?php
         }
      ?>

   </body>
</html>
  • 写回答

2条回答 默认 最新

  • doumibi6899 2017-02-10 20:31
    关注

    There are few things you need to change in your code, such as:

    • If you're taking only integer input for all of your <input> fields, then make the type as number instead of int.

      <input name = "UserID" type = "number" id = "UserID">
      <input name = "TableID" type = "number" id = "TableID">
      // and so on ...
      
    • There are few syntax errors in your code. Also, your SQL query is wrong. So change your code in the following way,

      if(isset($_POST['update'])) {
          // your code
          $Userid = $_POST['UserID'];
          $TableID = $_POST['TableID'];
          $Life_points = $_POST['life_points'];
          $xp_points = $_POST['xp_points'];
      
          $sql = "UPDATE points SET TableID = " . $TableID . " WHERE UserID = ". $Userid . " AND life_points = " . $Life_points . " AND xp_points= " . $xp_points;
          // your code
      }else{
          // your code
      }
      
    • mysqli_query( $sql, $conn ) should be mysqli_query( $conn, $sql ). mysqli_query() takes the first argument as your connection handler and second argument as your query string.

    • Since you're doing UPDATE operation, use mysqli_affected_rows() function to check how many rows are affected from this UPDATE operation.

    • Last but not the least, don't mix mysql and mysqli APIs. Use either mysqli or pdo. Also, learn about prepared statement since it prevents your database from any kind of SQL injection attack.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么