-3

I am trying to pass 2 arrays in a MYSQL table using HTML array, I want to insert both values in the same row at the same time of the loop, of course nested loop isn't going to work, the first input value passed successfully, but the second input inserts wrong & unrelated values. I am sure it's because the for loop logic is incomplete, but I can't seem to adjust properly. any help will be appreciated.

HTML (...html code inside PHP then this, $row['id'] is the value that shall be passed to POST):

<input type="text" id="mytextbox" name="comment[]" placeholder = "Add your comments here" required>
<input type="text" id="mytextbox" list="decision[]" name="decision" placeholder = "Choose your decision" required>
<datalist id="decision[]">';
echo '<option value="'.htmlspecialchars($row['id']).'">'.htmlspecialchars($row['name']).'</option>';

<input type="text" id="mytextbox" name="comment[]" placeholder = "Add your comments here" required>
<input type="text" id="mytextbox" list="decision[]" name="decision" placeholder = "Choose your decision" required>
<datalist id="decision[]">';
    echo '<option value="'.htmlspecialchars($row['id']).'">'.htmlspecialchars($row['name']).'</option>';
   
<input type="text" id="mytextbox" name="comment[]" placeholder = "Add your comments here" required>
<input type="text" id="mytextbox" list="decision[]" name="decision" placeholder = "Choose your decision" required>
<datalist id="decision[]">';
    echo '<option value="'.htmlspecialchars($row['id']).'">'.htmlspecialchars($row['name']).'</option>';

PHP (after successful POST of $comment as input(1) & $decision as input(2) and working queries):

for ($i=0;$i<count($comment);$i++){
  $query = "INSERT INTO table (otherid,col1,col2) VALUES ('$otherid','$comment[$i]','$decision[$i]')";
  $result = $dbc->query($query);
}
Samy
  • 63
  • 7
  • 3
    Your code is open to [SQL injection](https://stackoverflow.com/questions/601300/what-is-sql-injection) – DarkBee Nov 24 '22 at 12:10
  • yes I know, thanks for the hint though. I am developing the core first before working on securing it. – Samy Nov 24 '22 at 12:12
  • And you are duplicating IDs (so the connection between the decision input field and the datalist should not work properly here to begin with.) But other than that, I can't see anything actually wrong here, at least not regarding your loop. Can you do a var_dump or print_r of both $comment and $decision please, and show us the result? – CBroe Nov 24 '22 at 12:12
  • @CBroe exactly, I am passing the same ID as foreign key into another table, so 3 records must carry same FK, 3 same IDs, 3 different comments, 3 decisions. the comments results are passed as strings correctly into its column, the decisions' IDs i.e 21,9,5, gets passed as 1,6,0 into col2.. no matter how I change my decision choices, it gets passed at 1,6,0 i don't know why – Samy Nov 24 '22 at 12:18
  • 4
    I am not talking about your database records IDs, I am talking about `id="mytextbox"` and `id="decision[]"` in your HTML. – CBroe Nov 24 '22 at 12:35
  • 1
    And please show us the result of the debug outputs I asked you for. – CBroe Nov 24 '22 at 12:36
  • @CBroe the $comment value in debugging shows correct value, now the $decision post shows only the last passed record in the third datalist. I think this has to do with your explanation of same datalist IDs, but how could I make it understand to record all datalist input values in one array? – Samy Nov 24 '22 at 12:51
  • @CBroe $comment prints ==> Array ( [0] => ok1 [1] => ok2 [2] => ok3 ) //ok1 first comment, ok2 second comment, ok3 third comment $decision prints ==> 5 //5 is the last choice – Samy Nov 24 '22 at 13:04
  • 2
    You are getting only the last decision, because you did not use square brackets in the field name, as you did with the comments field. `name="decision"` needs to be `name="decision[]"`. The duplicate IDs are only of client-side importance - selecting from those lists, will likely not populate the correct input field, but it has little to do with what actually gets submitted, if you filled those fields by hand. – CBroe Nov 24 '22 at 13:16
  • @CBroe I don't know what to say. problem solved. it was a stupid mistake I didn't come to notice at all. appreciating you taking the time to help, man. about the IDs, yes, they are like this because of styling purposes later on, and I will look for a way to prevent inputs from outside the list. thanks again! if you would comment as answer I will mark it for you. – Samy Nov 24 '22 at 13:28

1 Answers1

2

You are getting only the last decision, because you did not use square brackets in the field name, as you did with the comments field. name="decision" needs to be name="decision[]". Only then will PHP create an array out of multiple passed parameters of the same name; without square brackets, they simply overwrite each other.


The duplicate IDs are only of client-side importance - selecting from those lists, will likely not populate the correct input field, but it has little to do with what actually gets submitted, if you filled those fields by hand. But you should be able to make thos IDs dynamic, for example by appending the row ID.

<datalist id="decision-123">, with a matching list="decision-123" on the input field.

CBroe
  • 91,630
  • 14
  • 92
  • 150