我绊倒了这一块。在提交创建产品后,我无法立即将此代码重定向到其产品页面。我有三个文件create_product.php | product.php | &安培;一个外部函数页面,我正在调用一个函数。我似乎无法工作的是:header(“location:product.php?product_id ='。$ product_id。'”);功能。所有的东西都正确地输入到数据库中,但是当用户点击提交来创建产品时,我得到“该产品不存在”错误。任何帮助将不胜感激(我知道有安全性等问题...目前不担心这些问题,而只是获得“创建产品提交按钮”将用户重定向到新创建的产品页面..抱歉低响应率(作为新用户已经引起我的注意......我将开始接受所有答案)。非常感谢您提供任何帮助,
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页面:
<?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页面中回显诸如$ prodtitle等变量
最后包含的产品功能页面:(仅显示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.'/');
}
?>
答案 0 :(得分:2)
这一行错了:
header("location: product.php?product_id='.$product_id.'");
应如下所示:
header("Location: product.php?product_id=$product_id");
请注意,没有单引号。
此外,您似乎还没有在CREATE_PRODUCT.PHP中定义$product_id
var
答案 1 :(得分:1)
我没有看到你在任何地方定义$product_id
。错误页面product.php?product_id=
的网址是=
答案 2 :(得分:0)
您应该使用位置uri的绝对路径。您还需要确保在发送标头之前没有其他输出发送到客户端。
来自http://php.net/manual/en/function.header.php
的相关剪辑<?php
/* Redirect to a different page in the current directory that was requested */
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
?>