简体   繁体   中英

edit mysql table with php

I am missing something and I need some help!

The below code generates a table with editable fields. When I edit the fields it passes that data to my edit page which will run an UPDATE script. But when I hit the Update button on any row it sends every record in the while loop. How do I limit that to just the record I have selected to update?

Thanks for your help!

$sitequery = "SELECT * FROM meterdata WHERE address LIKE '%$site%' ORDER BY apart_num + 0";

$siteresult = mysqli_query($connection,$sitequery);
    echo "<form action='editmeter.php' method='get'>";
$resultstring = '<table class="table table-hover">';
$resultstring .= '<tr>';
$resultstring .= '<th>Apartment</th>';
$resultstring .= '<th>Address</th>';
$resultstring .= '<th>Suburb</th>';
$resultstring .= '<th>Meter</th>';
$resultstring .= '<th>Meter Loction</th>';
$resultstring .= '<th>Type</th>';
$resultstring .= '<th>Units</th>';
$resultstring .= '<th>Start Read</th>';
$resultstring .= '<th>Read Date</th>';
$resultstring .= '<th>Waveflow</th>';
$resultstring .= '<th>Status</th>';
$resultstring .= '<th>Notes</th>';
$resultstring .= '</tr>';

while($data = mysqli_fetch_assoc($siteresult)){

    $resultstring .= "<tr>";
    $resultstring .= "<td><input type='text' value='$data[apart_num]' name='apart' name='apart' </td>";
    $resultstring .= "<td>$data[address]</td>";
    $resultstring .= "<td>$data[suburb]</td>";
    $resultstring .= "<td><input type='text' value='$data[meter_serial]' name='serial' ></td>";
    $resultstring .= "<td>$data[meter_location]</td>";
    $resultstring .= "<td><input type='text' value='$data[meter_type]' name='type'></td>";
    $resultstring .= "<td><input type='text' value='$data[meter_units]' name='unit'></td>";
    $resultstring .= "<td><input type='text' value='$data[start_read]' name='read'></td>";
    $resultstring .= "<td>$data[start_read_date]</td>";
    $resultstring .= "<td>$data[node_address]</td>";
    $resultstring .= "<td>$data[status]</td>";
    $resultstring .= "<td>$data[notes]</td>";
    $resultstring .= "<td><input type='submit' value='Update'></td>";
    echo"</from>";
    $resultstring .= "</tr>";

    }



$resultstring .= '</table>';


echo $resultstring;

我建议您添加一个具有ID值的隐藏字段,该字段可以标识要传递给更新的唯一项,然后在更新查询中设置WHERE item_id = $ item_id。

You are facing this problem because you are updating every record in your table. To update a selected row you need to pass an unique id which you can do by giving a hidden field in the form. You must pull that unique column say "id" from your table. It must be something like this:

while($data = mysqli_fetch_assoc($siteresult)){

$resultstring .= "<tr>";
$resultstring .= "<td><input type="hidden" name="id" value="'.$data['id'].'" />
<input type='text' value='$data[apart_num]' name='apart' name='apart' </td>";
$resultstring .= "<td>$data[address]</td>";
........
}

Now accordingly your update script must be like this:

$query = "UPDATE your_table SET apart='$apart', address='$address' WHERE id = '$id'";

You may add as many as columns here but make sure to use the WHERE clause.

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