-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!

  • 1
    use this in your else statement: $con->error. Specifically replace echo "Not Created!" with echo $con->error what error comes back? – Arnolio Dec 14 '16 at 18:12
  • execute the query mysql_query($sql); and then check the condition – Soniya Basireddy Dec 14 '16 at 18:14
  • _"Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers."_ http://stackoverflow.com/help/mcve – WillardSolutions Dec 14 '16 at 18:15
  • @SoniyaReddy mysql_ with mysqli_ ? doubt that will work. – Funk Forty Niner Dec 14 '16 at 18:15
  • 1
    @RajdeepPaul no, nothing wrong with their query, they didn't choose a database, that's why,. – Funk Forty Niner Dec 14 '16 at 18:15
  • 1
    Mohammad, you didn't choose a database; that's why your code is failing. – Funk Forty Niner Dec 14 '16 at 18:16
  • can everyone here not give off false information? – Funk Forty Niner Dec 14 '16 at 18:16
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! [Don't believe it?](http://stackoverflow.com/q/38297105/1011527) – Jay Blanchard Dec 14 '16 at 18:18
  • 1
    **Never store plain text passwords!** Please use PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). Make sure you ***[don't escape passwords](http://stackoverflow.com/q/36628418/1011527)*** or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard Dec 14 '16 at 18:18
  • @Fred-ii- Can these keywords be used in the query like *that*? – Rajdeep Paul Dec 14 '16 at 18:18
  • 1
    @RajdeepPaul yes they can and you need to retract your duplicate. – Funk Forty Niner Dec 14 '16 at 18:19
  • @Fred-ii- Retracted *close vote* and deleted the comment as well. Thanks for your input. :-) – Rajdeep Paul Dec 14 '16 at 18:20
  • @RajdeepPaul you're welcome. I deleted mine also. – Funk Forty Niner Dec 14 '16 at 18:22

1 Answers1

-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

  • 1
    [I said this already, didn't I?... *"Mohammad, you didn't choose a database; that's why your code is failing"*](http://stackoverflow.com/questions/41149284/insert-data-to-database-with-php?noredirect=1#comment69501135_41149284) – Funk Forty Niner Dec 14 '16 at 18:59