am currently creating a table in PHP that display all row of data from one of the table in my database. After that, I want to save all row from the PHP's table into new database table. But the problem is, after save, only the last row will be stored into the new table in database, the other row from early will not be save. Can someone help me. I want to try the foreach but can someone teach me.
<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());
}
To copy all rows from source_table
to book_booked_list
table, just execute the query below:
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
Replace source_table
with the name of the table you copy data from.