【发布时间】:2022-01-23 03:44:54
【问题描述】:
所以我读过这样的表格行: Read of rows in database
当我尝试更新它时,它会更新我的所有行: This is when i click update it shows data in the row
当我更改一个数据时,例如我将名称更改为“IVICA”,所有名称都会更改。 enter image description here
<?php
include_once 'database.php';
if(count($_POST)>0) {
mysqli_query($conn,"UPDATE rezervacije set check_in='" . $_POST['check_in'] . "', check_out='" . $_POST['check_out']
. "', ime_turista='" . $_POST['ime_turista'] . "' ,prezime_turista='" . $_POST['prezime_turista']
. "' ,email_turista='" . $_POST['email_turista']
. "' ,broj_osobne='" . $_POST['broj_osobne']
. "' ,drzavljanstvo='" . $_POST['drzavljanstvo']
. "' ,god_rod_turista='" . $_POST['god_rod_turista']
. "' ,id_apartmana='" . $_POST['id_apartmana']
. "'");
$message = "Record Modified Successfully";
}
$result = mysqli_query($conn,"SELECT * FROM rezervacije WHERE id_rezervacije='" . $_GET['id_rezervacije'] . "'");
$row= mysqli_fetch_array($result);
?>
<html>
<head>
<title>Ažuriraj rezervaciju</title>
</head>
<body>
<form name="azuriraj" method="post" action="">
<div><?php if(isset($message)) { echo $message; } ?>
</div>
<div style="padding-bottom:5px;">
<a href="prikaz_rezervacija.php">Prikaz rezervacija</a>
</div>
Check In: <br>
<input type="date" name="check_in" value="<?php echo $row['check_in']; ?>">
<br>
Check Out: <br>
<input type="date" name="check_out" value="<?php echo $row['check_out']; ?>">
<br>
Ime turista :<br>
<input type="text" name="ime_turista" value="<?php echo $row['ime_turista']; ?>">
<br>
Prezime turista:<br>
<input type="text" name="prezime_turista" value="<?php echo $row['prezime_turista']; ?>">
<br>
Email turista:<br>
<input type="text" name="email_turista" value="<?php echo $row['email_turista']; ?>">
<br>
Broj osobne:<br>
<input type="text" name="broj_osobne" value="<?php echo $row['broj_osobne']; ?>">
<br>
Drzavljanstvo:<br>
<input type="text" name="drzavljanstvo" value="<?php echo $row['drzavljanstvo']; ?>">
<br>
Godina rođenja turista<br>
<input type="date" name="god_rod_turista" value="<?php echo $row['god_rod_turista']; ?>">
<br>
ID Apartmana<br>
<input type="text" name="id_apartmana" value="<?php echo $row['id_apartmana']; ?>">
<br>
<input type="submit" name="submit" value="Submit" class="buttom">
</form>
</body>
</html>
【问题讨论】:
-
您需要在更新中使用
where子句来限制更新范围。您也可以使用此代码进行 SQL 注入。 -
非常感谢,我以前也这样做过,但我已经放了,然后是 where 子句,所以现在它可以工作了......
-
不清楚
before but i have put , and then where clause是什么意思..听起来问题已经解决了。 -
问题已解决,是的,我做到了。 "' ,其中 id_rezervacije='" 。 $_GET['id_rezervacije'] 。 "'");但没有“,”之前 WHERE 是这样工作的。 “'在哪里 id_rezervacije='”。 $_GET['id_rezervacije'] 。 "'");
-
(可能)旁注:不要使用字符串插值或连接将值获取到 SQL 查询中。这很容易出错,并且可能使您的程序容易受到 SQL 注入攻击。使用参数化查询。见"How to include a PHP variable inside a MySQL statement" 和"How can I prevent SQL injection in PHP?"。