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 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)
  • ¥15 服务端控制goose报文控制块的发布问题
  • ¥15 学习指导与未来导向啊
  • ¥15 求多普勒频移瞬时表达式
  • ¥15 如果要做一个老年人平板有哪些需求
  • ¥15 k8s生产配置推荐配置及部署方案
  • ¥15 matlab提取运动物体的坐标
  • ¥15 人大金仓下载,有人知道怎么解决吗