我正在制作一个应用程序,只需更新数据库中的一行。我构建应用程序的方式是更新数据库中的所有行。有人可以查看我的脚本并告诉我如何更新一行吗?
我知道我需要用id做一些事情,但我不知道怎么做..
以下是用户可以从数据库更改数据的页面脚本。这样每条记录都会更新。
<section>
<?php
$sql = "SELECT naam, plaats.plaats, type.type, status FROM devices, type, plaats, status
WHERE devices.type_id = type.type_id
AND plaats.plaats_id = devices.plaats_id
AND status.status_id = devices.status_id
AND plaats.plaats ='benedenverdieping'";
$result = $con->query($sql);
if ($result->num_rows > 0) {
echo '<table><tr><td>Naam</td><td>Plaats</td><td>Type</td><td>Status</td><td>open/dicht</td><td>Submit</td></tr>';
while($row = $result->fetch_assoc()) {
echo '<tr>';
echo '<td>' .$row['naam']. '</td>';
echo '<td>' .$row['plaats']. '</td>';
echo '<td>' .$row['type']. '</td>';
echo '<td>' .$row['status']. '</td>';
echo '<td>';?>
<form action="opendicht.php" method="post">
<select name="opendicht">
<option name="open" value="1">open</option>
<option name="dicht" value="2">dicht</option>
</select>
<?php '</td>';
echo '<td>'?>
<input type="submit" value="Doorvoeren" name="submit">
</form>
<?php '</td>';
}
echo '</table>';
}
?>
</section>
这是执行用户输入的脚本。由于sql查询,它不适用于只有一条记录。它现在的工作方式是查询使用status_id 1或2更新每条记录。在数据库中,1代表open,2代表closed。我找不到让查询只更新一行数据的方法。
<?php
session_start();
$naam = $_SESSION['username'];
echo $naam;
include '../dbconnect.php';
date_default_timezone_set("Europe/Amsterdam");
$t=time();
$aanpassing = date("d-m-y h:i",$t);
$sel_device_id = ("SELECT device_id FROM devices");
$run_device_id = mysqli_query($con, $sel_device_id);
if(isset($_POST['submit']))
{ // Fetching variables of the form which travels in URL
$opendicht = $_POST['opendicht'];
if($opendicht =='1')
{
//UPDATE devices SET status_id = $opendicht, username = (SELECT user_id FROM users WHERE username = '$naam'), timestamp = '$timestamp' WHERE status_id = '2'
mysqli_query($con,"UPDATE devices SET status_id = $opendicht, username = '$naam', tijd = '$aanpassing' WHERE status_id = 2");
}
else
{
mysqli_query($con,"UPDATE devices SET status_id = $opendicht, username = '$naam', tijd = '$aanpassing' WHERE status_id = 1");
}
}
else{
echo "er is iets fout gegaan";
}
mysqli_close($con); // Closing Connection with Server
header( "refresh:0;url=index.php" );
?>
答案 0 :(得分:1)
这些是您的UPDATE
查询:
mysqli_query($con,"UPDATE devices SET status_id = $opendicht, username = '$naam', tijd = '$aanpassing' WHERE status_id = 1");
mysqli_query($con,"UPDATE devices SET status_id = $opendicht, username = '$naam', tijd = '$aanpassing' WHERE status_id = 2");
您正在更新status_id
为1
或2
的所有行。
您需要添加AND
条件以标识要更新的唯一行标识符。