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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。