1

I wrote this query in php:

$query = "insert into admin values($user,$pass,$email,$signature)";
mysql_query($query);

But it has problems and can't insert information in database. Why? The database doesn't have any mistakes.

2
  • What "problems" does it have. What are the results of mysql_error()? Commented May 10, 2011 at 21:35
  • how can i understand what is error? i know data doesn't enter into database. Commented May 10, 2011 at 21:38

5 Answers 5

4

You have to put string within quotes. Try in this way and check error messages.

$query = "insert into admin values('$user','$pass','$email','$signiture')";
mysql_query($query) or die(mysql_error());
Sign up to request clarification or add additional context in comments.

2 Comments

make sure not to forget to escape the strings...otherwise you could run into more errors later. see my answer for how to do it.
@stevevls: you're right. I supposed he had already did it before passing variables to query execution.
1

You need to quote string values:

$query = "insert into admin values('$user','$pass','$email','$signiture')";

Also your values count must match your columns count EXACTLY or else you have to explicitly list your columns out:

$query = "insert into admin (`username`, `password`, `email`, `signiture`)  values('$user','$pass','$email','$signiture')";

Also "signiture" is mis-spelt.

Comments

1

For one, you need to put single quotes around your variables. In addition, you need to make sure that your variables are properly escaped because your query will still fail if one of your variables has a single quote.

$query = sprintf("insert into admin values('%s','%s','%s','%s')",
        mysql_real_escape_string($user),
        mysql_real_escape_string($pass),
        mysql_real_escape_string($email),
        mysql_real_escape_string($signiture));

mysql_query($query);

Finally, as other users have noted, it's generally a good idea to explicitly list the columns that you wish to insert.

Comments

0

you need to surround your values with single quote like this

$query = "insert into admin values('$user','$pass','$email','$signiture')";

If above does not work try to use the following syntax instead:

INSERT INTO TABLE(col1,col2,col3) VALUES('val1','val2','val3')

Comments

0

Try this (may not make a difference, but worth a shot):

$query = "insert into admin (userColumnName, passColumnName, emailColumnName, sigColumnName) values ($user, $pass, $email, $signature)";
mysql_query($query);

You'll have to replace userColumnName, etc with the actual column names in your DB.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.