1

我正在寻求一点帮助。我有一个页面供用户输入多达 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 个,文本框的名称在末尾加上 index 的值。(以我有限的编码经验,我对自己很满意)所以我转到 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");
?>

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

4

3 回答 3

3

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

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 循环之外执行该查询。

于 2013-09-12T13:43:39.123 回答
1

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

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

This is really messy too - you are creating extra work and complication for yourself. Use arrays:

name="transno[]"

If you do exlpicitly want to reference the item again then set the index:

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

And at the receiving end....use a single insert statement to add the rows - not 10 seperate ones (it will be much faster).

于 2013-09-12T13:51:01.047 回答
0

You've already set up your while loop with $index - you could simply use that to iterate through the POST values, since you set their name attribute with an index. Consider:

$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++;}

Though it would be much cleaner to set up your form inputs as arrays, as noted by others here.

Also, please read up on SQL injection. You need to sanitize any user input before it's inserted into a database - otherwise a malign user could wipe your whole database.

于 2013-09-12T13:59:54.590 回答