php - mysql在单行中插入php while循环数据

标签 php mysql

我有这段代码可以选择数据库中的 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/

相关文章:

php - 在 MySQL 中搜索字符串 WHERE string LIKE string(括在括号中)

mysql - LEFT JOIN 的效率

mysql - 插入mysql数据库,如果记录已经存在,则更新

php - 在 PHP 中将 mySQL 数据库查询转换为 JSON

php - 数据库列内的静态值

php - SOAP 客户端接收空标准类

用于将数据从一个页面传输到另一个页面的 php 代码逻辑

php - 我如何使用 php 回显从文本框表单发布的变量?

php - Apache 安装和运行 php 文件

php - 如何使用 1 个 mysql 查询执行 2 个 php while 循环?