Solved

Inserting data into mysql using php

Posted on 2011-09-14
7
255 Views
Last Modified: 2013-12-13
Hi,
I'm inserting  data into a mysql table as below and it works.
However when I add 2 more Null columns to the end of the table, the procedure doesn't work.
I have no problem querying the table with the extra columns added to the query.
Hope you can help.
Thanks

$query = "INSERT INTO competition  (dates,day,type,format,fee,) VALUES
            		
			('".$dates."', '".$day."', '".$type."', '".$format."', '".$fee."')";
  $result = $conn->query($query);

Open in new window

0
Comment
Question by:encodeme
  • 3
  • 3
7 Comments
 
LVL 17

Expert Comment

by:Garry-G
ID: 36538683
Have you tried the same syntax in the MySQL console?
Have you tried configuring a default value for the new columns, does that correct the behavior?
0
 

Author Comment

by:encodeme
ID: 36538835
Hi Garry,
I'm struggling on how to write the query to work in the MYSQL console.... or it doesn't work.

I tried this

INSERT INTO competition  (dates,day,type,format,fee,) VALUES
            		
			(['2011-09-09'], ['sunday'], ['red'], ['green'], ['10'])"

Open in new window

0
 

Author Comment

by:encodeme
ID: 36539075
It's to do with the 2 new columns.
If i create a variable, give it a value, change the insert to include the new columns it works.

The column format is as below...I'm sure I'm doing something silly.


2011-09-14-213305.jpg
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 13

Accepted Solution

by:
Andrew Derse earned 250 total points
ID: 36539118
encodeme try this query syntax instead:

 
$query = "INSERT INTO competition  (dates,day,type,format,fee) VALUES
            		
			('$dates', '$day', '$type', '$format', '$fee')";
  $result = $conn->query($query);

Open in new window


For starters, you had an extra comma at the end of line 1 after fee...that needs to be removed.

Then, you need to only have 1 set of quotes or single quotes around your variables.  Numeric variables do not need quotes around them...all of the variables are seperated by commas.

Try that code I modified for you and see if it that helps.
0
 
LVL 13

Expert Comment

by:Andrew Derse
ID: 36539127
Also, you don't need to have your default as NULL as you have the NULL checkbox checked already...if you don't insert any information it's already NULL so remove your default = NULL from the draw column, but make sure you keep the NULL checkbox checked.  Do the same with your other column.
0
 

Author Closing Comment

by:encodeme
ID: 36539181
Brilliant .. I'm not sure which bit did it but removing the comma and quotations marks made it work.
Curiously in Mysql ..unless I'm wrong, when you change the default field to none, if the Null is ticked, the default fiield is re-set to Null.
many thanks for the help ..enjoy the points :)
0
 
LVL 13

Expert Comment

by:Andrew Derse
ID: 36539185
Glad to hear it worked ;)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

914 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

12 Experts available now in Live!

Get 1:1 Help Now