【问题标题】:php Create account page with not redirect to index.htmlphp 创建不重定向到 index.html 的帐户页面
【发布时间】:2019-03-24 17:56:00
【问题描述】:

此网站适用于一家航运公司,用户在该网站上创建账户并获得 在另一个国家有点虚拟邮箱。当用户创建帐户时,他的信息应该被发送到数据库,并且他应该被重定向到dashboard.html,但是当创建帐户表单被填写并按下提交按钮时,用户应该被重定向到索引.html 但页面不会重定向。并且即使使用php错误报告也没有错误请帮助。

   <?php
   session_start();

   ini_set('display_errors', 'On');
   error_reporting(E_ALL | E_STRICT);

    error_reporting(E_ALL);
    ini_set('display_errors', 1);


       if(isset($_POST['submit'])){ 

         //check if variables are empty

     if (!empty($firstname) || !empty($lastname) || !empty($email) || 
         !empty($confirmemail) ||
         !empty($password) || !empty($confirmpassword)  || 
         !empty($phonenumber)    || !empty($address)  || !empty($city)  || 
         !empty($parish)  || !empty($trn)  ) 

        {
          $firstname = $_POST['firstname'];
           $lastname = $_POST['lastname'];
           $email= $_POST['email'];
           $confirmemail = $_POST['confirmemail'];
           $password = $_POST['password'];
           $confirmpassword = $_POST['confirmpassword'];
           $phonenumber = $_POST['phonenumber'];
           $address= $_POST['address'];
           $city = $_POST['city'];
           $parish = $_POST['parish'];
           $trn = $_POST['trn'];


       $DB_NAME= "ship2yaad";
       $DB_USER="root";
        $DB_PASSWORD="";
        $DB_HOST= "localhost";

    //create connection

     $conn= new mysqli("$DB_HOST", "$DB_USER", "$DB_PASSWORD","$DB_NAME");


   if (mysqli_connect_error()) {
   die('Connect Error('. mysqli_connect_errno().')'.mysqli_connect_error());
    } 



     $SELECT = "SELECT email From usersdb Where email = ? Limit 1";
     $INSERT = "INSERT Into usersdb (firstname , lastname , email 
       ,confirmemail , password , confirmpassword , phonenumber, address, 
         city, parish ,trn) 
  values(?,?,?,?,?,?,?,?,?,?,?)";
 //Prepare statement
   # code...



    $stmt = $conn->prepare($SELECT);
    $stmt->bind_param("s", $email);
    $stmt->execute();
    $stmt->bind_result($email);
    $stmt->store_result();
   $rnum = $stmt->num_rows;


    if ($rnum==0) {
        $stmt->close();
        $stmt = $conn->prepare($INSERT);

  // prepare and bind
     $stmt->bind_param("ssssssisssi", $firstname, $lastname, $email, 
      $confirmemail, $password, $confirmpassword ,$phonenumber, 
      $address, $city, $parish , $trn); 
     //binds the parameters to the SQL query and tells the database what the 
    parameters are
      $stmt->execute();

    $stmt->close();
    $conn->close();



        header('Location:dashboard.html');
        }



    ?>
     the database credentials are correct im 100% sure of it. PLus im not getting a sql connect error. 

【问题讨论】:

  • 尝试添加空格,例如header('Location: dashboard.html');
  • 我无意中发布了此代码 非常抱歉,真实代码是作为答案发布的

标签: php database html redirect header


【解决方案1】:

您的代码中存在一些问题;

1.重复定义

   ini_set('display_errors', 'On');   // useless because its overwritten
   error_reporting(E_ALL | E_STRICT);  //  uselesss again

   ini_set('display_errors', 1);   // repeat
   error_reporting(E_ALL);   // repeat

不要重复。选择一个并坚持下去。

2.循环关闭不当
很多右括号不存在,导致更难调试。


3.问题的根源。 代码 cmets 的错误使用。

//Prepare statement
# code...

单行 cmets 以 // 开头,顾名思义,应该在同一行结束。
代码中的# code(上面的 sn-p)滑入不同的行。
如果您更愿意使用多行 cmets,正确的语法是

/* Multi
   Line
   Comment */

这里也会出现这种情况↓

//binds the parameters to the SQL query and tells the database what the 
    parameters are

注意代码颜色的不同↑


我的建议
使用提供 PHP 支持和语法高亮显示的 IDE。
Atom、Visual Studio Code、PhpStorm 等

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多