我有这段代码可以选择数据库中的 2 行(固定)并将其输出为具有隐藏输入的 html 形式
<?php
$sql = ("SELECT * FROM `prices`");
$query = mysql_query($sql);
while ($rows = mysql_fetch_array($query)) {
?>
<form method="post" action="process/save.php">
<input type="hidden" value="<?php echo $rows['Id']; ?>" id="offerid" name="offerid[]" />
<input type="hidden" value="<?php echo $rows['price']; ?>" id="offerprice" name="offerprice" />
<input type="hidden" value="<?php echo $rows['service']; ?>" id="offerservice" name="offerservice" />
<?php }; ?>
<input type="email" value="" id="customeremail" name="customeremail" placeholder="Enter Your Email Address" />
<input type="submit" class="button" value="Save Prices" >
</form>
save.php 文件是
<?php include("database.php");
$customeremail = mysql_real_escape_string($_POST['offerid']);
$offerprice = mysql_real_escape_string($_POST['offerprice']);
$offerservice = mysql_real_escape_string($_POST['offerservice']);
if(isset($_POST['submit'])){
$sql="INSERT INTO Save_Quote(Email, Price1, Service1, Price2, Service2,) VALUES ('// what comes here')";
$result="mysql_query($sql) or die (mysql_error())";
}
?>
如何插入值?
任何帮助将不胜感激,谢谢。
最佳答案
首先,您必须修复生成表单的代码:
<?php
$sql = ("SELECT * FROM `prices`");
$query = mysql_query($sql);
$counter = 1;
echo '<form method="post" action="process/save.php">';
while ($rows = mysql_fetch_array($query)) {
echo '<input type="hidden" value="'.$rows['Id'].'" id="offerid'.$counter.'" name="offerid'.$counter.'" />
<input type="hidden" value="'.$rows['price'].'" id="offerprice'.$counter.'" name="offerprice'.$counter.'" />
<input type="hidden" value="'.$rows['service'].'" id="offerservice'.$counter.'" name="offerservice'.$counter.'" />';
$counter ++;
};
echo '<input type="email" value="" id="customeremail" name="customeremail" placeholder="Enter Your Email Address" />
<input type="submit" class="button" value="Save Prices" >
</form>';
?>
然后就可以获取save.php文件中的变量了:
$offerprice1 = mysql_real_escape_string($_POST['offerprice1']);
$offerservice1 = mysql_real_escape_string($_POST['offerservice1']);
$offerprice2 = mysql_real_escape_string($_POST['offerprice2']);
$offerservice2 = mysql_real_escape_string($_POST['offerservice2']);
$customeremail = mysql_real_escape_string($_POST['customeremail']);
最后,查询:
$sql="INSERT INTO Save_Quote(Email, Price1, Service1, Price2, Service2) VALUES ('$customeremail','$offerprice1','$offerservice1','$offerprice2','$offerservice2')";
关于php - mysql在单行中插入php while循环数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27893522/