获取多个数据并将其添加到多个数据库条目中


Getting multiple data and them adding them into multiple database entries

Hi制作了一个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>";
      }
      ?>

谢谢,如果有人能帮我做这件事,我真的很感激,我只是编码方面的一个小人物,所以很抱歉

这是因为您在while循环之外执行查询。解决方案是在while循环中移动mysql_query($sq, $cn)。此外,您的插入查询具有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_eescape_string,如下所示:

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

还要注意,mysql_扩展已被弃用,因此您应该研究mysqli或PDO。