php - 使用 php 编辑 mysql 表

标签 php mysql

我缺少一些东西,我需要一些帮助!

下面的代码生成一个具有可编辑字段的表。当我编辑字段时,它会将数据传递到我的编辑页面,该页面将运行更新脚本。但是,当我点击任何行上的“更新”按钮时,它会发送 while 循环中的每条记录。如何将其限制为仅我选择更新的记录?

感谢您的帮助!

$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。

关于php - 使用 php 编辑 mysql 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23209513/

相关文章:

php - 在 Linux 主机上,FTP 到自身(从 PHP)是否会生成出站流量?

php - 基本页脚定位问题

MySQL 选择最近的 5 个 ID

mysql - 在sql中查找具有最大连接数的2个人

mysql - 每行内嵌套表格

PHP将相同的xml对象键存储到不同的mysql表

php - fatal error : Class 'mysqli' not found in

javascript - 添加帖子并显示它而不刷新页面 Codeigniter?

PHP json_encode 函数返回空白 {}

mysql - 插入子mysql