gpt4 book ai didi

PHP - 使用 header 返回到另一个页面

转载 作者:行者123 更新时间:2023-11-29 12:14:13 28 4
gpt4 key购买 nike

我试图使用oop 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>

<?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);
}

最佳答案

您的问题似乎是在显示更新页面之前正在执行重定向。您只想在更新了游戏后才进行重定向。

<?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();
}
?>

关于PHP - 使用 header 返回到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30125217/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com