开发者

mysql query not inserting to database

开发者 https://www.devze.com 2023-02-14 14:56 出处:网络
i have a db query in php that is not inserting into database. Have used开发者_JAVA百科 this format lots of times but for some reason its not working now. any ideas please

i have a db query in php that is not inserting into database. Have used开发者_JAVA百科 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";
}


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


Instead of...

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

do

$query = "INSERT INTO member_users ..."

Hope it works. :)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消