1

i have a db query in php that is not inserting into database. Have used this format lots of times but for some reason its not working now. any ideas please

    $query = "INSERT INTO `databasename`.`member_users` (`id`, `first_name`, `last_name`, `username`, `password`, `address1`, `address2`, `postcode`, `access`, `expires`) VALUES (NULL, '$fname', '$lname', '$email', '', '$add1', '$add2', '$postcode', '0', '')";
$result = mysql_query($query);
if($result){
    echo"query inserted";

}else{

    echo "nope";
}
6
  • 3
    What does mysql_error() show? Commented Mar 2, 2011 at 16:35
  • As Pekka says, check mysql_error(). Change your echo "nope"; to echo mysql_error();, which will give you the exact reason the query's failing. Just saying 'nope' is useless. Commented Mar 2, 2011 at 16:40
  • $postcode = "abc', '0', ''); DROP TABLE databasename.member_users; --" Commented Mar 2, 2011 at 16:40
  • think i have solved. If the form uses auto insert from previous data (i.e. chrome save details) it does not work. i have no idea why this is Commented Mar 2, 2011 at 16:41
  • 1
    To clarify @Tyler's comment, the fact that you "Have used this format lots of times" is downright scary. This is wide open to an attack called "SQL Injection" (google it) and is the MOST insecure way to write database statements. It is trivial for someone to write a URL that can destroy your database, or harvest its contents unfiltered. PLEASE read up on SQL Injection and change your ways. Commented Mar 2, 2011 at 16:43

3 Answers 3

2

Instead of echo "nope"; I suggest something like :

echo 'error while inserting : ['.mysql_errno().'] '.mysql_error();
echo 'query : '.$query;

This way you will be able to see the exact error and the query that was executed.

It can be a lot of things :

  1. Constraint error with a foreign key
  2. Data type error
  3. Non-existent field
  4. Wrong database or table name
Sign up to request clarification or add additional context in comments.

1 Comment

think i have solved. If the form uses auto insert from previous data (i.e. chrome save details) it does not work. i have no idea why this is
0

Instead of...

$query = "INSERT INTO `databasename`.`member_users` ..."

do

$query = "INSERT INTO member_users ..."

Hope it works. :)

Comments

0

If databasename and member_users are variables then, Instead of $query = "INSERT INTO databasename.member_users... do $query = "INSERT INTO $databasename.$member_users...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.