我正在尝试使用包含html表单的php文件更新我的数据库。 当我在网址栏中点击“更新”时,会显示更新的信息。但当我回到我的HTML页面向我显示数据库中的所有内容时,它仍然具有旧信息。
我不做什么?
我知道存在安全问题,例如不使用会话,清理数据或使用my_sql。 这仅适用于学校项目。学期结束后,我将关闭主机帐户。
编辑:我移动了“$ id = $ _GET ['id'];”更新查询上方的行,以便在查询之前定义“$ id”。更新了代码。
EDIT2:在关注打开错误并在更新查询后显示错误之后。它显示ID实际上没有被读回。所以我在ID之后添加了一个隐藏的输入值,以便在提交按钮之后返回给文件。
<?php
error_reporting(E_ALL & ~E_DEPRECATED);
ini_set('display_errors', 1);
$host = 'hose';
$user = 'user';
$pass = 'pass';
$database = 'database';
$table = 'table';
//connecting to server
$conn = mysql_pconnect($host,$user,$pass);
//opening to database
if (!($db = mysql_select_db($database))) {
echo "Could NOT connect to database.";
}
//gathering new data from update form
if (isset($_GET['submit'])) {
$title= $_GET['title'];
$year = $_GET['year'];
$director = $_GET['director'];
$genre = $_GET['genre'];
$runtime = $_GET['runtime'];
$id = $_GET['id'];
$query = mysql_query("UPDATE `collection`
SET `title`='$title', `year`='$year', `director`='$director', `genre`='$genre', `runtime`='$runtime'
WHERE `ID`='$id'");
}
//passing in ID number and running query
$id = $_GET['id'];
$query = "SELECT * FROM '$table' WHERE ID = '$id'";
$result = mysql_query($query);
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
//getting row data for ID number
$row = mysql_fetch_array( $result );
?>
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<title>title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="webpage.css">
<style type = "text/css">
table, th, td {
border: 0px solid black;
border-collapse: collapse;
}
table {
margin: auto;
width: 50%;
}
td {
padding: 5px;
}
img {
text-decoration: none;
}
</style>
</head>
<body class="subStyle">
<div class="topnav">
<a href="#">Home</a>
<a href="#">Database</a>
<a href="#">Insert</a>
</div>
<form class='form' method='get'>
<table border=0>
<tr>
<th>Movie Title</th>
<th>Year Made</th>
<th>Director</th>
<th>Genre</th>
<th>Runtime(Minutes)</th>
</tr>
<tr>
<td><input type=text name="title" id="title" maxlength=100 size=50 value="<?php echo $row['title']; ?>"></td>
<td><input type=text name="year" id="year" maxlength=4 size=10 value="<?php echo $row['year']; ?>"></td>
<td><input type=text name="director" id="director" maxlength=100 size=30 value="<?php echo $row['director']; ?>"></td>
<td><input type=text name="genre" id="genre" maxlength=20 size=20 value="<?php echo $row['genre']; ?>"></td>
<td><input type=text name="runtime" id="runtime" maxlength=4 size=20 value="<?php echo $row['runtime']; ?>"></td>
<td><input type=hidden name="id" id="id" value="<?php echo $row['ID']; ?>"></td>
</tr>
<tr><td></td><td></td><td>
<button class='submit' type='submit' name='submit' value='update'>Update Movie</button></td></tr>
</table>
</form>
</body>
</html>
<?php
//check if update worked
if (isset($_GET['submit'])) {
echo '<div class="form" id="form3"><br><br><br><br><br><br>
<Span>Data Updated Successfuly</span></div>';
}
//close connection
mysql_close($conn);
?>
答案 0 :(得分:0)
您的代码不安全 - Why shouldn't I use mysql_* functions in PHP?
我认为问题归结于未定义的变量 - 在您的查询中:
UPDATE '$table' SET `title`='$title', `year`='$year',
`director`='$director', `genre`='$genre', `runtime`='$runtime'
WHERE `ID`='$id'
$ id未定义