问题描述
这是我的update.php代码。我已经检查过其他的php文件,发现没有问题。这些代码没有错误,但它无法更新数据库中的数据。
require_onceconn.php;
$ conn = connect();
$ db = connectdb();
$ id =;
$ parcelno =;
$ items =;
if(isset($ _ REQUEST ['id'])){$ id = $ _REQUEST ['id']; }
if(isset($ _ REQUEST ['parcel'])){$ parcel = $ _REQUEST ['parcel']; }
if(isset($ _ REQUEST ['items'])){$ items = $ _REQUEST ['items']; }
mysql_select_db($ db,$ conn)或die(mysql_error()。\\\
);
$ sql =UPDATE parcel SET parcelno ='$ parcelno',items ='$ items'where id ='$ id';
$ result = @ mysql_query($ sql)或die(mysql_error()。\\\
);
这是用于更新形式的edit.php代码:
<?php
$ ic = $ _REQUEST [ic];
require_onceconn.php;
$ conn = connect();
$ db = connectdb();
mysqli_select_db($ conn,$ db)或die(mysqli_error($ conn)。\\\
);
$ query_usr =select * from parcel;
$ usr = mysqli_query($ conn,$ query_usr)或die(mysqli_error($ conn)。\\\
。$ query_usr);
$ row_usr = mysqli_fetch_assoc($ usr);
?>
< html>< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8>
< style type =text / css>
body {
background-color:#CCC;
}
< / style>
< / head>< body><中心>< p>< a href =adminselect.php>< img src =image / header.pngwidth =800 height =200>< / a>< / p>< / center>
< form action =update.phpmethod =get>
<?php
$ sql =SELECT * FROM parcel where ic = $ ic;
$ result1 = mysqli_query($ conn,$ sql);
while($ row = mysqli_fetch_assoc($ result1)){
?>
< center>< table border =0>
< tr>
< td colspan =3bgcolor =#0066FF>< strong>< center>更新注册< / strong>< / td&
< / tr>
< / tr>
< tr>
< td bgcolor =#0099FF>包裹号< / td>
< td>:< / td>
< td bgcolor =#FFFFFF>< input name =parcelnotype =textid =parcelnovalue =<?php echo $ row [parcelno] ;? > size =50>< / td>
< / tr>
< tr>
< td bgcolor =#0099FF> Items< / td>
< td>:< / td>
< td colspan =2bgcolor =#FFFFFF>
< p>
< label for =select>< / label>
< select name =itemssize =1id =items>
< option><?php echo $ row [items];?>< / option>
< option> Pos Laju< / option>
< option> Pos Ekspress< / option>
< option> Skynet< / option>
< option> GDEX< / option>
< option>全国快递< / option>
< option> FedEx< / option>
< option> UPS< / option>
< / select>
< / p>< / td>
< / tr>
< tr>
< td>& nbsp;< / td>
< td>& nbsp;< / td>
< td colspan =2bgcolor =#CCCCCC> < input name =type =submitvalue =Update>< / td>
< / tr>
<?php}?>
< / table>< / center>
< / form>< / body>< / html>
我研究这个问题几乎半天,没有什么工作:/
您使用两个不同的变量:
$ parcelno
在您的UPDATE查询中
和 $ parcel = $ _REQUEST ['parcel'];
/ p>
两个变量必须匹配。
将错误报告添加到文件顶部)
> error_reporting(E_ALL);
ini_set('display_errors',1);
您现在的代码可以打开。使用,或和。
此外,我在评论 +1
(如果可能)中引用 nkchandra
:
与您的问题无关,但是FYI,PHPMyAdmin不是数据库,而是与您的案例中的mysql数据库交互的工具
修改:阅读您的评论后,您需要切换到功能。
在学习使用预编译语句之前,这只是一个快速修复。
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$ DB_HOST =xxx; // replace with yours
$ DB_USER =xxx; // replace with yours
$ DB_PASS =xxx; // replace with yours
$ DB_NAME =xxx; // replace with yours
$ conn = new mysqli($ DB_HOST,$ DB_USER,$ DB_PASS,$ DB_NAME);
if($ conn-> connect_errno> 0){
die('Connection failed ['。$ conn-> connect_error。']');
}
$ id =;
$ parcelno =;
$ items =;
if(isset($ _ REQUEST ['id'])){
$ id = mysqli_real_escape_string($ conn,$ _ REQUEST ['id']); }
if(isset($ _ REQUEST ['parcel'])){
$ parcelno = mysqli_real_escape_string($ conn,$ _ REQUEST ['parcel']); }
if(isset($ _ REQUEST ['items'])){
$ items = mysqli_real_escape_string($ conn,$ _ REQUEST ['items']); }
$ sql =UPDATE parcel SET parcelno ='$ parcelno',items ='$ items'where id ='$ id';
$ result = mysqli_query($ conn,$ sql)或die(mysqli_error()。\\\
);
if(!$ result)
{
throw new Exception($ conn-> error);
}
else {echoSuccess; }
mysqli_close($ conn); //关闭连接
code> r3wt's 注释:您也可以使用:
$ result = $ conn- > query($ sql)或die(mysqli_error()。\\\
);
而不是
$ result = mysqli_query($ conn,$ sql)或die(mysqli_error()。\\\
);
This is my update.php codes. I already checked others php file and found no problem. This codes have no error but it cant updating data in database.
require_once "conn.php";
$conn=connect();
$db=connectdb();
$id= "";
$parcelno = "";
$items = "";
if(isset($_REQUEST['id'])){ $id= $_REQUEST['id']; }
if(isset($_REQUEST['parcel'])){ $parcel = $_REQUEST['parcel']; }
if(isset($_REQUEST['items'])){ $items = $_REQUEST['items']; }
mysql_select_db($db,$conn) or die (mysql_error()."\n");
$sql="UPDATE parcel SET parcelno='$parcelno', items='$items' where id='$id'";
$result=@mysql_query($sql) or die(mysql_error()."\n");
This is edit.php code which used for update form :
<?php
$ic = $_REQUEST["ic"];
require_once "conn.php";
$conn = connect();
$db = connectdb();
mysqli_select_db($conn,$db) or die (mysqli_error($conn)."\n");
$query_usr = "select * from parcel";
$usr = mysqli_query($conn,$query_usr) or die (mysqli_error($conn)."\n".$query_usr);
$row_usr = mysqli_fetch_assoc($usr);
?>
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
body {
background-color: #CCC;
}
</style>
</head><body><center><p><a href="adminselect.php"><img src="image/header.png" width="800" height="200"></a></p></center>
<form action="update.php" method="get">
<?php
$sql = "SELECT * FROM parcel where ic = $ic";
$result1 = mysqli_query($conn,$sql);
while ($row=mysqli_fetch_assoc($result1)){
?>
<center><table border="0">
<tr>
<td colspan="3" bgcolor="#0066FF"><strong><center>Update Registration </strong></td>
</tr>
</tr>
<tr>
<td bgcolor="#0099FF">Parcel Number</td>
<td>:</td>
<td bgcolor="#FFFFFF"><input name="parcelno" type="text" id="parcelno" value="<?php echo $row["parcelno"];?>" size="50"></td>
</tr>
<tr>
<td bgcolor="#0099FF">Items</td>
<td>:</td>
<td colspan="2" bgcolor="#FFFFFF">
<p>
<label for="select"></label>
<select name="items" size="1" id="items">
<option><?php echo $row["items"];?></option>
<option>Pos Laju</option>
<option>Pos Ekspress</option>
<option>Skynet</option>
<option>GDEX</option>
<option>Nationwide Express</option>
<option>FedEx</option>
<option>UPS</option>
</select>
</p></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td colspan="2" bgcolor="#CCCCCC"> <input name="" type="submit" value="Update"></td>
</tr>
<?php }?>
</table></center>
</form></body></html>
I researched this problem almost half a day and nothing works :/
You're using two different variables:
$parcelno
in your UPDATE query
and $parcel = $_REQUEST['parcel'];
both variables must match. If one doesn't, then your entire query will fail.
Add error reporting to the top of your file(s) right after your opening <?php
tag, which will help during pre-production testing.
error_reporting(E_ALL);
ini_set('display_errors', 1);
Your present code is open to SQL injection. Use prepared statements, or PDO with prepared statements.
Plus, I quote nkchandra
in a comment +1
(if I may):
"Irrelevant to your question, but FYI, PHPMyAdmin is not a database but a tool to interact with database like mysql in your case"
Edit: After reading your comment, it seems like you will need to switch to mysqli_
functions.
This is just a quick fix before you learn to use prepared statements.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$DB_HOST = "xxx"; // replace with yours
$DB_USER = "xxx"; // replace with yours
$DB_PASS = "xxx"; // replace with yours
$DB_NAME = "xxx"; // replace with yours
$conn = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($conn->connect_errno > 0) {
die('Connection failed [' . $conn->connect_error . ']');
}
$id= "";
$parcelno = "";
$items = "";
if(isset($_REQUEST['id'])){
$id= mysqli_real_escape_string($conn,$_REQUEST['id']); }
if(isset($_REQUEST['parcel'])){
$parcelno = mysqli_real_escape_string($conn,$_REQUEST['parcel']); }
if(isset($_REQUEST['items'])){
$items = mysqli_real_escape_string($conn,$_REQUEST['items']); }
$sql="UPDATE parcel SET parcelno='$parcelno', items='$items' where id='$id'";
$result=mysqli_query($conn,$sql) or die(mysqli_error()."\n");
if (!$result)
{
throw new Exception($conn->error);
}
else { echo "Success"; }
mysqli_close($conn); // close the connection
Plus, as per r3wt's
comment: You can also use:
$result= $conn->query($sql) or die(mysqli_error()."\n");
instead of
$result=mysqli_query($conn,$sql) or die(mysqli_error()."\n");
这篇关于无法从MySQL数据库更新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!