-2

I got some problem with sending data from an android app to MySQL database through PHP. the problem is that data is received but not inserted to the database. here is my code:

<?php
error_reporting(0);
include("db_config.php");

// array for JSON response
$response = array();

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

$result = mysql_query("INSERT INTO registeration(id,name,number,password) VALUES('','$name','$number','$password')");    

if ($result > 0) {
   $response["success"] = 1;
}    
else {
   $response["success"] = 0;
}

// echoing JSON response
echo json_encode($response);

?>
Maru
  • 894
  • 1
  • 12
  • 29
mohammed
  • 3
  • 2

2 Answers2

0

Because you are inserting null value in your primary key. Change your query with this :

$result = mysql_query("INSERT INTO registeration(name,number,password) VALUES('$name','$number','$password')");

Two important thing :

1) Dont use mysql_*. It's removed from PHP 7. Use mysqli_* or PDO.

2) Your query is open for sql injection. See this : How can I prevent SQL injection in PHP?

Community
  • 1
  • 1
Mr. Engineer
  • 3,522
  • 4
  • 17
  • 34
0

There are several issues with your code.

  • Check your spelling. Is your table really named registeration
  • Remove the primary key in the SQL statement. It is most probably an int, you pass a varchar.
  • Do not use mysql_*. Use PDO or mysqli_* with prepared statements
  • Do not store passwords as plain text, but use PHP's password feature (my personal 2 cents)
Maru
  • 894
  • 1
  • 12
  • 29