Neew help to send data to my database mysql xampp

#1

here is my process.php file

$username = $_POST['name'];
$password = $_POST['password'];

if(!empty($username) || !empty($password)){
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "tut";

//connection
$conn = new mysqli($servername, $username, $password, $dbname);
if(mysqli_connect_error()){
    die ('connect Error('. mysqli_connect_error().')'. mysqli_connect_error());
}else {
    $SELECT = "SELECT username From register Where username = ? Limit 1";
    $INSERT = "INSERT Into register (username, password) values(?, ?)";
}

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

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

    $stmt = $conn->prepare($INSERT);
    $stmt->bind_param("ss", $username, $password);
    $stmt->execute();
    echo "New record inserted succesfully";
}else{
    echo "someone has already used this username";
}

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

}else{
    echo "All fields are required";
    die();

}

and here is my index.php

<?php
    require "header.php"
?>



    <button action="process.php" method="POST">
        Klicka för att lägga in en produkt
    </button>
    
    <form action="process.php" method="POST">
        <input type="text" name="name" placeholder="name">
        <input type="password" name="password" placeholder="password">
        <input type="submit">
    </form>


<?php
    require "footer.php"
?>
#2

The whole logic is off. You don’t check for a username first. Set a unique constraint on the username column, attempt the insert and catch the duplicate error if any. Do not create variables for nothing. Do not output internal system errors to the user. I would also recommend you use PDO.

!! DO NOT USE PLAINTEXT PASSWORDS !!

FYI: The limit one is pointless. There should only every be one unique username, therefore there will always only be one result.

#3

You have a thread on this forum 9 days ago for a registration form and form processing code - Help with an error in my signup form?

I recommend that you reread (translating as necessary using google translator) the detailed reply you got. The forum members are not going to repeatedly look at code that contains the same problems you have already been made aware of.

There’s a new problem in the above code. You are reusing the $username and $password variables for two different things, which will prevent your code doing anything useful. If you are not aware from one point in your to code to few lines later what your variables are and what you are using them for, you won’t be able to program.

Also, your - if(!empty($username) || !empty($password)){ logic is wrong. You would want to use && (and) so that you only run the rest of the form processing code if both variables are not empty.