insert wont work

Posted on 2013-06-27
Last Modified: 2013-06-27

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')";


Open in new window

Question by:jagguy
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 35

Expert Comment

ID: 39281328

$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')";


(Added single quotes around the user value, and added mysql_real_escape_string on the value itself.
LVL 31

Accepted Solution

Marco Gasi earned 500 total points
ID: 39281345
To get error messages you must use mysql_error() function:

$result2=mysql_query($sql2) or die(mysql_error());

Also, keep in mind mysqk is deprecated and you should migrate your code to mysqli: see the giant pink warning here:


Expert Comment

ID: 39281354
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.
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.


Expert Comment

ID: 39281356
$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')";


Author Comment

ID: 39281374
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
LVL 35

Expert Comment

ID: 39281381
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.
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39281410
Just to confuse matters, you should be moving away from using mysql_* and switch to mysqli or PDO ;)

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question