简体   繁体   English

我正在尝试做一个在数据库(MySQL)中注册用户的注册表

[英]I am trying to do a registration form that registers a user in a database (MySQL)

Question:I am trying to do a registration form that registers a user in a database (MySQL).问题:我正在尝试制作一个在数据库 (MySQL) 中注册用户的注册表。
The code is supposed to register: - username - password - first name - last name - email该代码应该注册: - 用户名 - 密码 - 名字 - 姓氏 - 电子邮件

And I am getting errors like this:我收到这样的错误:

Notice: Undefined index: fname in C:\xampp\htdocs\Railways\reg.php on line 89

Notice: Undefined index: lname in C:\xampp\htdocs\Railways\reg.php on line 90

Notice: Undefined index: uname1 in C:\xampp\htdocs\Railways\reg.php on line 91

Notice: Undefined index: pswrd1 in C:\xampp\htdocs\Railways\reg.php on line 92

Notice: Undefined index: email in C:\xampp\htdocs\Railways\reg.php on line 93

Fatal error: Call to a member function query() on null in C:\xampp\htdocs\Railways\reg.php on line 98

Here is the code:这是代码:

<?php
    global $conn;
    include_once('connect.php');
        $first=$_POST['fname'];
        $last=$_POST['lname'];
        $usernam=$_POST['uname1'];
        $passwrd=$_POST['pswrd1'];
        $email=$_POST['email'];
    $sql=" INSERT INTO users(username,password,firstname,lastname,email) VALUES ('".$usernam."','".$passwrd."','".$first."','".$last."','".$email."')";
    if($conn->query($sql))
    {
        echo "<h1 style='text-align:center'> Registered Succesfully </h1>";
    }
    else
    {
        echo "<h1 style='text-align:center' id='text'>Error</h1>";
        echo $conn->connect_error;
    }
?>

And here is the HTML:这是 HTML:

<form  action="reg.php" method="POST">
    <label id="text"> Firstname: <br> <br> <input id="input" type="text" name="fname" placeholder="Firstname" id="t3"> </label>
    <br> <br> <br>
    <label id="text"> Lastname: <br> <br> <input id="input" type="text" name="lname" placeholder="Lastname" id="t4"> </label>
    <br> <br> <br>

    <label id="text"> Username: <br> <br> <input id="input" type="text" name="uname1" placeholder="Username" id="t1"> </label>
    <br> <br> <br>

    <label id="text"> Password: <br> <br> <input id="input" type="text" name="pswrd1" placeholder="Password" id="t2"> </label>
    <br> <br> <br>

    <label id="text"> Email: <br> <br> <input id="input" type="text" name="email" placeholder="Email" id="t1"> </label>
    <br> <br> <br>

    <input style="background-color:red;padding:7px;margin-left:40px;"type="submit" value="Register" name="submit" id="s1">

    </td></tr>
    </table>
    </div>
</form>

connect.php code连接.php代码

    <?php

          $conn_error="could not connect";
          $host="localhost";
          $user="root";
          $pass="";
          $db="railways";
        $sql =mysqli_connect($host,$user,$pass,$db) or die($conn_error);
        echo'conneced';

?> ?>

Add a check if post submit is set then run your query otherwise if you open this page directly $_POST variable is empty and reason of you get error is your $_POST array is empty.添加检查是否设置了 post submit 然后运行您的查询,否则如果您直接打开此页面$_POST变量为空并且您收到错误的原因是您的$_POST数组为空。

<?php


include_once('connect.php');
global $conn;
if(isset($_POST['submit'])) // this check
{
    $first=$_POST['fname'];
    $last=$_POST['lname'];
    $usernam=$_POST['uname1'];
    $passwrd=$_POST['pswrd1'];
    $email=$_POST['email'];


$sql=" INSERT INTO users(username,password,firstname,lastname,email) VALUES ('".$usernam."','".$passwrd."','".$first."','".$last."','".$email."')";

if($conn->query($sql))
  {
    echo "<h1 style='text-align:center'> Registered Succesfully </h1>";

  }
else
  {
    echo "<h1 style='text-align:center' id='text'>Error</h1>";
    echo $conn->connect_error;
  }
}
?>

You need to define those values to something if $_POST is not set.如果未设置$_POST您需要将这些值定义为某些内容。

Something like this:像这样的东西:

$first=isset($_POST['fname']) ? $_POST['fname'] : "" ;
$last=isset($_POST['lname']) ? $_POST['lname'] : "";
$usernam=isset($_POST['uname1']) ? $_POST['unmae1'] : "";
$passwrd=isset($_POST['pswrd1']) ? $_POST['pswrd1'] : "";
$email=isset($_POST['email']) ? $_POST['email'] : "";

if(isset($_POST['submit'])){
  $sql=" INSERT INTO users(username,password,firstname,lastname,email) VALUES ('".$usernam."','".$passwrd."','".$first."','".$last."','".$email."')";

 if($conn->query($sql)){
    echo "<h1 style='text-align:center'> Registered Succesfully </h1>";
}
else{
    echo "<h1 style='text-align:center' id='text'>Error</h1>";
    echo $conn->connect_error;}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试为注册的每个用户创建一个数据库表。 我究竟做错了什么? - I'm trying to create a database table for each user who registers. What am I doing wrong? 我正在尝试将复杂复选框表单的值存储在 mysql 数据库中 - I am trying to store the values of a complex checkbox form in mysql database 用户以PHP形式注册后该怎么办? - What do I do after the user registers in PHP form? 我正在尝试通过验证创建此登录/注册表单 - I am trying to create this login/registration form with validation 我正在尝试在用户注册后登录用户,但我不断收到 java.lang.NullPointerException - i am trying to login a user after the user registers but i keep getting java.lang.NullPointerException 我正在尝试将表单信息存储在mysql数据库中,但是它不适合该数据库 - I am trying to store form information in a mysql database but it does not foward to the database 提交按钮不起作用。 我有一个 mysql 数据库,我正在尝试制作 php 表格 - submit button is not working. i have a mysql database and i am trying to make a php form 尝试进行注册以在 mysql 中显示 - trying to do registration to display in mysql 我是PHP + MySql的新手,并尝试编写一个简单的脚本从数据库中删除用户,但是我没有摆脱错误 - I am new to PHP+MySql,and trying to write a simple script that will delete a user from the database,,but i am not getting rid of a error 我正在尝试从mysql下载文件,我将文件位置保存在数据库中,现在我正在尝试为页面上的用户创建下载链接 - I am trying to download a file from mysql, I saved the file location in the database, now I am trying to make a download link for the user on my page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM