Forum Moderators: coopster

Message Too Old, No Replies

Insert data problem

         

tunnelvision

10:39 am on Nov 20, 2005 (gmt 0)

10+ Year Member



Ive been trying to get this registration script working on my site. But when i try to insert the data it comes up with a MySQL error the cause of the problem is an array on line 110. Heres the code:


// Insert data into database
$sql = "INSERT INTO " . USERS_TABLE . "
(user_id, username, user_email, user_password, user_pob, user_location, user_reg_date)
VALUES ('$user_id', '$username', '$email', md5('$password'), '$pob', '$pob', '$time')";

$db->query($sql) or exit('An error occured while saving data.<br/><strong>Line:</strong>'. __LINE__ .'<br/><strong>File:</strong>'. __FILE__ .'<br/><strong>Cause:</strong>'.$db->error().'');

Has any one else ever had this problem? or has any one got a solution to the problem?

Thanks tunnelvision

directrix

2:35 pm on Nov 20, 2005 (gmt 0)

10+ Year Member



Some ideas...

Does mysql_error() hold anything useful?

Using mysql_real_escape_string() on each field as you insert it is good practice.

IamStang

2:40 pm on Nov 20, 2005 (gmt 0)

10+ Year Member



I have just started using mysql, but it seems to me your $sql statement is not formatted correctly. I could be wrong but I use the following.

$sql = mysql_query("INSERT INTO TABLE_NAME (Field_1, Field_2,.............etc) VALUES ('Field1_value', 'Field2_value',...........etc)") or die ("Error!");

Is worth a try. Hope it helps.

IamStang

PS.

directrix has a good point with his last comment too!

Rincewind456

3:07 pm on Nov 20, 2005 (gmt 0)

10+ Year Member



$sql = "INSERT INTO " . USERS_TABLE . "

Either this should be:

$sql = "INSERT INTO " . $USERS_TABLE . "

or

$sql = "INSERT INTO USERS_TABLE

As long as you are inserting to all the fields in the table you could shorten the query to:
$sql = "INSERT INTO USERS_TABLE VALUES ('$user_id', '$username', '$email', md5('$password'), '$pob', '$pob', '$time')";

directrix

3:40 pm on Nov 20, 2005 (gmt 0)

10+ Year Member



Rincewind456, re building $sql, not necessarily -- USERS_TABLE may be a constant.

dreamcatcher

5:01 pm on Nov 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Rincewind456, defined variables have no dollar sign as directrix mentioned.

define('USERS_TABLE', 'tablename');

echo USERS_TABLE;

tunnelvision, you say the error is related to an array. Can you post the error message you receive?

dc