javascript - PHP 注册从返回到错误的 URL 而不是索引,数据库错误?

标签 javascript php html css mysql

我目前有一个登录系统,用户可以在其中以用户身份注册和登录。

当用户登录后,他们应该能够上传个人资料图片。

我遇到的问题是,当我按下“注册”按钮时,我将重定向到此 URL ( http://localhost/php51/login.php) 而不是此 ( http://localhost/php51/index.php)。

事情是,当用户注册后,用户应该在 index.php 上弹出并显示用户的默认个人资料图片。

相反,我在 ( http://localhost/php51/login.php) 上得到了一个空白页。

这是我的数据库,包含以下列:

我的数据库中有两个表,名为 (loginsystem)

这两个表是:

包含列(id、status、userid)的表“profileimg”

和包含列(user_email、user_id、user_name、user_password、user_phone、user_zip)的表“users”

这是我的代码:

INDEX.php

    <?php
session_start();
include_once 'dbh.php';
?>




<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
</head>

<body>


    <?php
// Check if there is users
    $sql = "SELECT * FROM users";
    $result = mysqli_query($conn, $sql);
    if(mysqli_num_rows($result) > 0) {
      while ($row = mysqli_fetch_assoc($result)) {
        $id = $row['user_id'];
        $sqlImg = "SELECT * FROM profileimg WHERE userid='$id'";
        $resultImg = mysqli_query($conn, $sqlImg);
        while ($rowImg = mysqli_fetch_assoc($resultImg)) {
          echo "<div>";
            if($rowImg['status'] == 0) {
              echo "<img src='uploads/profile".$id.".jpg'";

            } else {
              echo "<img src='uploads/profiledefault.jpg'";
            }
            echo $row['name'];
          echo "</div>";
        }

      }

    } else {
        echo "No registered users :";
    }


    //Her checker vi for om man er logget ind, 
    //og viser herefter upload FORM

      if (isset($_SESSION['id'])) {
        if ($_SESSION['id'] == 1) {
          echo " You are logged in!";
        }
        //If the user is logged in, we allow the user to upload a profile image
        echo "<form action = 'upload.php' method='POST' enctype = 'multipart/form-data'>
          <input type = 'file' name = 'file'>
          <button type = 'submit' name = 'submit'>UPLOAD FILE</button>
        </form>";
      } else {
        echo " You are not logged in!";

        echo "<form action='login.php' method='POST'>
        <input type='text' name='name' placeholder='Name'>
        <input type='text' name='phone' placeholder='phone'>
        <input type='text' name='email' placeholder='email'>
        <input type='text' name='zip' placeholder='zip'>
        <input type='password' name='password' placeholder='password'>
        <button type ='submit' name='submitSignup'>Signup</button>
      </form>";
      }
    ?>


    <!--We use HTML forms when we want to upload images or files-->
    <!--The form HAS to be set as a POST method, and it needs the "enctype" attribute, which specifies that the content we are submitting using the form is a file-->


    <p> Login as user </p>
    <form action="login.php" method="POST">
        <button type="submit" name="submitLogin"> Login  </button>
    </form>


    <p> Logout as user </p>
    <form action="logout.php" method="POST">
        <button type="submit" name = "submitLogout">Logout</button>
        </form>

</body>

</html>


LOUGOUT.php

    <?php

session_start();

session_unset();

session_destroy();

header("Location: index.php");


UPLOAD.php

    <?php
  //First we check if the form has been submitted
  if (isset($_POST['submit'])) {
    //Then we grab the file using the FILES superglobal
    //When we send a file using FILES, we also send all sorts of information regarding the file
    $file = $_FILES['file'];
    //Here we get the different information from the file, and assign it to a variable, just so we have it for later
    //If you use "print_r($file)" you can see the file info in the browser
    $fileName = $file['name'];
    $fileType = $file['type'];
    //The "tmp_name" is the temporary location the file is stored in the browser, while it waits to get uploaded
    $fileTempName = $file['tmp_name'];
    $fileError = $file['error'];
    $fileSize = $file['size'];

    //Later we are going to decide the file extensions that we allow to be uploaded
    //Here we are getting the extension of the uploaded file
    //First we split the file name into name and extension
    $fileExt = explode('.', $fileName);
    //Then we get the extention
    $fileActualExt = strtolower(end($fileExt));

    //Here we declare which extentions we want to allow to be uploaded (You can change these to any extention YOU want)
    $allowed = array("jpg", "jpeg", "png", "pdf");

    //First we check if the extention is allowed on the uploaded file
    if (in_array($fileActualExt, $allowed)) {
      //Then we check if there was an upload error
      if ($fileError === 0) {
        //Here we set a limit on the allowed file size (in this case 500mb)
        if ($fileSize < 500000) {
          //We now need to create a unique ID which we use to replace the name of the uploaded file, before inserting it into our rootfolder
          //If we don't do this, we might end up overwriting the file if we upload a file later with the same name
          //Here we create a unique ID based on the current time, meaning that no ID is identical. And we add the file extention type behind it.
          $fileNameNew = uniqid('', true).".".$fileActualExt;
          //Here we define where we want the new file uploaded to
          $fileDestination = 'uploads/'.$fileNameNew;
          //And finally we upload the file using the following function, to send it from its temporary location to the uploads folder
          move_uploaded_file($fileTempName, $fileDestination);
          //Going back to the previous page
          header("Location: index.php");
        }
        else {
          echo "Your file is too big!";
        }
      }
      else {
        echo "There was an error uploading your file, try again!";
      }
    }
    else {
      echo "You cannot upload files of this type!";
    }
  }


SIGNUP.php

    <?php

include_once 'dbh.php';

$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$zip = $_POST['zip'];
$password = $_POST['password'];


$sql = "INSERT INTO users (name, phone, email, zip, password) 
VALUES ('$name', '$phone', '$email', '$zip', '$password')";
mysqli_query($conn, $sql);

$sql = "SELECT * FROM users WHERE name = '$name' AND phone='$phone'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        $userid = $row['user_id'];
        $sql = "INSERT INTO profileimg (userid, status) 
            VALUES ('$userid', 1)";
            mysqli_query($conn, $sql);
            header("Location: index.php");


    }

} else {
    echo "Error";
}


DBH.PHP

    <?php

$conn = mysqli_connect("localhost", "root", "", "loginsystem");


LOGIN.PHP

    <?php

session_start();

if (isset($_POST['submitLogin'])) {
    $_SESSION['id'] = 1;

    header("Location: index.php");
}

最佳答案

在您的文件 index.php 中,您需要更改行

echo "<form action='login.php' method='POST'>

echo "<form action='signup.php' method='POST'>

现在您将注册数据发送到错误的地方

关于javascript - PHP 注册从返回到错误的 URL 而不是索引,数据库错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55241580/

相关文章:

javascript - 从编辑表单中的下拉列表中显示国家/地区 Laravel 5.2

javascript - 如何搜索由变量确定的特定数组?

javascript - 我想将 img src 内部链接更改为外部链接

php - php中文件上传的验证

html - Firefox 图片选择 bug CSS

javascript - .string之后的所有字符如何大写。像手机键盘

javascript - Javascript 的正则表达式不正确

php - 想出了空间用户搜索,但我如何按距离排序? (php laravel mysql)

php - 如何从 WooCommerce 子类别产品计数中删除括号

html - 使居中图像远离非居中父元素