0

您好已经制作了一个 php 脚本,用户可以选择或添加多个条目到数据库中,例如用户可以在 10 个区域的节目中一次添加 10 个项目,但主要问题是只有最后一个项目被添加到数据库中,而不是全部数据分成多个条目,这里是代码,它的名字是 final.php

      <?php
      if(isset($_POST['multi'])){
      echo "<form action='final.php' method='POST'>";
      $a = $_POST['number'];
      $b = 0 ;
      $c = $_POST['country'];
      $d = $_POST['state'];
      echo"<table border='1'><tr><td colspan='7'>Country:<input type='text'  
      name='country' value='$c'></td></tr>";
      while($a != $b){
      $b++;
      echo "
      <tr>
     <td>State</td>
 <td>Name</td>
     <td>Main Information</td>
     <td>Second Information</td>
      </tr>
      <tr>
     <td><input type='text' name='state".$b."' value='$d'></td>
     <td><textarea name='name".$b."'></textarea></td>
    <td><textarea name='info1".$b."'></textarea></td>
    <td><textarea name='info2".$b."'></textarea></td>
      </tr>
      ";

      };
      echo "</table></br>Total:<input type='text' name='total' value='$a'>";
      echo"<input type='submit' name='Add' value='Add'></form>";

      }

      elseif(isset($_POST['Add'])){
          $a1 = $_POST['total'];
          $b1 = 0;
          $c1 = $_POST['country'];
          echo "There is $a1 types in here";

          $cn = mysql_connect("127.0.0.1","root","");
          if(!$cn)
          {
          die('Could not connect:'.mysql_error());
          }
          mysql_select_db("test",$cn);


          while($a1 != $b1){
              $b1++;        
              $state = "state".$b1;
              $name = "name".$b1;
              $info1 = "info1".$b1;
              $info2 = "info2".$b1;

              $sq="INSERT INTO ftable(Country, State, Name, First, Second ) VALUE('$c1', '$_POST[$state]', '$_POST[$name]', '$_POST[$info1]', '$_POST[$info2]')";

              echo "</br>Country:$c1</br> 
                  State:".$_POST[$state]."</br>
                  Name:".$_POST[$name]."</br>
                  Info1:".$_POST[$info1]."</br>
                  Info2:".$_POST[$info2]."</br>";
          }


          if (!mysql_query($sq,$cn))
          {
           die('Error:'.mysql_error());
          }
          echo"<palign='center'>You have added $a to the dbms</br>";




      }

      else{
      echo"<form action='final.php' method='POST'>
      <input type='number' name='number'>
      <input type='text' name='country' value='country'>
      <input type='submit' value='Generate' name='multi'>
      </form>";
      }

      ?>

谢谢,如果有人能帮我解决这个问题,我将不胜感激,我只是编码方面的菜鸟,非常抱歉

4

1 回答 1

1

while这是因为您在循环之外执行查询。解决方案是将您移动mysql_query($sq, $cn)到 ​​while 循环中。此外,您的插入查询有VALUE代替VALUES,请参阅插入语法

       while($a1 != $b1){
          $b1++;        
          $state = "state".$b1;
          $name = "name".$b1;
          $info1 = "info1".$b1;
          $info2 = "info2".$b1;

          $sq="INSERT INTO ftable(Country, State, Name, First, Second ) VALUES('$c1', '$_POST[$state]', '$_POST[$name]', '$_POST[$info1]', '$_POST[$info2]')";

          echo "</br>Country:$c1</br> 
              State:".$_POST[$state]."</br>
              Name:".$_POST[$name]."</br>
              Info1:".$_POST[$info1]."</br>
              Info2:".$_POST[$info2]."</br>";

          if (!mysql_query($sq,$cn))
          {
            die('Error:'.mysql_error());
          }
          echo"<palign='center'>You have added $a to the dbms</br>";
       }

请注意,您的代码容易受到 SQL 注入的影响,您应该考虑使用mysql_real_escape_string,如下所示:

  $a = mysql_real_escape_string($_POST['number']);
  $c = mysql_real_escape_string($_POST['country']);
  $d = mysql_real_escape_string($_POST['state']);

另请注意,mysql_不推荐使用扩展,因此您应该查看mysqliPDO

于 2013-08-04T16:23:02.300 回答