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

时间:2017-02-10 19:59:15

标签: php sql html5

我一直在尝试创建一个更新到我的数据库的表单但是...当我点击"更新"只是说不能连接!为什么???我知道它没有更新,因为当我进入我的数据库时没有任何改变...... :-(。

<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\n";

            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 个答案:

答案 0 :(得分:0)

它说&#34;无法更新...&#34;

因为在传递参数后你的$ sql看起来像这样:

&#34; UPDATE points SET TableID = yourTableIdHereWHERE UserID = yourUserIdHereWHERE life_points = yourLifePointsHereWHERE xp_points = yourXpPointsHere&#34;

正如您所看到的,您将参数与查询的下一部分一起运行,因为没有空格将它们分开,(哦,您还使用了WHERE关键字3次。)

答案 1 :(得分:0)

您需要在代码中更改一些内容,例如:

  • 如果您只为所有type字段提取整数输入,请将number改为int而不是<input name = "UserID" type = "number" id = "UserID"> <input name = "TableID" type = "number" id = "TableID"> // and so on ...

    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
    }
    
  • 代码中的语法错误很少。此外,您的SQL查询是错误的。因此,请按以下方式更改代码,

    mysqli_query( $sql, $conn )
  • mysqli_query( $conn, $sql )应为UPDATEmysqli_query()将第一个参数作为您的连接处理程序,将第二个参数作为查询字符串。

  • 由于您正在执行UPDATE操作,请使用mysqli_affected_rows()函数检查此mysql操作会影响的行数。

  • 最后但并非最不重要,请勿混用mysqlipublic java.lang.String toString() { #if ( $members.size() > 0 ) #set ( $i = 0 ) return "$classname{" #foreach( $member in $members ) #if ( $i == 0 ) + " ## #else + ", ## #end #if ( $member.objectArray ) #if ($java_version < 5) $member.name=" + ($member.accessor == null ? null : java.util.Arrays.asList($member.accessor)) + #else $member.name=" + java.util.Arrays.toString($member.accessor) #end #elseif ( $member.primitiveArray && $java_version >= 5) $member.name=" + java.util.Arrays.toString($member.accessor) #elseif ( $member.string ) $member.name='" + $member.accessor + '\'' #else $member.name=" + $member.accessor #end #set ( $i = $i + 1 ) #end + '}'; #else return "$classname{}"; #end } API。使用mysqlipdo。另外,了解prepared statement,因为它会阻止您的数据库遭受任何类型的SQL注入攻击。