-1
<?php
include_once("dbconnect.php");
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];`enter code here`
$password = $_POST['password'];

$sqlregister = "INSERT INTO USER(NAME,EMAIL,PHONE,PASSWORD) VALUES('$name','$email','$phone','$password')";

if($conn->query($sqlregister) === TRUE){
    echo "success";
}else{
    echo "failed";
}
?> 

dont know what wrong with it, keep echo failed. the same code work for my friend

X unknown
  • 11
  • 5
  • 1
    Please note that the way you're building your query is unsafe. You're open to [SQL injection](https://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work). You should use [prepared statements](https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) or [PDO](https://www.php.net/manual/en/book.pdo) instead. – El_Vanja Dec 12 '20 at 16:57
  • 1
    **Never store passwords as plain text**, use PHP's built-in [`password_hash`](https://www.php.net/manual/en/function.password-hash) and [`password_verify`](https://www.php.net/manual/en/function.password-verify) to safely store and check passwords. – El_Vanja Dec 12 '20 at 16:57
  • Ultimately, here's a thorough [guide](https://stackoverflow.com/a/22662582/4205384) on debugging `mysqli` related problems. It should help you identify the error your code is throwing. – El_Vanja Dec 12 '20 at 16:58
  • I'm new to this, i'll change it later for now can u tell me why it keep echo failed – X unknown Dec 12 '20 at 16:59
  • 1
    I can't know that. I have no idea what kind of values you're trying with or if all your column names are correct. Please read the guide and attempt to find the error. – El_Vanja Dec 12 '20 at 17:02
  • And it's very possible that one reason it's failing is precisely because you built the queries in the wrong way. But first you'll need to debug and find the specific error – ADyson Dec 12 '20 at 17:28
  • If you're using PDO, `$conn->query()` will not return a boolean `true` but an object for a successful query. Your query will succeed, but your code will still echo `failed`. – rickdenhaan Dec 13 '20 at 16:10

2 Answers2

1

if your db connection problem use this code dbconnect.php:

<?php
$hostname='localhost';
$username='root';
$password='';
$dbname='your dbname';
$conn=mysqli_connect($hostname,$username,$password,$dbname);
if (!$conn) {
    echo "not connected";
    
}
?>
1

When your database action fails, you should first check the errors that the DB gives. In this case you can see the error like this:

if ($conn->query($sqlregister)) {
    echo "success";
} else {
    printf("Error message: %s\n", $conn->error);
}

You can find more info about the errors from the manual: https://www.php.net/manual/en/mysqli.error.php

Antti A
  • 410
  • 4
  • 12