如何在删除该记录之前显示该记录?
Posted
技术标签:
【中文标题】如何在删除该记录之前显示该记录?【英文标题】:How do I display the record before deleting that record? 【发布时间】:2021-07-09 17:54:18 【问题描述】:我希望在 php 中向用户显示我正在删除的记录?
我正在尝试以要删除的表单显示记录,但仅显示输入类型 = 文本值,而未显示单选按钮值?
<?php
require('/home/s3022041/sqlC/dbConnect.php');
if(isset($_POST['search']))
$search = mysqli_real_escape_string($connection, $_POST['id']);
$id = $_POST['id'];
$stp1 = preg_replace("/[^a-zA-Z0-9]/", "", $id); //grab only the alphanumerics
$stp2 = strtoupper($stp1); //Make all alphabets uppercase
$stp3 = preg_replace('/\d+/', '',$stp2); //extract the alphabets part
$newsearchid = str_replace($stp3,"-".$stp3."-",$stp2); //put hyphens before and after the alphabet part
$query = "SELECT * FROM cars WHERE Registration_Number = '$newsearchid' ";
$query_run = mysqli_query($connection, $query);
while($row = mysqli_fetch_array($query_run))
?>
<form action="delete_process.php" method="POST" class="form">
<h2>Car Registration Form</h2>
<div class="mb-3">
<input type="radio" name="Car" id="Toyota" value=" <?php echo $row['make']; ?>" />
<label for="Toyota" class="form-label">Toyota</label>
<select name="Models[Toyota]" class="form-control" value=" <?php echo $row['model']; ?>">
<option value="">None</option>
<option value="Camri">Camri</option>
<option value="Corolla">Corolla</option>
<option value="Estima">Estima</option>
</select><br>
<input type="radio" name="Car" id="Nissan" value=" <?php echo $row['make']; ?>"/>
<label for="Nissan" class="form-label">Nissan</label>
<select name="Models[Nissan]" class="form-control" value=" <?php echo $row['model']; ?>">
<option value="">None</option>
<option value="Micra">Micra</option>
<option value="roller">roller</option>
<option value="fushe">fushe</option>
</select><br>
<input type="radio" name="Car" id="Mercedez" value=" <?php echo $row['make']; ?>"/>
<label for="Mercedez" class="form-label">Mercedez</label>
<select name="Models[Mercedez]" class="form-control" value=" <?php echo $row['model']; ?>">
<option value="">None</option>
<option value="5series">5series</option>
<option value="x7">x7</option>
<option value="x5">x5</option>
</select><br>
<label for="vehicleidentificationnumber" class="form-label">vehicle identification
number(VIN)</label>
<input type="text" disabled="disabled" name="vehicleidentificationnumber" class="form-control" maxlength=20 value=" <?php echo $row['VIN']; ?>" required >
<br>
<label for="ManufacturingYear" class="form-label">Manufacturing Year</label>
<input type="text" name="Manufacture_Year" class="form-control" maxlength=20 value=" <?php echo $row['Manufacture_Year']; ?>" required >
<label for="Enginesize" class="form-label">Engine size</label>
<input type="text" name="Engine_Size" class="form-control" maxlength=20 value=" <?php echo $row['Engine_Size']; ?>" required>
<label for="TransmissionType" class="form-label">TransmissionType</label><br>
<input type="radio" name="Transmission_Type" id="TransmissionType" value=" <?php echo $row['Transmission_Type']; ?>" />
<label for="Automatic" class="form-label">Automatic</label><br>
<input type="radio" name="Transmission_Type" id="TransmissionType" value=" <?php echo $row['Transmission_Type']; ?>" />
<label for="Manual" class="form-label">Manual</label><br>
<input type="radio" name="Transmission_Type" id="TransmissionType" value=" <?php echo $row['Transmission_Type']; ?>" />
<label for="SemiAutomatic" class="form-label">Semi-Automatic</label><br>
<label for="NoofSeats" class="form-label">No. of Seats</label>
<input type="number" name="NoOfSeats" class="form-control" value="<?php echo $row['NoOfSeats']; ?>" maxlength=20 required>
<label for="Noofdoors" class="form-label">No. of doors</label>
<input type="number" name="NoOfDoors" class="form-control" maxlength=20 value="<?php echo $row['NoOfDoors']; ?>" required>
<label for="Fueltype" class="form-label">Fuel type</label>
<input type="text" name="Fuel_Type" class="form-control" maxlength=20 value=" <?php echo $row['Fuel_Type']; ?>" required>
<label for="Colour" class="form-label">Colour</label>
<input type="text" name="Colour" class="form-control" maxlength=20 value=" <?php echo $row['Colour']; ?>" required>
<label for="RegistrationNumber" class="form-label">Registration Number (use Dublin
registration) </label>
<input type="hidden" disabled="disabled" name="Registration_Number" class="form-control" maxlength=20 value=" <?php echo $row['Registration_Number']; ?>" required>
<label for="Dateoffirstregistration" class="form-label">Date of first registration</label>
<input type="date" name="DateOfRegestration" class="form-control" maxlength=20 value=" <?php echo $row['DateOfRegestration']; ?>" required>
</div>
<button type="submit" name="search" class="btn btn-primary">Delete</button>
</form>
<?php
else
echo "<h1> No records found </h1>";
echo "<a href='index.php'>home</a>";
?>
</div>
<div class="modal-footer">
</form>
这里我正在尝试删除上面显示的记录,但它说删除成功但是当我去检查记录时它没有被删除它仍然存在?
<?php
require('/home/s3022041/sqlC/dbConnect.php');
if(isset($_POST['search']))
$search = mysqli_real_escape_string($connection, $_POST['search']);
$id = $_POST['Registration_Number'];
$query = "DELETE FROM `cars` WHERE Registration_Number='$id' ";
$query_run = mysqli_query($connection, $query) or die ("not done");
if($query_run)
echo "<h1> deleted successfully</h1>";
echo "<a href='index.php'>home</a>";
else
echo "<h1> not deleted </h1>";
echo "<a href='index.php'>home</a>";
echo 'Error! ' . mysqli_error($connection);
【问题讨论】:
仅供参考,<select>
没有 value
属性。您的单选元素有 两个 值属性,只使用一个
我已经删除了同样的问题?
您确定要“硬删除”数据吗?
是的,我想删除它
警告:您对SQL Injections 持开放态度,应该使用参数化的prepared statements,而不是手动构建查询。它们由PDO 或MySQLi 提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your data。 Escaping is not enough!
【参考方案1】:
改变
1.表格
<input type="radio" name="Car" id="Toyota" value="<?=$row['make']; ?>" />
到
<input type="radio" name="Car" id="Toyota" <?=($row['make'] == 'Toyota' ? 'value="'.$row['make'].'" checked':NULL); ?> />
<!-- or better yet -->
<input type="radio" name="Car" id="Toyota" value="Toyota" <?=($row['make'] == 'Toyota' ? 'checked':NULL); ?> />
2。数据库
文件:delete_process.php
// Change
if($query_run) // << This only tell the script the sql statement run with no errors
// To
if(mysql_affected_rows()<=1) // << This confirms that one or more rows where changed by the sql statement
解释
使用三元运算符 (?:) 的 PHP 速记 If/Else
/* basic usage */
$var = 5;
$var_is_greater_than_two = ($var > 2 ? true : false); // returns true
错误原因
如果未显示单选按钮,则在获取 $row['make'] 时,它很可能是“未定义变量”。
其他的,去掉白色的间距。因为这可能在使用 MYSQL 语句时发挥作用。
<!-- From -->
value=" <?php echo $row['Registration_Number']; ?>"
<!-- To -->
value="<?=$row['Registration_Number']; ?>"
*简写一行回显/打印=
【讨论】:
以上是关于如何在删除该记录之前显示该记录?的主要内容,如果未能解决你的问题,请参考以下文章