简体   繁体   中英

Php update mysql loop data

I would like to update looping data when user submit.

Here is the table, called "T1":

+-------+------------+----------------+-----------------+-----------+
| id    | areacode   | type           | price           | time      |
+-------+------------+----------------+-----------------+-----------+
| 1     | 1          | 1              | 100000          | 12345678  |
| 2     | 1          | 1              | 100000          | 12345678  |
| 3     | 1          | 1              | 100000          | 12345678  |
| 4     | 1          | 1              | 100000          | 44444444  |
+-------------------------------------------------------------------+

The id is Auto_Increase in T1.

I loop my data like this:

$query = DB::query("SELECT * FROM ".DB::table('land')." WHERE areacode = '$areaid'");
while($row = DB::fetch($query)) {
    $i = $i+1;
    $areacode = $row['areacode '];
    $time = date("Y-m-d H:i",($row['time']));
    $type = $row['type'];
    $price = $row['price'];
    $rowid = $row['id'];
    if ($type == 1){
        $valuetype = '<label><input name="radio['.$i.']" type="radio" id="radio" value="1" checked="checked" />
  normaltype</label>
  <label><input type="radio" name="radio['.$i.']" id="radio" value="2" />hightype</label>';
    }else{
        $valuetype = '<label><input type="radio" name="radio['.$i.']" id="radio" value="1" />normaltype</label>
  <label><input name="radio['.$i.']" type="radio" id="radio" value="2" checked="checked" />hightype</label>';
    }
    $sqlsearch .= '<tr><td>'.$rowid.'</td>
    <td>'.$areaname.'</td>
    <td>'.$valuetype.'</td>
    <td>'.$price.'</td>
    <td>'.$rowbuytimes.'</td>
    </tr>'; 
    }
    include template('admincp_area');
    if($_GET['mod'] =='updatearealand'){
        //how to update the mysql when user submit the loop data
    }

My template coding is:

<form method="post" autocomplete="off"  name="paynameform" action="plugin.php?id=admincp&do=area&areaid=1&mod=updatearealand" enctype="multipart/form-data" ><table width="100%" border="1" cellspacing="0" cellpadding="5">$sqlsearch</table><input name="" type="button" /></form>

I can looping the above data, but I don't know how to update the looping data when user submit the form. Part of data user can edit is the radio .

Please help me to looking on this matter, thank you.

You already have name="radio['.$i.']" , so the result is an array. That's good, so you can separate them. In order to match them to database entries you can change that to use the row id instead of $i :

<label><input name="radio['.$rowid.']" type="radio" value="1" />normaltype</label>

Now, after submitting the form, you'll get an array you can loop through like this:

foreach ($_POST['radio'] as $rowid => $value) {
  DB::query("UPDATE ".DB::table('land')." SET type=$value WHERE id='$rowid'");
}

I don't know what DB framework you are using, so you should adapt it to prevent SQL injections.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM