PHP从html表格向mysql插入可变数量的记录到mysql [英] PHP INSERT a variable number of records to mysql from a html form
问题描述
我有一个HTML表单,该表单检索用户输入库存数字的不同数量的产品类型.然后需要将该数据插入到新表中. 这是填充表单的PHP查询.
I have a HTML form that retrieves a varying number product types that the user inputs stock figures. This data then needs to be INSERTED to a new table. Here is the PHP query that populates the form.
require_once 'config.php';
$i = 1;
$sql = "SELECT * FROM dealer_product WHERE customer_code='$custcode' ORDER BY prod_code";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$prodcode = $row['prod_code'];
echo "<tr><td><input type='text' name='prod".($i++)."' value='" . $prodcode . "'/></td><td><input type='number' name='openstock".($i++)."'/></td><td><input type='number' name='sold".($i++)."'/></td></tr>";
}
mysql_close($con);
?>
我知道如何插入一定数量的多个记录,但是如何插入不同数量的记录? 提前致谢.对不起,我的基本知识是我是网络管理员,而不是PHP MYSQL.
I know how to INSERT a set number of multiple records, but how do I INSERT a varying number of records? Thanks in advance. Sorry for my basic knowledge, I'm a network admin not PHP MYSQL.
推荐答案
我建议您以相同的方式使用
I recommend that you go in the same way but using
同时($ row = mysql_fetch_assoc($ result))
while ($row = mysql_fetch_assoc($result))
现在您没有数字作为索引,这是查看事物的更复杂方法,最好是从mysql中表的列名称中找到关联方法.
And now you have not numbers as index that is more complicated way to see things, better see associative way that's the column name from your table in mysql.
您在$ i ++上执行了很多次递增操作.我不知道您是否故意这样做,但是否只是一次增加$ i.
And you're doing a lot of increments there doing $i++ a lot of times. I don't know if you're doing that intentionally but if not just increment $i once.
也可以使用以下方法达到行数:
Also the number of row can be reached using this:
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
查看手册
http://us1.php.net/mysql_fetch_assoc
这篇关于PHP从html表格向mysql插入可变数量的记录到mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!