Click here to Skip to main content
15,881,084 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<?php

@include 'config.php';

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

    $name = mysqli_real_escape_string($conn,$_POST['name']);
    $username = mysqli_real_escape_string($conn,$_POST['username']);
    $pass = md5($_POST['password']);
    $cpass = md5($_POST['cpassword']);
    $email = mysqli_real_escape_string($conn,$_POST['email']);
    $phone = mysqli_real_escape_string($conn,$_POST['phoneno']);


    $select =" Select * FROM user_form WHERE email = '$email' && password = '$pass' ";

    $result = mysqli_query($conn, $select);

    if(mysqli_num_rows($result)>0){
        
        $error[]=' users already exist!';
    }

    else{
        if($pass !=$cpass)
        {
            $error[]='Password does not match'; 
        }
        else{
            $insert="INSERT INTO user_form(name,username,password,email,phoeneno)VALUES ('$name','$username','$pass','$email','$phone')";
            mysqli_query($conn,$insert);
            header('location:login.php');
        }
    }

};


?>


html
<input type="text" name="name" placeholder="entet your name">
<input type="text" name="username" placeholder="entet your username">
<input type="password" name="password" placeholder="entet your password">
<input type="password" name="cpassword" placeholder="confirm your password">
<input type="tel" name="phoneno" placeholder="entet your phone number">
<input type="text" name="email" placeholder="entet your email">

<input type="submit" name="submit" value="Sign up now" class="form-btn">
<p>already have an account?<a href="login.php">Login Now</a></p> 


config.php
<?php 
$conn =mysqli_connect('localhost','root','','travel_db');
?>


What I have tried:

i am just a newbie for php and i would like to ask all the master hereee, may i ask which of my code is wrong, becuase after i press sign up button , it direct to my login page but doesn't save my data into database :(
Posted
Updated 23-Jul-22 23:55pm
v2
Comments
Richard MacCutchan 24-Jul-22 7:30am    
Do not use md5 to create password hashes; it is well known to be unreliable. PHP provides full featured secure hashing: PHP: Password Hashing Functions - Manual[^].

1 solution

So little code, so many major problems ...

Let's start with what you haven't seen: SQL Injection.
Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Then there is the Data Protection problem.
Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]

And remember: if this is web based and you have any European Union users then GDPR applies and that means you need to handle passwords as sensitive data and store them in a safe and secure manner. Text is neither of those and the fines can be .... um ... outstanding. In December 2018 a German company received a relatively low fine of €20,000 for just that.

Put the two together so I don't even have to be logged in and you've got real problems ... I can bypass your security to login as anyone, and do exactly what I like with your data.

Then ... I can create a user with the same username as any existing user provided I use a different password ...

And as the coup de gras, you expose your DB access details including login name and password in your HTML of anyone who knows how to press F12 or CTRL+U can see it - ignoring that it's hardcoded into your app so you can't test it before release without trying it out on the production DB.

And that's before we get to the problem you have noticed!
And that's probably because your column names are also MySql reserved words so they can't be used without escape characters: NAME and PASSWORD both mean something specific to mySql already.
MySQL :: MySQL 8.0 Reference Manual :: 9.3 Keywords and Reserved Words[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900