doulu5717 2013-03-20 08:39
浏览 47
已采纳

while循环将值插入数据库? [关闭]

I currently have a form where I can subscribe and unsubscribe users from newsletters wich looks like this:

enter image description here

The code for the bottom part (the subscriptions part) is this:

 <?php 
$i = 0;  
while($objResult1 = mysql_fetch_array($objQuery1))  
 {  
$i++;  
?>  
<tr>  
   <td><div align="center"><?=$objResult1["ID"];?><input type="hidden" name="mailid" value="<?=$objResult1["ID"];?>"> </div></td>
   <td><div align="center"><?=$objResult1["Titel"];?> </div></td>  
   <td><div align="center"><input type="checkbox" name="sub" value="10"> </div></td>  
   <td><div align="center"><input type="checkbox" name="sub" value="90"> </div></td>
</tr>  
<?php  
 }  
?>  

And the code to insert the values into the database is this:

<?php
mysql_connect('localhost','root','root');
mysql_select_db('NAW') or die (mysql_error());

$Klant_ID = $_POST['klantid'];
$Mail_ID  = $_POST['mailid'];
$Status   = $_POST['sub'];
$Datum    = date("d-m-Y");


$sql = mysql_query(
    "INSERT INTO Subscriptions (
        Klant_ID,
        Mail_ID,
        Status,
        Datum
    ) VALUES (
        '".$Klant_ID."',
        '".$Mail_ID."',
        '".$Status."',
        '".$Datum."'
    )"
);

if ($sql === false) {
    die (mysql_error());
} else {
    echo 'Je gegevens zijn succesvol in de database geplaatst.<br><br>Om de gegevens uit de database te bekijken klik <A HREF="klanten.php">hier</A>. <br>Om meer gegevens in te voeren klik <A HREF="index.html">hier</A>.';
}
?>

The database is:

ID   Klant_ID  Mail_ID  Status Datum 
1     6        1               test
2     6        1        test   test
3     10       10       10     19-03-13
16    6        4        90     20-03-2013
17    6        4        10     20-03-2013
18    7        4        90     20-03-2013

Now it will just add 1 row to the database where I checked a checkbox. I want to get it working so it will add 3 rows to the database, for example: When I select the boxes like this: enter image description here

I want these rows to be added to the database:

ID  Klant_ID  Mail_ID  Status  Datum
20  6         1        10      20-03-2013
21  6         2        90      20-03-2013
22  6         4        10      20-03-2013

So my question is if this is posible using a while loop with mysql_fetch_array? I am not a pro in php cause I'm still a student and I can't solve this problem on my own. I hope my question is clear enough but if you have any question just ask in the comments^^ If anyone can show me how it is done or push me in the right direction it would be great!

(I know I shouldn't be using mysql_* anymore but that is not the point here^^)

  • 写回答

2条回答 默认 最新

  • duan6301 2013-03-20 08:49
    关注

    Change:

    <input type="checkbox" name="sub" value="10">
    <input type="hidden" name="mailid" value="<?=$objResult1["ID"];?>">
    

    to:

    <input type="checkbox" name="sub[]" value="10">
    <input type="hidden" name="mailid[]" value="<?=$objResult1["ID"];?>">
    

    and do in php:

    $sql = mysql_query("INSERT INTO Subscriptions (Klant_ID, Mail_ID, Status, Datum) VALUES   ('".$Klant_ID."', '".$Mail_ID."', '".$Status."', '".$Datum."')") or die (mysql_error());
    

    to:

    foreach($_POST['sub'] as $i=>$s){
        $sql = mysql_query("INSERT INTO Subscriptions (Klant_ID, Mail_ID, Status, Datum) VALUES   ('".$Klant_ID."', '".$_POST['mailid'][$i]."', '".$s."', '".$Datum."')") or die(mysql_error());
    }
    

    NOTE: This is solution is WITHOUT injection and other checks , and created just to show you direction to think.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了