如何从PHP中将多行数据插入数据库

时间:2016-08-18 03:41:29

标签: php mysql

我目前在PHP中创建一个表,显示数据库中某个表的所有数据行。之后,我想将PHP表中的所有行保存到新的数据库表中。但问题是,保存后,只有最后一行将存储到数据库中的新表中,而早期的另一行将不会保存。有人能帮我吗。我想试试foreach但是有人可以教我。

<table border="1" align="center" cellspacing="0">
<tr>
  <td colspan="12"><div align="center">
      <h3><strong>BOOKS</strong></h3>
  </div></td>
</tr>
<tr>
  <td width="80"><div align="center">Code</div></td>
  <td width="40"><div align="center">No</div></td>
  <td width="250"><div align="center">Name</div></td>
  <td width="100"><div align="center">Publisher</div></td>
  <td width="80"><div align="center">Price</div></td>
  <td width="60"><div align="center">Quantity</div></td>
</tr>

<?php do { ?>
  <tr>
    <td><div align="center">
        <input name="student_no" type="hidden" id="student_no" value="<?php echo $row_UserDetails['student_no']; ?>" />
        <input name="book_code" type="text" id="book_code" value="<?php echo $row_book_booking_list['book_code']; ?>" size="8" readonly="readonly" />
    </div></td>
    <td><div align="center">
        <input name="book_no" type="text" id="book_no" value="<?php echo $row_book_booking_list['book_no']; ?>" size="1" readonly="readonly" />
    </div></td>
    <td><div align="center">
        <input name="book_name" type="text" id="book_name" value="<?php echo $row_book_booking_list['book_name']; ?>" size="31" readonly="readonly" />
    </div></td>
    <td><div align="center">
        <input name="book_publisher" type="text" id="book_publisher" value="<?php echo $row_book_booking_list['book_publisher']; ?>" size="10" readonly="readonly" />
    </div></td>
    <td><div align="center">
        <input name="book_price" type="text" id="book_price" value="<?php echo $row_book_booking_list['book_price']; ?>" size="7" readonly="readonly" />
    </div></td>
    <td><div align="center">
      <select name="book_quantity" id="book_quantity">
        <option value="0">0</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
      </select>
    </div></td>
  </tr>
<?php } while ($row_book_booking_list = mysql_fetch_assoc($book_booking_list)); ?>
</table>
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO book_booked_list (student_no, book_code, book_no, book_name, book_publisher, book_price, book_quantity) VALUES (%s, %s, %s, %s, %s, %s, %s)",
                   GetSQLValueString($_POST['student_no'], "int"),
                   GetSQLValueString($_POST['book_code'], "text"),
                   GetSQLValueString($_POST['book_no'], "text"),
                   GetSQLValueString($_POST['book_name'], "text"),
                   GetSQLValueString($_POST['book_publisher'], "text"),
                   GetSQLValueString($_POST['book_price'], "double"),
                   GetSQLValueString($_POST['book_quantity'], "int"));

mysql_select_db($database_book_con, $book_con);
$Result1 = mysql_query($insertSQL, $book_con) or die(mysql_error());
}

2 个答案:

答案 0 :(得分:3)

要将所有行从source_table复制到book_booked_list表,只需执行以下查询:

INSERT INTO book_booked_list 
    (student_no, book_code, book_no, book_name, book_publisher,
       book_price, book_quantity)
SELECT 
    student_no, book_code, book_no, book_name, book_publisher, 
       book_price, book_quantity
FROM source_table

source_table替换为您从中复制数据的表的名称。

答案 1 :(得分:-1)

您的DO ... WHILE循环生成具有相同名称的输入字段:所有学生编号字段都命名为“student_no”,因此只保留最后一组表单值并将其发送以进行处理。

如果要插入多行,则需要使用数组:将“student_no”替换为“student_no []”,将“book_code”替换为“book_code []”等。然后,在服务器端,循环执行数组分别插入每一行:

$stuno = $_POST['student_no'];
$bookcode = $_POST['book_code'];
.... etc...
for($i=0; $i < count($stuno); $i++){
    $sql = "INSERT INTO book_booked_list (student_no, ....) 
    VALUES ($stuno[$i])"
}

当然,有更有效的方法可以做到这一点,您应该在插入数据库之前清理表单值。这只是为了让您开始使用数组。