duanjie9630 2017-01-30 06:42
浏览 92
已采纳

插入foreach循环

So i get this error :

Notice: Uninitialized string offset

I want to insert the value of the checked radio button of the radio button set at each row of my table .

That mean at every row of my table i have this Question ll Radio button set ll Commentary .

A survey form .

<?php
$link=Mysqli_connect($host,$login,$pass,$dbname);
$un = 0;
$msgerror = "Veuillez remplir tous les champs ";    
if(isset($_POST["bouton10"])) {
    $id = $_REQUEST["Picolo4"];
    $Nom = $_REQUEST["Picolo1"];
    $Prenom = $_REQUEST["Picolo2"];
    $Email = $_REQUEST["Picolo3"];

    if ($id !="" && $Nom !="" && $Prenom !="" && $Email !="") {
        $recherche= "SELECT Ref,Question,Choix,Commentara FROM questionnaire WHERE Qref ='$id'";
        mysqli_query($link,$recherche);
        $result= mysqli_query($link,$recherche);
        $num_results = $result->num_rows;

        while ($row = mysqli_fetch_assoc($result)) {
            $Ref =$row["Ref"];
            $Question =$row["Question"];
            $un++;

            echo" <tr bgcolor=\"white\">
                <td>$Question position: $un </td>
                <td>
                    <input type=\"radio\" name =\"$un\" id =\"un\" value = \"3\">
                    <input type=\"radio\" name =\"$un\" id =\"un\" value = \"2\">
                    <input type=\"radio\" name =\"$un\" id =\"un\" value = \"1\">
                    <input type=\"radio\" name =\"$un\" id =\"un\"  value = \"0\">
                    <input type=\"radio\" name =\"$un\" id =\"un\" value = \"0\">
                </td>
                <td width = \"60\"> <textarea> </textarea> </td> 
            </tr>                              
            </div>
            </div>"; 
        }
    } else {
        echo "<script type='text/javascript'>alert('$msgerror')</script>";
    }
}
//The part with the problem //
$un = 1;
if (isset($_POST["bouton11"])) {
    $i= 0 ;  
    while(isset($_POST[$un])) {
        //Line 71 //
        $choix = $_POST["$un"][$i];
        $enregistrer = "INSERT INTO questionnaire(Choix)  VALUES('$choix') ";
        $un++;
        $i++;
        mysqli_query($link, $enregistrer);
    }
 }
 ?>
  • 写回答

1条回答 默认 最新

  • dongtuojuan8998 2017-01-30 06:54
    关注

    $_POST["$un"] might be

    3,2,1,0 or 0.

    And then you're trying to access $_POST["$un"][$i] while $i is an incresing number. When you try to treat a string as an array - the [$i] would access it's characters, but your string has only 1 character and your loop keeps on running for each $_POST key exists which is more than 1 time obviously.

    For instance:

    $string = "abcd";
    echo $string[0]; // a
    echo $string[1]; // b
    echo $string[5]; // Notice:  Uninitialized string offset: 5 in ..
    

    So you will need to modify your loop. Instead of using:

    foreach ($_POST as $val){
    

    Try to use:

    while(isset($_POST[$un])){
       //your code here
       $un++; //don't forget to increase $un
    }
    

    Update 1:

    There's no reason in trying to treat $_POST[$un] as an array so replace this:

    $choix = $_POST["$un"][$i];
    

    with:

    $choix = $_POST["$un"];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?