我正在使用 PHP,需要使用 while 循环插入到 sql 中


I'm using PHP and need to Insert into sql using a while loop

我正在寻求一点帮助。我有一个页面供用户输入多达 10 行不同的信息。派单详细信息。我使用循环用我的表单创建了一个页面。

<?php
  session_start();
  require("config.php");
  require("header.php");
  $db = mysql_connect($dbhost, $dbuser, $dbpassword);
  mysql_select_db($dbdatabase, $db);    
?>
<br><br><br></br>
<form action="insertdispatch.php"  method="post">
  <body>
    <center>
      <table>
        <tr>
          <td><center><b>Ref</td>
          <td><b><center>Date</td>
          <td><b><center>Service</td>
          <td><b>   <center>Tracking</td>
        </tr>
<?php 
  $index = 1;
  $name = 1;
  while($index <= 10){
?>
         <td><input type="text" 
                   name="transno<?php echo $index;?>" 
                   id="transno<?php echo     $index;?>" />
         </td>
     <td><input type="text" name="date<?php echo $index;?>" 
               id="date<?php echo $index;?> "/>
         </td>
     <td><select name = "service<?php echo $index;?>"><?php
    $viewsql = "SELECT * FROM dispatch_service ORDER BY service ASC";
            $viewresult = mysql_query($viewsql);
            while($row = mysql_fetch_assoc($viewresult)){
        ?> <option value=<?php echo $row['service'] ;?>> 
                    <?php echo  $row['service'] ;?></option>
        <?php
            }
              echo "</select>";?>
      <td><input type="text" 
                     name="tracking<?php echo $index;?>" 
                     id="tracking<?php echo  $index;?>"/>
          </td>
      </tr>
      <?php $index ++;
    }?>
         <center>
       <td><input type="submit" value="Add Product" />
     </form>
     </center>
   </td>
 </tr>
 </table>
 </center>
 <center><a href='javascript:history.back(1);'>Back</a>
 </body>
 </html>`

我每个文本框有 10 个,文本框的名称将索引的值添加到末尾。(凭借我有限的编码经验,我对自己非常满意)所以我转到 insertdispatch.php 页面,计划是将这些值中的每一个插入到我的表中......现在。。。我不知道...而且我似乎不知道我将如何做到这一点......

我想我需要再次使用循环......但我似乎无法弄清楚我将如何调用每个 $_POST 值。我真的不想使用 10 个不同的插入语句,因为表单的大小可能会增加。这是我到目前为止所拥有的..

<?php
session_start();
require("config.php");
$db = mysql_connect("localhost","root","");
if (!$db)
{
do_error("Could not connect to the server");
}

mysql_select_db("hbt",$db)or do_error("Could not connect to the database");
$index = 1;
while($index <= 10){
$insertsql = "INSERT into dispatch (trans_no, date, service, tracking) values     ()";
mysql_query($insertsql);
 $index ++;
  }
//header("Location: " . $config_basedir . "home.php");
?>

不是在寻找任何人为我完成编码,但任何提示将不胜感激! :)

您可以构建 1 个插入多行的 insert 语句:

INSERT into dispatch (trans_no, date, service, tracking) values
   (1, '2013-09-12', 'myService1', 'on'),
   (1, '2013-09-12', 'myService2', 'on'),
   (1, '2013-09-12', 'myService3', 'on'),
   (1, '2013-09-12', 'myService4', 'on'),
   (1, '2013-09-12', 'myService5', 'on');

只需在 while 内部构建它,并在 while 完成后执行它。

要构建此查询,您需要执行与生成 HTML 时完全相同的循环,但现在只需从$_POST获取值,而不是为它们创建 html 字段......

注意 在构建 HTML 时,您将在 for 循环中触发静态查询。 由于此查询是静态的,因此结果也不会更改,最好在外部 while 循环之外执行该查询。

您已经使用 $index 设置了 while 循环 - 您可以简单地使用它来遍历 POST 值,因为您使用索引设置了它们的 name 属性。考虑:

$index = 1;
while($index <= 10){
    $trans_no = $_POST["transno$index"];
    $service = $_POST["service$index"];
    $date = $_POST["date$index"];
    $tracking = $_POST["tracking$index"];
    $insertsql = "INSERT into dispatch (trans_no, date, service, tracking)
                  VALUES($trans_no, $date, $service, $tracking)";
    mysql_query($insertsql);
 $index++;}

尽管将表单输入设置为数组会干净得多,正如此处其他人所指出的那样。

另外,请阅读SQL注入。您需要在将任何用户输入插入数据库之前对其进行清理 - 否则恶意用户可能会擦除整个数据库。

(你真的应该阅读更多关于基本HTML的内容 - 甚至在考虑PHP代码之前,那里有很多错误)。

name="transno<?php echo $index;?>"

这也真的很混乱 - 你正在为自己创造额外的工作和复杂性。使用数组:

name="transno[]"

如果您确实想再次引用该项目,请设置索引:

id="transno[<?php echo $index; ?>]"

而在接收端....使用单个 insert 语句添加行 - 而不是 10 个单独的行(它会快得多)。