我试图使用oop php更新数据库中的数据但是在我调用函数更新之后我应该返回用户查看所有页面,所以我使用了header但是每当我尝试查看更新页面时,它都会查看所有页面。 我该怎么办?
<body>
<form method='POST'>
<table>
<tr>
<td>id</td>
<td>
<input type='text' name='id' value='' ></td>
</tr>
<tr>
<td>price</td>
<td>
<input type='text' name='price' value='' ></td>
</tr>
<tr>
<td></td>
<td>
<input type='submit' name='commit' value='Submit'>
</td>
</tr>
</table>
</form>
</body>
</html>
<?php
require_once ('database.php');
require_once ('admin.php');
$object= new admin();
if (isset($_POST['commit'])) {
$id = $object->clean($_POST['id']);
$price = $object->clean($_POST['price']);
$object->update_sport($id ,$price);
}
header("C:\wamp\www\omnia\viewsports.php");
这是viewsports.php
<head>
<title>Sports</title>
</head>
<body>
<h1>Sports</h1>
<h3>Use ID to update</h3>
<?php
require_once 'database.php';
$obj=new databaseClass();
echo '<table>';
echo '<tr bgcolor="white">';
echo ' <td>ID</td><td>Name</td><td>Price</td>';
echo ' </tr>';
$sql="SELECT sport.id, sport.name, sport.price FROM sport";
$result= mysql_query($sql);
$i=0;
while($row = mysql_fetch_assoc($result)){
echo '<tr><td>' .$row['id'].'</td><td>'
.$row['name'].'</td><td>'
.$row['price'].'</td>'
.'<td><a href="delete_sport.php?id=' . $row['id'].'">Delete</a></td>'
.'<td><a href="update_sport.php?id=' . $row['id'].'">Update</a></td></tr>';
$i++;
}
echo'</table>';
?>
<a href="add_sport.php">Add</a>
</body>
</html>
编辑:
class admin{
public function clean($str) {
$str = trim($str); // remove
/*Magic Quotes, generally speaking, is the process of escaping special characters with a '\' to allow a string to be entered into a database. This is considered 'magic' because PHP can do this automatically for you if you have magic_quotes_gpc turned on.
More specifically if magic_quotes_gpc is turned on for the copy of PHP you are using all Get, Post & Cookie variables (gpc, get it?) in PHP will already have special characters like ", ' and \ escaped so it is safe to put them directly into an SQL query.*/
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
答案 0 :(得分:1)
看起来您的问题是您在显示更新页面之前正在进行重定向。如果你更新了这项运动,你只想进行重定向。
<?php
ob_start();
require_once ('database.php');
require_once ('admin.php');
$object= new admin();
if (isset($_POST['commit'])) {
$id = $object->clean($_POST['id']);
$price = $object->clean($_POST['price']);
$object->update_sport($id ,$price);
header("Location: viewsports.php");
exit();
}
?>
编辑:
试试这个方法:
<?php
ob_start();
require_once ('database.php');
require_once ('admin.php');
$object= new admin();
if (isset($_POST['commit'])) {
$id = $object->clean($_POST['id']);
$price = $object->clean($_POST['price']);
$object->update_sport($id ,$price);
echo '<script>window.location = "viewsports.php";</script>';
exit();
}
?>
答案 1 :(得分:0)
header
发送标题信息,如果要将浏览器重定向到其他页面,则需要使用:
header("Location: viewsports.php"); // viewsports.php relative path
请参阅PHP手册了解header
正如评论中所述,这是另一种如何做到这一点的方法(imho是更好的解决方案):
<?php
require_once ('database.php');
require_once ('admin.php');
$object= new admin();
if (isset($_POST['commit'])) {
$id = $object->clean($_POST['id']);
$price = $object->clean($_POST['price']);
$object->update_sport($id ,$price);
header("Location: viewsports.php");
} else {
?>
<body>
<form method='POST'>
<table>
<tr>
<td>id</td>
<td>
<input type='text' name='id' value='' ></td>
</tr>
<tr>
<td>price</td>
<td>
<input type='text' name='price' value='' ></td>
</tr>
<tr>
<td></td>
<td>
<input type='submit' name='commit' value='Submit'>
</td>
</tr>
</table>
</form>
</body>
</html>
<?php
}
?>
如你所见,围绕HTML的东西是else子句,它只会被发送到客户端,当数据未被提交时,如果数据被提交,客户端将被重定向。
答案 2 :(得分:0)
我认为您的代码应该在下面
<?php
ob_start();
require_once ('database.php');
require_once ('admin.php');
$object= new admin();
if (isset($_POST['commit'])) {
$id = $object->clean($_POST['id']);
$price = $object->clean($_POST['price']);
$object->update_sport($id ,$price);
}
header("location:viewsports.php");
?>
<body>
<form method='POST'>
<table>
<tr>
<td>id</td>
<td>
<input type='text' name='id' value='' ></td>
</tr>
<tr>
<td>price</td>
<td>
<input type='text' name='price' value='' ></td>
</tr>
<tr>
<td></td>
<td>
<input type='submit' name='commit' value='Submit'>
</td>
</tr>
</table>
</form>
</body>
</html>
答案 3 :(得分:0)
您需要在表单标记
中添加操作所以: