“页面未正确重定向” firefox xampp php页面无法加载

时间:2020-11-03 09:27:52

标签: php html mysql xampp

11月11日更新

我试图从tutorialrepublic复制并粘贴整个内容,仅将目录编辑到错误页面和登录页面。另外,我根据此代码(雇员[id,姓名,地址,薪水] )和三行数据创建了一个表。

因此,下面是 try-update.php 的外观:

<?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>

尽管如此,它仍将我直接重定向到错误页面。 您能告诉我这段代码有什么问题以及如何解决吗?

如果有帮助,我已经对tutorialrepublic中的 Read 部分进行了编辑编辑,并且可以正常使用。

谢谢。


我正在创建一个页面来更新数据库中的数据。从this page复制编辑的php代码和<head>

PHP文件和html表单位于同一文件中。除了文件要连接到数据库。

以下是表单(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>

这是我的连接文件(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());
}

该表单应根据ID(即 id_jenispembayaran )的ID选择一个 jenispembayaran ,并将其放在用户可以对其进行编辑的位置,然后单击“提交”,页面将更改数据库上的数据,然后重定向到 display-jenispembayaran.php ,其中显示所有 jenispembayaran 。如果出现问题,应重新加载 try-update.php 并写出错误所在。

问题是,有了这段代码,我得到的只是这个问题:

页面无法正确重定向
Firefox检测到服务器正在以永远无法完成的方式重定向对该地址的请求。
有时可能是由于禁用或拒绝接受Cookie引起的。

或者,当我在其中更改header()时:

} else{
    // URL doesn't contain id parameter. Redirect to error page
   header("location: /skripsi-manual/try-update.php");
   echo "somethings's wrong";
   exit();
   }
  }
?>

对此:

} else{
    // URL doesn't contain id parameter. Redirect to error page
   header("location: /skripsi-manual/display-jenispembayaran.php");
   echo "somethings's wrong";
   exit();
   }
  }
?>

此表单将我定向到 display-jenispembayaran.php 或其他我键入的页面。在 display-jenispembayaran中没有回声“出了点问题” 。 php

经过几次尝试和观察,我得出的错误是我的页面不包含 id参数(如果我输入错了,请更正我)。因此,我尝试通过将php代码更改为id来赋予id参数:

// 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.";
   }
  }

还是,它将我重定向回 display-jenispembayaran.php 。 我还尝试将ini_set('display_errors', 1); error_reporting(E_ALL);标记上方放置<html>。它不会给我任何错误。

任何人都可以告诉我哪里出了问题以及如何解决这些问题?

谢谢。

0 个答案:

没有答案