尝试使用 PHP Header(“Location:”) 函数进行重定向
我在这件事上跌跌撞撞地走出了困境。我无法让此代码在提交创建产品后立即将用户重定向到其产品页面。我有三个文件 create_product.php |产品.php | &我正在从中调用函数的外部函数页面。我似乎无法开始工作的是: header("location:product.php?product_id='.$product_id.'");功能。所有内容都正确输入到数据库中,但是当用户单击“提交”来创建产品时,我收到“该产品不存在”错误。任何帮助将不胜感激(我知道存在安全问题等......目前不担心这些问题,而只是获取“创建产品提交按钮以将用户重定向到新创建的产品页面......” 并且对低响应率感到抱歉(作为新用户已经引起我的注意......我将开始接受所有答案)
.
<?php
if (isset($_POST['product_name'], $_POST['product_description'], $_FILES['fileField'])) {
$product_name = $_POST['product_name'];
$product_description = $_POST['product_description'];
$image_name = $_FILES['fileField']['name'];
$image_size = $_FILES['fileField']['size'];
$image_temp = $_FILES['fileField']['tmp_name'];
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
$image_ext = strtolower(end(explode('.', $image_name)));
$errors = array();
if (empty($product_name) || empty($product_description) || empty($image_name)) {
$errors[] = "All Fields are required";
} else {
if (strlen($product_name) > 55 || strlen($product_description) > 255) {
$errors[] = "One or more fields contains too many characters";
} else {
if (in_array($image_ext, $allowed_ext) === false) {
$errors[] = 'File type not allowed';
}
if ($image_size > 2097152) {
$errors[] = 'Maximum file size is 2MB';
}
}
}
if (!empty($errors)) {
foreach ($errors as $error) {
echo $error, '<br/>';
}
} else {
create_product($product_name, $product_description, $image_temp, $image_ext);
header("location: product.php?product_id='.$product_id.'");
exit();
}
}
?>
。
<?php
// check to see the url variable product_id is set
if (isset($_GET['product_id'])) {
$product_id = preg_replace('#[^0-9]#i','',$_GET['product_id']);
// use this variable to check to see if this product ID exists...
// if yes then get details...
// if no then exit this script and give error
$sql = mysql_query("SELECT * FROM products WHERE product_id = '$product_id' LIMIT 1");
$productCount = mysql_num_rows($sql); // count the output
if ($productCount > 0) {
// get all the product details
while($row = mysql_fetch_array($sql)) {
$member_id = $row['member_id'];
$timestamp = strftime("%b %d %Y", strtotime($row['timestamp']));
$prodtitle = $row['bmtitle'];
$proddesc = $row['bmdesc'];
}
} else {
echo "That product does not exist";
exit();
}
} else {
echo "Need data - product id needed to render this page is missing.";
exit();
}
?>
然后我继续在这个product.php页面中回显 最后包含的产品函数
页面:(仅显示 create_product 函数):
<?php
function create_product($product_name, $product_description, $image_temp, $image_ext) {
$product_name = mysql_real_escape_string(htmlentities($product_name));
$product_description = mysql_real_escape_string(htmlentities($product_description));
mysql_query("INSERT INTO products (product_id, member_id, timestamp, prodtitle, proddesc, image_temp, image_ext) VALUES ('','".$_SESSION['member_id']."', UNIX_TIMESTAMP(), '$product_name', '$product_description', '$image_temp', '$image_ext')");
mkdir('uploads/'.mysql_insert_id(), 0777);
mkdir('uploads/thumbs/'.mysql_insert_id(), 0777);
// place image & thumbnail in the respective folder
$bmid = mysql_insert_id();
$image_file = "$bmid."."jpg";
$location = "uploads/$bmid/$image_file";
move_uploaded_file($_FILES['fileField']['tmp_name'],$location);
create_thumb('uploads/'.$bmid.'/', $image_file, 'uploads/thumbs/'.$bmid.'/');
}
?>
I'm stumbling out of the blocks on this one. I can't get this code to redirect the user to their product page immediately after submitting to create the product. I've got three files create_product.php | product.php | & an external function page i'm calling a function from. What i can't seem to get to work is the: header("location: product.php?product_id='.$product_id.'"); functionality. All the stuff is input into the database correctly but when the user clicks submit to create the product i get the "That product does not exist" error. Any help would be greatly appreciated (I know there are issues w/ security, etc...not worried about those at the moment, but rather just getting the "create product submit button to redirect the user to that newly created product page...and sorry for the low response rate (already been brought to my attention as a new user...i will start accepting all answers). Thanks a ton for any help you can provide,
CREATE_PRODUCT.PHP PAGE:
<?php
if (isset($_POST['product_name'], $_POST['product_description'], $_FILES['fileField'])) {
$product_name = $_POST['product_name'];
$product_description = $_POST['product_description'];
$image_name = $_FILES['fileField']['name'];
$image_size = $_FILES['fileField']['size'];
$image_temp = $_FILES['fileField']['tmp_name'];
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
$image_ext = strtolower(end(explode('.', $image_name)));
$errors = array();
if (empty($product_name) || empty($product_description) || empty($image_name)) {
$errors[] = "All Fields are required";
} else {
if (strlen($product_name) > 55 || strlen($product_description) > 255) {
$errors[] = "One or more fields contains too many characters";
} else {
if (in_array($image_ext, $allowed_ext) === false) {
$errors[] = 'File type not allowed';
}
if ($image_size > 2097152) {
$errors[] = 'Maximum file size is 2MB';
}
}
}
if (!empty($errors)) {
foreach ($errors as $error) {
echo $error, '<br/>';
}
} else {
create_product($product_name, $product_description, $image_temp, $image_ext);
header("location: product.php?product_id='.$product_id.'");
exit();
}
}
?>
PRODUCT.PHP PAGE:
<?php
// check to see the url variable product_id is set
if (isset($_GET['product_id'])) {
$product_id = preg_replace('#[^0-9]#i','',$_GET['product_id']);
// use this variable to check to see if this product ID exists...
// if yes then get details...
// if no then exit this script and give error
$sql = mysql_query("SELECT * FROM products WHERE product_id = '$product_id' LIMIT 1");
$productCount = mysql_num_rows($sql); // count the output
if ($productCount > 0) {
// get all the product details
while($row = mysql_fetch_array($sql)) {
$member_id = $row['member_id'];
$timestamp = strftime("%b %d %Y", strtotime($row['timestamp']));
$prodtitle = $row['bmtitle'];
$proddesc = $row['bmdesc'];
}
} else {
echo "That product does not exist";
exit();
}
} else {
echo "Need data - product id needed to render this page is missing.";
exit();
}
?>
I then go on within this product.php page to echo out the variables like $prodtitle, etc
LASTLY THE INCLUDED PRODUCT FUNCTIONS PAGE: (showing just create_product function):
<?php
function create_product($product_name, $product_description, $image_temp, $image_ext) {
$product_name = mysql_real_escape_string(htmlentities($product_name));
$product_description = mysql_real_escape_string(htmlentities($product_description));
mysql_query("INSERT INTO products (product_id, member_id, timestamp, prodtitle, proddesc, image_temp, image_ext) VALUES ('','".$_SESSION['member_id']."', UNIX_TIMESTAMP(), '$product_name', '$product_description', '$image_temp', '$image_ext')");
mkdir('uploads/'.mysql_insert_id(), 0777);
mkdir('uploads/thumbs/'.mysql_insert_id(), 0777);
// place image & thumbnail in the respective folder
$bmid = mysql_insert_id();
$image_file = "$bmid."."jpg";
$location = "uploads/$bmid/$image_file";
move_uploaded_file($_FILES['fileField']['tmp_name'],$location);
create_thumb('uploads/'.$bmid.'/', $image_file, 'uploads/thumbs/'.$bmid.'/');
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此行是错误的:
应该如下所示:
请注意缺少单引号。
此外,您似乎根本没有在 CREATE_PRODUCT.PHP 中定义
$product_id
varThis line is wrong:
should look like this:
Please, note the absence of the single quotes.
Besides, it seems you have not defined the
$product_id
var at all in CREATE_PRODUCT.PHP我没有看到您在任何地方定义
$product_id
。错误页面的 URLproduct.php?product_id=
是否有可能在=
后面没有任何内容?I don't see you defining
$product_id
anywhere. By any chance is the URL of the error pageproduct.php?product_id=
with nothing after the=
?您应该对位置 uri 使用绝对路径。您还需要确保在发送标头之前没有其他输出发送到客户端。
来自 http://php.net/manual/en/function.header.php 的相关片段
You should use an absolute path for the location uri. You'll also need to ensure that no other output is sent to the client before the header is sent.
The relevant snip from http://php.net/manual/en/function.header.php