1

I have a MySql table:

id   concept
-------------
1    item1
2    item2
3    item3

I try to generate this table dynamically in the hmtl (I am not sure if I did it well):

<form action="extern.php" method="post">
<?php  
$result = mysqli_query($con, 'SELECT * FROM MyTable');
while ($row = mysqli_fetch_array($result)) {
?>

<input type="text" name="<?php echo $row["concept"] ?>"
value="<?php echo $row["concept"] ?>"><br>

<?php 
}
?>
<input type="submit">
</form>

This gives in the source of the html:

<form action="extern.php" method="post">
    <input type="text" name="item1" value="item1"><br>
    <input type="text" name="item2" value="item2"><br>
    <input type="text" name="item3" value="item3"><br>
    <input type="submit" name="send">
</form>

Then I put the php in the extern.php. And here comes my question:
How can I know the id of each input to match the concept in the update?

$id = $_POST["something"];  // how should I do this var?
$concept = $_POST["something"]; // how should I do this var?

mysqli_query($con,"UPDATE MyTable 
                   SET concept = '$concept'
                   WHERE id = $id");

(I am sorry if this question is too basic. I really have tried a lot of things and look a lot of places. I am learning. Any help would be appreciated)

Nrc
  • 9,577
  • 17
  • 67
  • 114
  • **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Jul 22 '14 at 14:49

2 Answers2

0

name of the input field will be combination of name and id like concept_id

for example

 <form action="" method="post">
     <input type="text" name="item1_21" value="item1"><br>
     <input type="text" name="item2_22" value="item2"><br>
     <input type="text" name="item3_23" value="item3"><br>
     <input type="submit" name="send">
 </form>

then on extern.php explode post key by underscore

foreach($_POST as $key => $value){        
  if(preg_match('/[_]/i', $key )){
     $postArr = explode('_', $key);
  }
}

when you print $postArr you will get result something like below

 array (size=2)
  { 0 => string 'item1' (length=5)
    1=> string '21'
  }

now you have both the values to update

 $concept = $postArr[0] 
 $id = $postArr[1] 

 mysqli_query($con,"UPDATE MyTable 
               SET concept = '$concept'
               WHERE id = $id");
Sangita Kendre
  • 429
  • 4
  • 11
0

You can't find the names by looping over the keys for the array… but don't.

You are using the same value for the name and the value, so give them all the same name, and then loop over that.

<input type="text" name="item[]" value="<?php echo htmlspecialchars($row["concept"]); ?>">

(The [] is due to a quirk of PHP that requires duplicate names to end in [] before it will inflate to an array instead of dropping all but one of them)

Then you can loop over:

foreach ($_POST['item'] as $item) {
     # Do something with $item
}
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I do not understand your answer. If I copy paste your code and echo $item; gives me item1item2item3 I do not see why it is useful? In other words, in my example what would be $id and $concept ? – Nrc Jul 23 '14 at 07:45
  • item1 et al are your concept and your ID (they are the same in the HTML you were writing in your question). – Quentin Jul 23 '14 at 09:01
  • Sorry, I do not understand how to get the id from your code – Nrc Jul 26 '14 at 14:30