2

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);
    }
 }
 ?>
8
  • It's most likely because the $_REQUEST[x] value doesn't exist, try something like $id = !empty($_REQUEST["Picolo4"]) ? $_REQUEST["Picolo4"] : ""; BUT please sanitize your inputs and use prepared statements. Your code is really vulnerable to SQL injections. Commented Jan 30, 2017 at 6:45
  • Still get the same error Commented Jan 30, 2017 at 6:47
  • What's the full error? It should show you what line is consing this, thus, you can check what index you are trying to read, that is not there yet. Ie, $choix = $_POST["$un"][$i] also looks suspicios Commented Jan 30, 2017 at 6:49
  • the full error : Notice: Uninitialized string offset: 1 in C:\wamp\www\Foredeck\foredeckaffiche_client.php on line 71 that mean yes the part '' $choix = $_POST["$un"][$i];" Commented Jan 30, 2017 at 6:50
  • Add check in foreach loop: if (! isset($_POST["$un"][$i])) { continue; } Commented Jan 30, 2017 at 6:52

1 Answer 1

1

$_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"];
Sign up to request clarification or add additional context in comments.

5 Comments

Ok let me explain my self , i m doing a survey that generate automaticaly a radio button set and a commentary text area if you have a question or many question in the survey . The purpose is to not need to change code everytime when you add a question to the survey but generate it . That mean I got a question then ==> generate radio button set then ==> generate text area . So that s why i m using in the same time a variable for radio button set to not get conflict beetween others radio button set .
Try to follow the update of my answer and modify your loop.
Notice: Uninitialized string offset: 1 in C:\wamp\www\Foredeck\foredeckaffiche_client.php on line 71. So i need to do a string for every values of every radio button set ?
Can you please update your new code in the question and mark which line is 71?
Check Update 1 in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.