Jump to content
Larry Ullman's Book Forums

A Problem With Insert!


Recommended Posts

Hello everyone,

 

I am working on a script which takes a user`s profile information and then writes it to a table called "profiles" in the database.

The code is based almost entirely on the registration script from Larry`s "knowledge Is Power" example!

 

The problem is I keep getting an error at the line where it tries to run the query (actually, the error line number the one for the

"if" statement). Also, when I go into PhpMyAdmin, it`s not adding the record to the database. I spent almost 2 hours playing

with it last night, but can`t figure out what is preventing the INSERT! I have errors turned on, and when I scroll down and check

the array element that holds the query, all the correct data is there. It has got to be something stupid that I overlooked!

 

Here is the offending code:

Add the user to the database...
$q = "INSERT INTO profiles (user_id, first_name, last_name, directory_name, description) VALUES 
({$_SESSION['user_id']}, '$fn', '$ln', `$dn`, `$d`)";

$r = mysqli_query ($dbc, $q);

if (mysqli_affected_rows($dbc) == 1)
{ // If it ran OK.
       ...

All field names and variable names have been checked about 10 times over and are correct!

 

Here is the design of the "profiles" table:

CREATE TABLE `profiles` (
 `profile_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
 `user_id` INT UNSIGNED NOT NULL,
 `first_name` VARCHAR(20) NOT NULL,
 `last_name` VARCHAR(40) NOT NULL,
 `description` TINYTEXT NOT NULL,
 `directory_name` VARCHAR(40) NOT NULL,
 `date_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
 PRIMARY KEY (`id`),
 UNIQUE KEY `user_id` (`user_id`),
 UNIQUE KEY `directory_name` (`directory_name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

 

If anyone has any idea what might be causing this please let me know!

I`d really appreciate it!

 

Thanks,

 

Matt

Link to comment
Share on other sites

Is mysqli installed in php running on your host? I have this problem so I need to either move to a different host or rewrite all the database connection code to use mysql.

I think I will upgrade to a VPS on a different host soon but until then I can't use any of the mysqli stuff except on my testing environment.

  • Upvote 1
Link to comment
Share on other sites

Wow! This was a tricky one. I was looking at your query, and then thinking about it...and then looking at your query again. Finally, I saw (what I think is) the issue. In your query, you are using grave accent marks for $dn and $d. Change those to single quotation marks, and you're good.

 

$q = "INSERT INTO profiles (user_id, first_name, last_name, directory_name, description) VALUES ({$_SESSION['user_id']}, '$fn', '$ln', `$dn`, `$d`)";

 

Please let us know if that solves the problem. Thanks.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...