1

I have a POST variable that is used to get data from my MySQL, but it's not working, there's something wrong with my POST data.

I have tried to escape a variable put in the $_POST variable using {$_POST[$var]} ,but the main problem is that I cannot put the {$_POST[$var]} in the SQL statement. Here is my code:

 for($i = 0; $i < mysqli_num_rows($result); $i++){
 $sqlx = mysqli_query($conn, "UPDATE manager_details
 SET article = '{$_POST[input_article$i]}' WHERE field_id ='1'"));  
 }

In this case, the SQL statement isn't running.

And here is my my html form :

        $result = mysqli_query($conn, "SELECT field_name FROM 
        manager_master");
        while($row = mysqli_fetch_array($result)){
        $fieldname[] = $row["field_name"];   
        }  
        echo $fieldname[1];
        for($i = 0; $i < mysqli_num_rows($result); $i++){
        echo "<tr><td>$fieldname[$i]</td><td><form action='update.php' 
        method='post'><textarea name='input_$fieldname[$i]'></textarea></td>
        <tr>";
        }
//</form> outside the loop
Martin
  • 22,212
  • 11
  • 70
  • 132
Explosion
  • 65
  • 1
  • 3
  • 12
  • try with this $_POST['input_article'.$i] – Priyank Aug 23 '17 at 10:19
  • @Priyank nope not working and I want the iterator included in the name not outside it. – Explosion Aug 23 '17 at 10:21
  • if you are "listing" `input_article` values you should instead be using `POST arrays` such as `$_POST['input_article'][$i]` Where `$i` is one of multiple array values in the `input_article` container. – Martin Aug 23 '17 at 10:21
  • 1
    *(You will need to [slightly reformat](https://stackoverflow.com/questions/9073690/post-an-array-from-an-html-form-without-javascript) your HTML form submission as well to fit this better data structure)* – Martin Aug 23 '17 at 10:23
  • @Martin should I post my html for you to show me? – Explosion Aug 23 '17 at 10:24
  • I have added a link to my secondary comment which should show you how to shape your POST inputs. – Martin Aug 23 '17 at 10:24
  • @Martin Oh, ok thanks – Explosion Aug 23 '17 at 10:29
  • I thought this question was a ***duplicate*** and not ***off-topic*** , however I can not find anything this is a duplicate of.... so instead this question should be reopened, answered and preserved for future duplication tags on the basis of ***How to reference an Array Key with a Variable*** (as that's what it is) – Martin Aug 23 '17 at 10:34
  • 1
    @Martin I'd suggest that you consider reworking the title and the question to better focus the question. – DavidRR Aug 23 '17 at 12:57
  • @DavidRR good idea. Done. – Martin Aug 23 '17 at 13:57
  • This is not a useful question as the data structure should be changed (as Martin suggested). More importantly, **prepared statements with placeholders** is the recommended technique while using `$_POST`ed values in queries. – mickmackusa Aug 24 '17 at 04:46

0 Answers0