【发布时间】:2017-07-30 19:46:16
【问题描述】:
我正在尝试在显示我网站上的数据库的表中创建一个编辑选项。 问题是即使我单击保存,数据库也没有更新。 没有错误显示。 有一个 EditComplaint.php,它从 DB 中获取数据并将其显示在输入框中,以便可以对其进行编辑,还有一个 Ecomp.php,它在单击 Save 时被调用。
<?php
$id = $_GET['id'];
$db_host = 'localhost'; // Server Name
$db_user = 'Username'; // Username
$db_pass = 'Username'; // Password
$db_name = 'Database'; // Database Name
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn) {
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
$sql = "SELECT `id`, `ref_no`, `type`, `comp_name`, `comp_no`, `station`, `pertains`, `user_remarks`, `to_whom`, `concern`, `brief_fct`, `sec_remarks`, `depart`, `cisf_remarks`, `generalcomment`, `status` FROM `Complaintstable` WHERE `Complaintstable`.`id` = '$id'";
$query = mysqli_query($conn, $sql);
if (!$query) {
die ('SQL Error: ' . mysqli_error($conn));
}
?>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link href="https://fonts.googleapis.com/css?family=Titillium+Web|Roboto+Condensed:400,300|Pathway+Gothic+One|ABeeZee:400,400italic" rel="stylesheet" type="text/css">
<link href="master-stylesheet.css" rel="stylesheet">
<link href="custom-stylesheet.css" rel="stylesheet">
<link href="complaintstable.css" rel="stylesheet">
<link href="dmrc-favicon.png" rel="shortcut icon" type="image/x-icon">
<title>DMRC/Login</title>
</head>
<body>
<form action="Ecomp.php" id="form1" method="post" name="form1">
<!-- header section -->
<div class="header">
<div class="header-inner">
<div class="header-top">
<div class="welcome-guest">
<span class="align-right float-right" id="lblusername" style="color:Black;">Guest</span><em><span class="welcome-cont float-right fontfamily_2" id="lblwelcome" style="padding-left:5px;padding-right:5px;color:black;">Welcome</span></em>
</div>
<div class="header-top-links">
<ul>
<li>
<a href="" id="active">Home</a>
</li>
</ul>
</div>
</div>
<div class="header-bottom">
</div>
</div>
</div>
<div class="container clearfix">
<br><br>
<table class="data-table">
<thead>
<tr>
<th>ID</th>
<th>Referral/No.</th>
<th>Type</th>
<th>Complainant Name</th>
<th>Station</th>
<th>Pertains To</th>
<th>User Remarks</th>
<th>To Whom</th>
<th>Concern</th>
<th>Brief Fact</th>
<th>Security Comments</th>
<th>Deptt</th>
<th>CISF Comments</th>
<th>General Comment</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
while($row = $query->fetch_assoc())
{
$id = $row['id'];
$ref_no = $row['ref_no'];
$type = $row['type'];
$comp_name = $row['comp_name'];
$station = $row['station'];
$pertains = $row['pertains'];
$user_remarks = $row['user_remarks'];
$to_whom = $row['to_whom'];
$concern = $row['concern'];
$brief_fct = $row['brief_fct'];
$sec_remarks = $row['sec_remarks'];
$depart = $row['depart'];
$cisf_remarks = $row['cisf_remarks'];
$generalcomment = $row['generalcomment'];
$status = $row['status'];
if ($i == 0)
{
$i++;
echo "<tr>";
echo "<td><input type='text' name='id' value='$id'/></td>";
echo "<td><input type='text' name='ref_no' value='$ref_no'/></td>";
echo "<td><input type='text' name='type' value='$type'/></td>";
echo "<td><input type='text' name='comp_name' value='$comp_name'/></td>";
echo "<td><input type='text' name='station' value='$station'/></td>";
echo "<td><input type='text' name='pertains' value='$pertains'/></td>";
echo "<td><input type='text' name='user_remarks' value='$user_remarks'/></td>";
echo "<td><input type='text' name='to_whom' value='$to_whom'/></td>";
echo "<td><input type='text' name='concern' value='$concern'/></td>";
echo "<td><input type='text' name='brief_fct' value='$brief_fct'/></td>";
echo "<td><input type='text' name='sec_remarks' value='$sec_remarks'/></td>";
echo "<td><input type='text' name='depart' value='$depart'/></td>";
echo "<td><input type='text' name='cisf_remarks' value='$cisf_remarks'/></td>";
echo "<td><input type='text' name='generalcomment' value='$generalcomment'/></td>";
echo "<td><input type='text' name='status' value='$status'/></td>";
echo "<td><a href='delete.php?id=$id' class='button-new'>Delete</a></td>";
echo "</tr>";
}
echo '<br><br>';
}
?>
</tbody>
</table>
<br><br>
<input type="submit" class="button-new" value="Save"/>
</div>
<div class="footer">
<div class="footer-top">
<div class="footer-top-inner">
<ul>
<li>
<a href="">FAQs</a>
</li>
<li>
<a href="">Contact Us</a>
</li>
<li>
<a href="">Disclaimer</a>
</li>
<li>
<a href="">Terms & Conditions</a>
</li>
</ul>
</div>
</div>
<div class="footer-bottom fontfamily_2">
<div class="footer-bottom-inner">
<div class="float-left footer-text">
</div>
</div>
</div>
</div>
</form>
</body>
</html>
这是 Save 调用的 Ecomp.php
<?php
$db_host = 'localhost'; // Server Name
$db_user = 'Username'; // Username
$db_pass = 'Username'; // Password
$db_name = 'Database'; // Database Name
// Create connection
$con = mysqli_connect($servername, $username, $password) or die("Unable to Connect to '$dname'");
// Check connection
if (!$con)
{
echo "Please try later.";
}
else
{
mysqli_select_db($con, $dname);
}
$id = $_POST['id'];
$ref_no = $_POST['ref_no'];
$type = $_POST['type'];
$comp_name = $_POST['comp_name'];
$station = $_POST['station'];
$pertains = $_POST['pertains'];
$user_remarks = $_POST['user_remarks'];
$to_whom = $_POST['to_whom'];
$concern = $_POST['concern'];
$brief_fct = $_POST['brief_fct'];
$sec_remarks = $_POST['sec_remarks'];
$depart = $_POST['depart'];
$cisf_remarks = $_POST['cisf_remarks'];
$generalcomment = $_POST['generalcomment'];
$status = $_POST['status'];
mysqli_query($con," UPDATE `Complaintstable` SET `ref_no`= '$ref_no',`type`='$type',`comp_name`=`$comp_name`,`station`='$station',`pertains`='$pertains',`user_remarks`='$user_remarks',`to_whom`='$to_whom',`concern`='$concern',`brief_fct`='$brief_fct',`sec_remarks`='$sec_remarks',`depart`='$depart',`cisf_remarks`='$cisf_remarks',`generalcomment`='$generalcomment',`status`='$status' WHERE `Complaintstable`.`id` = '$id'");
header("Location: complaintstable.php");
?>
请指导我。 这是我第一次来这里。 提前致谢。 编辑:更新 SQL 查询在 phpmyadmin 控制台中运行时有效。
【问题讨论】:
-
仅供参考。 PHPMyAdmin 不是数据库。它只是一个基于 Web 的应用程序,您可以在其中管理 MySQL 数据库。
-
好的,谢谢先生的澄清。我改一下标题。
-
您对SQL Injections 持开放态度,应该真正使用Prepared Statements 而不是连接您的查询。特别是因为您根本没有逃避用户输入!这也意味着如果任何字段包含
'字符或以反斜杠结尾,则查询将失败。 -
将
or die(mysqli_error($con)添加到您的查询中,以便您查看错误。mysqli_query($con,"YOUR UPDATE QUERY") or die(mysqli_error($con)); -
另外,检查服务器错误日志,看看是否有任何错误,可能有。
标签: php mysql phpmyadmin