php - "The page is not redirecting properly" firefox xampp php page failed to load
问题描述
Update Nov 11th
I tried to copy-paste the whole thing from tutorialrepublic, and only edited the directory to the error page and the landing page. Also, I created a table based on this code (which is employee [id, name, address, salary]) along with three rows of data.
So, here's how try-update.php looks now:
<?php
// Include config file
require_once "./assets/php/connecttodb.php";
// Define variables and initialize with empty values
$name = $address = $salary = "";
$name_err = $address_err = $salary_err = "";
// Processing form data when form is submitted
if(isset($_POST["id"]) && !empty($_POST["id"])){
// Get hidden input value
$id = $_POST["id"];
// Validate name
$input_name = trim($_POST["name"]);
if(empty($input_name)){
$name_err = "Please enter a name.";
} elseif(!filter_var($input_name, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
$name_err = "Please enter a valid name.";
} else{
$name = $input_name;
}
// Validate address address
$input_address = trim($_POST["address"]);
if(empty($input_address)){
$address_err = "Please enter an address.";
} else{
$address = $input_address;
}
// Validate salary
$input_salary = trim($_POST["salary"]);
if(empty($input_salary)){
$salary_err = "Please enter the salary amount.";
} elseif(!ctype_digit($input_salary)){
$salary_err = "Please enter a positive integer value.";
} else{
$salary = $input_salary;
}
// Check input errors before inserting in database
if(empty($name_err) && empty($address_err) && empty($salary_err)){
// Prepare an update statement
$sql = "UPDATE employees SET name=?, address=?, salary=? WHERE id=?";
if($stmt = mysqli_prepare($connect, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "sssi", $param_name, $param_address, $param_salary, $param_id);
// Set parameters
$param_name = $name;
$param_address = $address;
$param_salary = $salary;
$param_id = $id;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Records updated successfully. Redirect to landing page
header("location: /skripsi-manual/display-jenispembayaran.php");
exit();
} else{
echo "Something went wrong. Please try again later.";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($connect);
} else{
// Check existence of id parameter before processing further
if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
// Get URL parameter
$id = trim($_GET["id"]);
// Prepare a select statement
$sql = "SELECT * FROM employees WHERE id = ?";
if($stmt = mysqli_prepare($connect, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "i", $param_id);
// Set parameters
$param_id = $id;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
$result = mysqli_stmt_get_result($stmt);
if(mysqli_num_rows($result) == 1){
/* Fetch result row as an associative array. Since the result set contains only one row, we don't need to use while loop */
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
// Retrieve individual field value
$name = $row["name"];
$address = $row["address"];
$salary = $row["salary"];
} else{
// URL doesn't contain valid id. Redirect to error page
header("location: /skripsi-manual/try-errorpage.php");
exit();
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}
}
// Close statement
mysqli_stmt_close($stmt);
// Close connection
mysqli_close($connect);
} else{
// URL doesn't contain id parameter. Redirect to error page
header("location: /skripsi-manual/try-errorpage.php");
exit();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Update Record</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
.wrapper{
width: 500px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header">
<h2>Update Record</h2>
</div>
<p>Please edit the input values and submit to update the record.</p>
<form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
<div class="form-group <?php echo (!empty($name_err)) ? 'has-error' : ''; ?>">
<label>Name</label>
<input type="text" name="name" class="form-control" value="<?php echo $name; ?>">
<span class="help-block"><?php echo $name_err;?></span>
</div>
<div class="form-group <?php echo (!empty($address_err)) ? 'has-error' : ''; ?>">
<label>Address</label>
<textarea name="address" class="form-control"><?php echo $address; ?></textarea>
<span class="help-block"><?php echo $address_err;?></span>
</div>
<div class="form-group <?php echo (!empty($salary_err)) ? 'has-error' : ''; ?>">
<label>Salary</label>
<input type="text" name="salary" class="form-control" value="<?php echo $salary; ?>">
<span class="help-block"><?php echo $salary_err;?></span>
</div>
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<input type="submit" class="btn btn-primary" value="Submit">
<a href="index.php" class="btn btn-default">Cancel</a>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
And still, it redirects me straight to the error page. Could you show me what's wrong with this code and how to solve it?
If it helps, I've copy-edited the Read section in tutorialrepublic , and it works without much issue.
Thank you in advance.
I'm creating a page to update data on my database. Copy-edited the php code and <head>
from this page
PHP file and html form are in the same file. Except for the file to connect to database.
Here is the form (try-update.php):
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Update Record</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
.wrapper{
width: 500px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header">
<h2>Update Record</h2>
</div>
<?php
// Include config file
ini_set('display_errors', 1); error_reporting(E_ALL);
include_once './assets/php/connecttodb.php';
// Define variables and initialize with empty values
$id_jenispembayaran = $nama_jenispembayaran = "";
$id_jenispembayaran_err = $nama_jenispembayaran_err = "";
// Processing form data when form is submitted
if(isset($_POST["id_jenispembayaran"]) && !empty($_POST["id_jenispembayaran"])){
// Get hidden input value
$id = $_POST["id_jenispembayaran"];
// Validate name
$input_nama = trim($_POST["nama_jenispembayaran"]);
if(empty($input_nama)){
$nama_jenispembayaran_err = "Please enter a name.";
} elseif(!filter_var($input_nama, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
$nama_jenispembayaran_err = "Please enter a valid name.";
} else{
$nama_jenispembayaran = $input_nama;
}
// Check input errors before inserting in database
if(empty($nama_jenispembayaran_err)){
// Prepare an update statement
$sql = "UPDATE jenispembayaran SET nama_jenispembayaran=? WHERE id_jenispembayaran=?";
if($stmt = mysqli_prepare($connect, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "si", $param_nama_jenispembayaran, $param_id_jenispembayaran);
// Set parameters
$param_nama_jenispembayaran = $nama_jenispembayaran;
$param_id_jenispembayaran = $id_jenispembayaran;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Records updated successfully. Redirect to landing page
header("location: /skripsi-manual/display-jenispembayaran.php");
exit();
} else{
echo "Something went wrong. Please try again later.";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($connect);
} else{
// Check existence of id parameter before processing further
if(isset($_GET["id_jenispembayaran"]) && !empty(trim($_GET["id_jenispembayaran"]))){
// Get URL parameter
$id_jenispembayaran = trim($_GET["id_jenispembayaran"]);
// Prepare a select statement
$sql = "SELECT * FROM jenispembayaran WHERE id_jenispembayaran = ?";
if($stmt = mysqli_prepare($connect, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "i", $param_id_jenispembayaran);
// Set parameters
$param_id_jenispembayaran = $id_jenispembayaran;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
$result = mysqli_stmt_get_result($stmt);
if(mysqli_num_rows($result) == 1){
/* Fetch result row as an associative array. Since the result set contains only one row, we don't need to use while loop */
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
// Retrieve individual field value
$id_jenispembayaran = $row["id_jenispembayaran"];
$nama_jenispembayaran = $row["nama_jenispembayaran"];
} else{
// URL doesn't contain valid id. Redirect to error page
header("location: /skripsi-manual/try-update.php");
exit();
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}
}
// Close statement
mysqli_stmt_close($stmt);
// Close connection
mysqli_close($connect);
} else{
// URL doesn't contain id parameter. Redirect to error page
header("location: /skripsi-manual/try-update.php");
echo "somethings's wrong";
exit();
}
}
?>
<p>Please edit the input values and submit to update the record.</p>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
<label for="id_jenisPembayaran">Kode Jenis Pembayaran:</label>
<input type="text" name="id_jenisPembayaran" value="<?php echo $id_jenisPembayaran;?>">
<br>
<br>
<label for="nama_jenisPembayaran">Nama Jenis Pembayaran:</label>
<input type="text" name="nama_jenisPembayaran" value="<?php echo $nama_jenisPembayaran;?>">
<br>
<br>
<button type="submit" value="submit">Masukkan</button>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
And this is my connection file (connecttodb.php):
<?php
/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$serverName = 'localhost';
$dbUserName = 'root';
$dbPassword = '';
$dbName = 'sip-krl';
/* Attempt to connect to MySQL database */
$connect = mysqli_connect($serverName, $dbUserName, $dbPassword, $dbName);
// Check connection
if($connect === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
This form is supposed to select one jenispembayaran based on the id (which is id_jenispembayaran), puts it on the so users could edit it, and when they click "Submit", the page will change the data on the database, then redirects to display-jenispembayaran.php, where all jenispembayaran is displayed. If something went wrong, it should reload try-update.php and write what the error is.
Problem is, with this code, all I got is either this:
The page isn’t redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
This problem can sometimes be caused by disabling or refusing to accept cookies.
Or, when I change the header() in this:
} else{
// URL doesn't contain id parameter. Redirect to error page
header("location: /skripsi-manual/try-update.php");
echo "somethings's wrong";
exit();
}
}
?>
into this:
} else{
// URL doesn't contain id parameter. Redirect to error page
header("location: /skripsi-manual/display-jenispembayaran.php");
echo "somethings's wrong";
exit();
}
}
?>
This form directs me to display-jenispembayaran.php, or other pages I typed in. No echo 'something's wrong' whatsoever in display-jenispembayaran.php
After several attempts and observations, I concluded the error is that my page doesn't contain id parameter (do please correct me if I'm wrong). So, I tried to give id parameters by changing the php code into this:
// Processing form data when form is submitted
if(isset($_POST["id_jenispembayaran"]) && !empty($_POST["id_jenispembayaran"])){
// Get hidden input value
$id = $_POST["id_jenispembayaran"];
// Validate name and id
$input_id = trim($_POST["id_jenispembayaran"]);
if(empty($input_id)){
$id_jenispembayaran_err = "Please enter an id.";
} elseif(!filter_var($input_id, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[0-9a-zA-Z\s]+$/")))){
$id_jenispembayaran_err = "Please enter a valid id.";
} else{
$id_jenispembayaran = $input_id;
}
$input_nama = trim($_POST["nama_jenispembayaran"]);
if(empty($input_nama)){
$nama_jenispembayaran_err = "Please enter a name.";
} elseif(!filter_var($input_nama, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
$nama_jenispembayaran_err = "Please enter a valid name.";
} else{
$nama_jenispembayaran = $input_nama;
}
// Check input errors before inserting in database
if(!empty($nama_jenispembayaran_err)){
// Prepare an update statement
$sql = "UPDATE jenispembayaran SET nama_jenispembayaran=? WHERE id_jenispembayaran=?";
if($stmt = mysqli_prepare($connect, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "si", $param_nama_jenispembayaran, $param_id_jenispembayaran);
// Set parameters
$param_nama_jenispembayaran = $nama_jenispembayaran;
$param_id_jenispembayaran = $id_jenispembayaran;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Records updated successfully. Redirect to landing page
header("location: /skripsi-manual/try-update.php");
exit();
} else{
echo "Something went wrong. Please try again later.";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($connect);
} else{
// Check existence of id parameter before processing further
if(isset($_GET["id_jenispembayaran"]) && !empty(trim($_GET["id_jenispembayaran"]))){
// Get URL parameter
$id_jenispembayaran = trim($_GET["id_jenispembayaran"]);
// Prepare a select statement
$sql = "SELECT * FROM jenispembayaran WHERE id_jenispembayaran = ?";
if($stmt = mysqli_prepare($connect, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "is", $param_id_jenispembayaran, $param_nama_jenispembayaran);
// Set parameters
$param_id_jenispembayaran = $id_jenispembayaran;
$param_nama_jenispembayaran = $nama_jenisPembayaran;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
$result = mysqli_stmt_get_result($stmt);
if(mysqli_num_rows($result) == 1){
/* Fetch result row as an associative array. Since the result set contains only one row, we don't need to use while loop */
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
// Retrieve individual field value
$id_jenispembayaran = $row["id_jenispembayaran"];
$nama_jenispembayaran = $row["nama_jenispembayaran"];
} else{
// URL doesn't contain valid id. Redirect to error page
header("location: /skripsi-manual/try-update.php");
exit();
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}
}
Still, it redirects me back to display-jenispembayaran.php.
I also tried placing ini_set('display_errors', 1); error_reporting(E_ALL);
above the <html>
tag. It doesn't give me any errors.
Anyone could show me what went wrong and how to solve them?
Thanks in advance.
解决方案
推荐阅读
- python-3.x - Unintended dtype conversion leads to uncastable array
- inno-setup - 无法获取 [图标] 名称参数以识别 [设置] 部分中设置的名称
- javascript - 渲染异步数据,从 firebase 检索
- jquery - 为什么我的所有 Bootstrap 4 轮播链接都不起作用?
- javascript - 如何将两个原生 JavaScript BigInt 相除并得到十进制结果
- c# - Unity 动画列表
- javascript - How to fix "Error: Reducers may not dispatch actions." in my ReactJS Redux app?
- python - Django CBV UpdateView not saving form to database. (Django 2.0)
- html - 如何使背景颜色与图像相同?
- sql-server - 基于 WHERE 值的不同 SQL 执行计划