首页 > 解决方案 > 我无法使用 PHP 程序更新 MySQL 数据库中的所有数据

问题描述

我不明白为什么当我点击更新记录按钮时,下面的代码只更新以下数据:

语言 电话 酒店类型 金额

此外,所有表单字段内部都有值(以前保存在数据库中)但是当我点击更新记录按钮时(假设我这次没有更改任何内容)从数据库中删除的所有记录,只有上面提到的记录仍然完好无损。

有人可以帮我弄清楚我做错了什么吗?

提前致谢。

<?php
include 'auth.php';
require 'connect_db.php';
$id = $_REQUEST['id'];
$query = "SELECT * from csvdata where id ='".$id."'"; 
$result = mysqli_query($connection, $query) or die (mysqli_error($connection));
$row = mysqli_fetch_assoc($result);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Update Record(s)</title>
<link rel="stylesheet" href="css/style.css" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js" integrity="sha384-o+RDsa0aLu++PJvFqy8fFScvbHFLtbvScb8AjopnFD+iEQ7wo/CG0xlczd+2O/em" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap-theme.min.css" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
</head>
<body>
<?php
$status = "";
if(isset($_POST['new']) && $_POST['new']==1)
{
$id = $_REQUEST['id'];
$clientDepartment = $_REQUEST['clientDepartment'];
$language = $_REQUEST['language'];
$arrivalDate = $_REQUEST['arrivalDate'];
$passengerName = $_REQUEST['passengerName'];
$phone = $_REQUEST['phone'];
$ticketorHotel = $_REQUEST['ticketorHotel'];
$lastSegFlightNr = $_REQUEST['lastSegFlightNr'];
$arrivalTime = $_REQUEST['arrivalTime'];
$hotel = $_REQUEST['hotel'];
$regNr = $_REQUEST['regNr'];
$type = $_REQUEST['type'];
$amount = $_REQUEST['amount'];
$update = "update csvdata set clientDepartment='".$clientDepartment."', language='".$language."', arrivalDate='".$arrivalDate."', passengerName='".$passengerName."', phone='".$phone."', ticketorHotel='".$ticketorHotel."', lastSegFlightNr='".$lastSegFlightNr."', arrivalTime='".$arrivalTime."', hotel='".$hotel."', regNr='".$regNr."', type='".$type."', amount='".$amount."' where id='".$id."'" or die(mysqli_error($connection));
mysqli_query($connection, $update) or die (mysqli_error($connection));
$status = "<div style='text-align:center'><h3 class='form-signin-heading'>Record(s) Updated Successfully!</h3></br></br>
<a class='btn btn-lg btn-primary' href='dashboard.php'><i class='fas fa-clipboard-list'></i> Dashboard</a></div>";
echo '<div style="color:#FF0000;">'.$status.'</div>';
echo "</div>";
}else {
?>
<form class="form-signin" method="POST" action="">
<h2 class="form-signin-heading">Update Record(s)</h2>
<input type="hidden" name="new" value="1" />
<input name="id" type="hidden" value="<?php echo $row['id'];?>" />
<label for="inputClient" class="">Client Department</label>
        <input type="text" name="client" class="form-control" placeholder="Enter Client Department" value="<?php echo $row['department'];?>">
<label for="inputLanguage" class="">Language</label>
        <input type="text" name="language" class="form-control" placeholder="Enter Language" value="<?php echo $row['language'];?>">    
<label for="inputDate" class="">Arrival Date</label>
        <input type="text" name="date" class="form-control" placeholder=" Enter Arrival Date" value="<?php echo $row['arrivalDate'];?>">    
<label for="inputName" class="">Passenger Name</label>
        <input type="text" name="name" class="form-control" placeholder="Enter Passenger Name" value="<?php echo $row['passengerName'];?>">
<label for="inputPhone" class="">Phone</label>
        <input type="text" name="phone" class="form-control" placeholder="Enter Phone" value="<?php echo $row['phone'];?>">             
<label for="inputTicketor" class="">Ticketor/Hotel</label>
        <input type="text" name="ticketor" class="form-control" placeholder="Enter Ticketor/Hotel" value="<?php echo $row['ticketorHotel'];?>">
<label for="inputFlight" class="">LastSeg/FlightNr</label>
        <input type="text" name="flight" class="form-control" placeholder="Enter LastSeg FlighrNr" value="<?php echo $row['lastSegFlightNr'];?>">
<label for="inputTime" class="">Arrival Time</label>
        <input type="text" name="time" class="form-control" placeholder="Enter Arrival Time" value="<?php echo $row['arrivalTime'];?>"> 
<label for="inputHotel" class="">Hotel</label>
        <input type="text" name="hotel" class="form-control" placeholder="Enter Hotel" value="<?php echo $row['hotel'];?>">         
<label for="inputReg" class="">RegNr</label>
        <input type="text" name="reg" class="form-control" placeholder="Enter RegNr" value="<?php echo $row['regNr'];?>">       
<label for="inputType" class="">Type</label>
        <input type="text" name="type" class="form-control" placeholder="Enter Type" value="<?php echo $row['type'];?>">        
<label for="inputAmount" class="">Amount</label>
        <input type="text" name="amount" class="form-control" placeholder="Enter Amount" value="<?php echo $row['amount'];?>">
<p>&nbsp;</p>           
<p><button class="btn btn-lg btn-success" style="font-size: 1.1rem;" name="submit" type="submit"><i class="fas fa-plus-circle"></i> Update Record(s)</button> <a style="font-size: 1.1rem;" class="btn btn-lg btn-warning" href="dashboard.php" role="button"><i class="fas fa-times-circle"></i> Cancel</a></p>
</form>
<?php } ?>
</body>
</html>

标签: phpmysql

解决方案


检查输入名称。

例如 RegNr:您的表单包含 name="reg" 并且您使用 $_REQUEST['regNr'] 而不是 $_REQUEST['reg']; 其他领域也有同样的问题。

检查此链接以了解有关基础知识的更多信息 FORM/GET/POST https://www.w3schools.com/php/php_forms.asp


推荐阅读