Link to home
Start Free TrialLog in
Avatar of jagguy
jagguyFlag for Australia

asked on

insert wont work

Hi,

This insert in php wont work and I am not getting any error msg.

$sql2="insert into tbmod1  (id,q1,q2,q3,q4,q5,q6,q7,q8,q9,q10,q11,q12,user,section) values 
(1, 'enter here1' ,'enter here2', 'enter here3', 'enter here4' ,'enter here5', 'enter here6','enter here7' ,'enter here8', 'enter here9','enter here10' ,'enter here11', 'enter here12'," . $myusername .",'section1')";

$result2=mysql_query($sql2);

Open in new window

Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Try:

$sql2="insert into tbmod1  (id,q1,q2,q3,q4,q5,q6,q7,q8,q9,q10,q11,q12,user,section) values
(1, 'enter here1' ,'enter here2', 'enter here3', 'enter here4' ,'enter here5', 'enter here6','enter here7' ,'enter here8', 'enter here9','enter here10' ,'enter here11', 'enter here12','" . mysql_real_escape_string($myusername) ."','section1')";

$result2=mysql_query($sql2);

(Added single quotes around the user value, and added mysql_real_escape_string on the value itself.
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also confirm that your ID field is not an autonumber type field, trying to manually provide a value for ID if it is an autonumber field will break your insert.

For debugging I print the completed SQL statements to screen and then manually copy and paste them into phpmyadmin or SQL Server Management Studio (depending on what DB I'm working with) and then execute the statement and i can see any error messages straight away.
$sql2="insert into tbmod1  (id,q1,q2,q3,q4,q5,q6,q7,q8,q9,q10,q11,q12,user,section) values
(1, 'enter here1' ,'enter here2', 'enter here3', 'enter here4' ,'enter here5', 'enter here6','enter here7' ,'enter here8', 'enter here9','enter here10' ,'enter here11', 'enter here12','$myusername','section1')";

$result2=mysql_query($sql2);
Avatar of jagguy

ASKER

OK i had a duplicate value. The id filed is listed as a Primary but I didnt create the thing as a Primary key and I cant remove primary key from this field
You can use ALTER statements to change the table definition, or you can use a GUI tool like HeidiSQL (free) to do it for you.
Just to confuse matters, you should be moving away from using mysql_* and switch to mysqli or PDO ;)