0
$sql = "INSERT INTO $table_name VALUES
('$_POST[firstname]', '$_POST[lastname]', '$_POST[username]', password('$_POST[password]'), 'Users', '', '', '$pchange', 
'$_POST[email]', '$default_url', '$verify', '', 0, '', 0)";

$result = @mysql_query($sql,$connection) or die(mysql_error());

$sql, $connection and $table_name are all valid and are used previously in the script and the database, this is how my database looks like:

firstname   varchar(20) latin1_swedish_ci       Yes NULL          Change      Drop   More 
lastname    varchar(20) latin1_swedish_ci       Yes NULL          Change      Drop   More 
username    varchar(20) latin1_swedish_ci       Yes NULL          Change      Drop   More 
password    varchar(50) latin1_swedish_ci       Yes NULL          Change      Drop   More 
group1      varchar(20) latin1_swedish_ci       Yes NULL          Change      Drop   More 
group2      varchar(20) latin1_swedish_ci       Yes NULL          Change      Drop   More 
group3      varchar(20) latin1_swedish_ci       Yes NULL          Change      Drop   More 
pchange     varchar(1)  latin1_swedish_ci       Yes NULL          Change      Drop   More 
email       varchar(100)latin1_swedish_ci       Yes NULL          Change      Drop   More 
redirect    varchar(100)latin1_swedish_ci       Yes NULL          Change      Drop   More 
verified    varchar(1)  latin1_swedish_ci       Yes NULL          Change      Drop   More 
last_login  date                                Yes NULL          Change      Drop   More 
balance     double      UNSIGNED                Yes 0             Change      Drop   More 
address     varchar(60) latin1_swedish_ci       Yes NULL          Change      Drop   More 
lostbalance double                              Yes 0             Change      Drop   More 

Thanks in advance.

5
  • 2
    Don't use error suppressing operator if you want to see what's wrong. Commented May 10, 2012 at 13:37
  • 1
    echo $sql and see if you can run it manually inside mysql. Commented May 10, 2012 at 13:38
  • 1
    get rid of that @!!!!!!!!!! Commented May 10, 2012 at 13:39
  • 1
    Also, don't just blindly insert $_POST variables into your mysql queries. Either mysql_real_escape them before or use prepared statements (PDO is great for this too). Commented May 10, 2012 at 13:40
  • I used if(!cleanQuery($_POST['username'])==$_POST['username']) before that Commented May 10, 2012 at 16:37

3 Answers 3

4

No error because of @:

@mysql_query($sql,$connection) or die(mysql_error());

The @ suppresses the errors from the mysql_query() function.

I see multiple errors in your statements:

  1. '$_POST[firstname]'- is suppossed to be $_POST['firstname']. Store the value in a variable or use concatenation: "'.$_POST['firstname'].'"

  2. Use mysql_query($sql) or die(mysql_error());

  3. Escape all the data you are storing in the db.

Sign up to request clarification or add additional context in comments.

2 Comments

To further comment on this, read: Why is $foo[bar] wrong? (Search for it on the page, it doesn't provide a direct link to that paragraph)
"$_POST[firstname]" is correct. php.net/manual/en/…
1

First of all, it is not good security practice to leave any user input unfiltered, because soon you will be victim of SQL injection and/or XSS attacks. You should filter your user input this way:

$var = filter_var($_POST['var']), FILTER_SANITIZE_STRING);

Then you should use this $var in your SQL query, instead of directly using the $_POST['var']. i.e.:

$sql = "INSERT INTO $table_name VALUES
('$firstname', '$lastname', '$username', password('$password'), 'Users', '', '', '$pchange', 
'$email', '$default_url', '$verify', '', 0, '', 0)";

Comments

-2

You have to name the table columns in front of "VALUES"

INSERT INTO $table_name (field_1, field2, ...) VALUES ($_POST_1, ...)

1 Comment

Only if the provided value count doesn't match the column count.

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.