0

I have a form to update multiple dynamically generated input fields from a MySQL database table.

The following generates the inputs:

  <?php 
while ($row_exon = mysql_fetch_assoc($result_exon)){
    echo "<input name='exon_start[]' type='text' id='". $row_exon['exon_pk'] . "'  value='" . $row_exon['start'] . "' />";
} ?>

How can I modify the foreach loop to update each input field using $row_exon['exon_pk'] in the WHERE clause?

foreach($_POST['exon_start'] as $start){    
$query_exon = "UPDATE exon SET start = '$start' WHERE exon_pk = ''";
$result_exon = mysql_query($query_exon, $connection) or die(mysql_error());
}
0

2 Answers 2

1

This works...

 <?php 
while ($row_exon = mysql_fetch_assoc($result_exon)){
    echo "<input name='exon_start[". $row_exon['exon_pk'] . "]' type='text' value='" . $row_exon['start'] . "' />";
} ?>

and with the loop:

foreach($_POST['exon_start'] as $key => $value){    
$query_exon = "UPDATE exon SET start = '$value' WHERE exon_pk = '$key'";
$result_exon = mysql_query($query_exon, $connection) or die(mysql_error());
Sign up to request clarification or add additional context in comments.

2 Comments

Yep, they are unique and it works. Thanks for your answer though ;->
No worries, your method is preferable if they are unique so you don't have to concatenate any data.
0

You'll need to pass that value through the form as well (presuming this is a POST form). Now when you POST variables, only the name and value of each field will be passed, so in your case:

// pseudo/example
exon_start[] => each_value (start)

So you need exon_pk and start, you'll have to concatenate them together to pass through as the value. I'm going to use | as a delimiter, you can use whatever you like but make sure it will never be in either of those variables:

while ($row_exon = mysql_fetch_assoc($result_exon)){
    echo "<input name='exon_start[]' type='text' id='". $row_exon['exon_pk'] . "'  value='" . $row_exon['exon_pk'] . '|' . $row_exon['start'] . "' />";
}

Now when you post through, your data will be:

// pseudo/example
exon_start[] => each_value (exon_pk|start)

So you can split it to get each variable:

foreach($_POST['exon_start'] as $each_var){    
    list($exon_pk, $start) = explode('|', $each_var);
    $query_exon = "UPDATE exon SET start = '$start' WHERE exon_pk = '$exon_pk'";
    $result_exon = mysql_query($query_exon, $connection) or die(mysql_error());
}

Side note: for the record, mysql_* extension has been deprecated for years and will probably be removed completely in the future. You should start using mysqli_* or PDO extensions instead.

Also, this is not an ideal thing to do (in my opinion) because you are losing the roughly structured data you had by joining things together with delimiters, and as I said, if you have your delimiter in either of those variables at any point - by by working script.

Docs:

Comments

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.