将mysqlwhile循环数据存入数组再存入数据库
Save mysql while loop data into arrays and then save into database
首先我的 apopoliges ,这可能是一个可能重复的问题,我搜索并发现很少有人回答,但它们对我的情况不是很有帮助。
我有以下带有静态和动态值的表格
<input type="hidden" value="<?php echo $Quote ?>" id="amount" name="amount"/>
<input type="hidden" value="<?php echo $NoDays?>" id="total_days" name="total_days"/>
<?php $sql = mysql_query("select * from tbloffers ORDER BY Sequence"); ?>
<?php while($rows = mysql_fetch_array($sql)) { ?>
<input type="hidden" value="<?php echo $rows['Id']; ?>" name="offerid[]" />
<input type="hidden" value="<?php echo $rows['Name']; ?>" name="offername[]" />
<input type="hidden" value="<?php echo $rows['Price']; ?>" name="offercharges[]" />
<?php } ?>
<input type="email" id="customeremail" name="customeremail" required/>
我的 save.php 文件是
$date = date("j M, Y");
$total_days = mysql_real_escape_string($_POST["total_days"]);
$amount = mysql_real_escape_string($_POST["amount"]);
$customeremail = mysql_real_escape_string($_POST["customeremail"]);
$offerid = $_POST["offerid"];
$offername = $_POST["offername"];
$offercharges = $_POST["offercharges"];
当我点击提交时,我希望能够通过循环将所有这些值插入到数据库中。我尝试了 foreach 并挣扎。
注意:静态值将与动态值重复。
例如 $date,$amount 在插入时将保持不变。
$sql = "INSERT INTO table < What comes next?
提前致谢。
您可以使用 foreach
循环遍历其中一个重复输入,然后使用索引访问其他输入中的相应元素:
foreach ($offerid as $i => $offer) {
$offer = mysql_real_escape_string($offer);
$name = mysql_real_escape_string($offername[$i]);
$charges = mysql_real_escape_string($offercharges[$i]);
mysql_query("INSERT INTO table (date, total_days, amount, customer_email, offerid, offername, offfercharges)
VALUES ('$date', '$total_days', '$amount', '$customeremail', '$offer', '$name', $charges')");
}
P.S。你真的应该停止使用 mysql 扩展并升级到 mysqli 或 PDO,这样你就可以使用准备好的查询而不是字符串替换。
首先我的 apopoliges ,这可能是一个可能重复的问题,我搜索并发现很少有人回答,但它们对我的情况不是很有帮助。
我有以下带有静态和动态值的表格
<input type="hidden" value="<?php echo $Quote ?>" id="amount" name="amount"/>
<input type="hidden" value="<?php echo $NoDays?>" id="total_days" name="total_days"/>
<?php $sql = mysql_query("select * from tbloffers ORDER BY Sequence"); ?>
<?php while($rows = mysql_fetch_array($sql)) { ?>
<input type="hidden" value="<?php echo $rows['Id']; ?>" name="offerid[]" />
<input type="hidden" value="<?php echo $rows['Name']; ?>" name="offername[]" />
<input type="hidden" value="<?php echo $rows['Price']; ?>" name="offercharges[]" />
<?php } ?>
<input type="email" id="customeremail" name="customeremail" required/>
我的 save.php 文件是
$date = date("j M, Y");
$total_days = mysql_real_escape_string($_POST["total_days"]);
$amount = mysql_real_escape_string($_POST["amount"]);
$customeremail = mysql_real_escape_string($_POST["customeremail"]);
$offerid = $_POST["offerid"];
$offername = $_POST["offername"];
$offercharges = $_POST["offercharges"];
当我点击提交时,我希望能够通过循环将所有这些值插入到数据库中。我尝试了 foreach 并挣扎。
注意:静态值将与动态值重复。 例如 $date,$amount 在插入时将保持不变。
$sql = "INSERT INTO table < What comes next?
提前致谢。
您可以使用 foreach
循环遍历其中一个重复输入,然后使用索引访问其他输入中的相应元素:
foreach ($offerid as $i => $offer) {
$offer = mysql_real_escape_string($offer);
$name = mysql_real_escape_string($offername[$i]);
$charges = mysql_real_escape_string($offercharges[$i]);
mysql_query("INSERT INTO table (date, total_days, amount, customer_email, offerid, offername, offfercharges)
VALUES ('$date', '$total_days', '$amount', '$customeremail', '$offer', '$name', $charges')");
}
P.S。你真的应该停止使用 mysql 扩展并升级到 mysqli 或 PDO,这样你就可以使用准备好的查询而不是字符串替换。