-1

I want to insert a data in database by this php code:

<?php
/*if(isset($_POST['username'])&&isset($_POST['password'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];*/
    $DBS="localhost";// Database server
    $DBU="pb";
    $DBP="******";
    $con= new mysqli($DBS,$DBU,$DBP);
    if($con->connect_error)
        die("Connection failed: " . $con->connect_error);
    else{
        echo "Connected Sucssefully!";
        $sql = "INSERT INTO users (username, password, name)
        VALUES ('JJ', 'Doe', 'John')";
        if($con->query($sql)===TRUE)
            echo "User Created";
        else
            echo "Not Created!";}
//}
?>

but I get "Connected Sucssefully!Not Created!"! Idon't know why!

13
  • 1
    use this in your else statement: $con->error. Specifically replace echo "Not Created!" with echo $con->error what error comes back? Commented Dec 14, 2016 at 18:12
  • 1
    @RajdeepPaul no, nothing wrong with their query, they didn't choose a database, that's why,. Commented Dec 14, 2016 at 18:15
  • 1
    Mohammad, you didn't choose a database; that's why your code is failing. Commented Dec 14, 2016 at 18:16
  • 1
    Never store plain text passwords! Please use PHP's built-in functions to handle password security. If you're using a PHP version less than 5.5 you can use the password_hash() compatibility pack. Make sure you don't escape passwords or use any other cleansing mechanism on them before hashing. Doing so changes the password and causes unnecessary additional coding. Commented Dec 14, 2016 at 18:18
  • 1
    @RajdeepPaul yes they can and you need to retract your duplicate. Commented Dec 14, 2016 at 18:19

1 Answer 1

-1

Thank you ! Specially @Arnolio . I used $con->error and it gave me "No database selected " and this error solved my problem ! I did not specify any database name! Regaards

Sign up to request clarification or add additional context in comments.

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.