douji9518 2015-01-11 23:55
浏览 46
已采纳

mysql插入php而单行循环数据[关闭]

I have this code to select and output 2 rows (fixed) from database into html form with hidden input

<?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>

and the save.php file is

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

how can i insert the values?

Any help will be much appreciated, Thank You.

  • 写回答

1条回答 默认 最新

  • douyazi1129 2015-01-12 00:17
    关注

    First, you must fix the code that generate the form:

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

    And then, you can get the variables in the save.php file:

    $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']);
    

    Finally, the query:

    $sql="INSERT INTO Save_Quote(Email, Price1, Service1, Price2, Service2) VALUES ('$customeremail','$offerprice1','$offerservice1','$offerprice2','$offerservice2')";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?