我遇到的问题是我从一个页面传递到另一个页面。它到达好了,在开始时我可以得到值,但是点击我新页面上的按钮确认值已经消失。
我首先像这样设置值:
echo '<a href="removeproduct.php?product_id=' . $_GET ['product_id'] . '">';
在我的新页面上,我看到地址正确无误:
www.mysite.com/removeproduct.php?product_id=11
我使用echo显示product_id值,但没关系,但是在点击确认按钮后,我的网址丢失了?product_id 部分。
我想我的问题相当愚蠢和简单,但我完全是HTML和PHP的新手,并且在这个问题上已经失去了几个小时。
新页面中的代码(removeproduct.php)是这样的:
echo 'Value '. $_GET ['product_id'];
if (isset ( $_POST ['confirmar'] )) {
// Lets find the product
// Connect to the database
$dbc = mysqli_connect ( DB_HOST, DB_USER, DB_PASSWORD, DB_NAME );
// Grab the product data from the data base
$query = "SELECT user_id FROM product WHERE product_id = '" . $_GET ['product_id'] . "'";
echo 'value 2 '. $_GET ['prod_id']; **// value here is NULL!!!**
$data = mysqli_query ( $dbc, $query );
if (mysqli_num_rows ( $data ) == 1) {
// The product row was found so display the user data
$row = mysqli_fetch_array ( $data );
// Lets check the product belong to the user
if ($_SESSION ['user_id'] == $row ['user_id']) {
// If it belongs and user is allowded to erase a product
$query = "DELETE FROM product WHERE product_id = '" . $_GET ['product_id'] . "'";
$data = mysqli_query ( $dbc, $query );
$home_url = 'http://' . $_SERVER ['HTTP_HOST'] . dirname ( $_SERVER ['PHP_SELF'] `) . '/dashboard.php';`
// Confirm success with the user
echo "<script>setTimeout(\"location.href ='" . $home_url . "';\",2000);</script>";
echo '¡Tus datos han sido guardados correctamente!';
} else {
echo '<p class="error">No tiene autorización para acceder a ese producto. </p>';
}
} // End of check for a single row of user results
else {
echo '<p class="error">Tuvimos un error al acceder al producto. </p>';
}
}else if (isset ( $_POST ['cancelar'] )) {
$home_url = 'http://' . $_SERVER ['HTTP_HOST'] . dirname ( $_SERVER ['PHP_SELF'] ) . '/dashboard.php';
// Confirm success with the user
echo "<script>setTimeout(\"location.href ='" . $home_url . "';\",2000);</script>";
echo '¡Se han descartado los cambios!';
}
mysqli_close ( $dbc );
?>
<form enctype="multipart/form-data" method="post"
action="<?php echo $_SERVER['PHP_SELF']; ?>">
<button name="confirmar" value="confirmar" type="submit">Confirmar</button>
<button name="cancelar" value="cancelar" type="submit">Cancelar</button>
</form>
拜托,如果有人可以帮助我,我将不胜感激。
谢谢,
答案 0 :(得分:0)
不是制作表单操作$SERVER['PHP_SELF']
,而是将其设为$_SERVER['REQUEST_URI']
像
<form enctype="multipart/form-data" method="post"
action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<button name="confirmar" value="confirmar" type="submit">Confirmar</button>
<button name="cancelar" value="cancelar" type="submit">Cancelar</button>
</form>