Solved

insert wont work

Posted on 2013-06-27
7
308 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
7 Comments
 
LVL 34

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 30

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 34

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 42

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now