Solved

insert wont work

Posted on 2013-06-27
7
315 Views
Last Modified: 2013-06-27
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

0
Comment
Question by:jagguy
[X]
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
7 Comments
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 39281328
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.
0
 
LVL 31

Accepted Solution

by:
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: http://it2.php.net/manual/en/function.mysql-query.php

Cheers
0
 
LVL 6

Expert Comment

by:BurundiLapp
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.
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 7

Expert Comment

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

$result2=mysql_query($sql2);
0
 

Author Comment

by:jagguy
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
0
 
LVL 35

Expert Comment

by:gr8gonzo
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.
0
 
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 ;)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

752 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