mysql插入php而单行循环数据

时间:2015-01-11 23:55:58

标签: 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())";
}
?>

我如何插入值?

非常感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

首先,您必须修复生成表单的代码:

<?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')";