Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Inserting when NULL using PHP and mySQL
Old 07-07-2009, 04:44 AM Inserting when NULL using PHP and mySQL
Novice Talker

Posts: 6
Trades: 0
Hey guys

So I've built a form with various text fields, radio buttons, and text areas. Now I want to be able to use the data collected from that form and INSERT it into my table using PHP.

On the next page (the action=) page from my form, I set up the INSERT query, however it runs into a problem when I don't enter a value into a field on the form on the previous page.

It's because I'm using the $variable = $_POST["postedvariable"] method, and since nothing was entered, nothing is posted, and the query fails since there is no value.

I'm wondering how this is usually handled in PHP? Do you have to write 'if statements' to handle it?

Thanks for any help guys!
RynMan is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-07-2009, 07:26 AM Re: Inserting when NULL using PHP and mySQL
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
since nothing was entered, nothing is posted, and the query fails since there is no value.
It depends of your DB.
If the field is created with a NOT NULL constraint, then yes, it's probably your problem.

You must understand that you have 2 systems now that talk together.
You have PHP, that process the requests, and you have the DB, which execute requests sent by PHP and return a feedback.

Your error might be in the PHP layer, but it can be that PHP is just relaying back an error that occured in the DB layer.

Read (and post here) the error message returned by PHP, it will change depending where the problem occured, and we'll be able to help you further.
Post the structure of the sql table you want to insert into too. It will help us.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 07-07-2009, 02:19 PM Re: Inserting when NULL using PHP and mySQL
orionoreo's Avatar
Ultra Talker

Posts: 335
Name: Jerry
Trades: 0
yup as tripy said,

you could also set a default value for that column
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
orionoreo is offline
Reply With Quote
View Public Profile
 
Old 07-07-2009, 06:35 PM Re: Inserting when NULL using PHP and mySQL
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
You also want to be sure to quote your db field value data so if it's an empty string, it will still validate as proper sql statement rather than cause an sql error.

Ex:
Do this: VALUES('$var1', '$var2') instead of this: VALUES($var1, $var2)
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 07-07-2009, 07:51 PM Re: Inserting when NULL using PHP and mySQL
orionoreo's Avatar
Ultra Talker

Posts: 335
Name: Jerry
Trades: 0
Quote:
Originally Posted by mgraphic View Post
You also want to be sure to quote your db field value data so if it's an empty string, it will still validate as proper sql statement rather than cause an sql error.

Ex:
Do this: VALUES('$var1', '$var2') instead of this: VALUES($var1, $var2)

not to hijack thread but I've always wondered about that

"UPDATE $table SET $column='$var1' WHERE $column2='$var2'";

how come table, column, and column2 doesn't have to be in '' but $var1, and 2 has to be?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
orionoreo is offline
Reply With Quote
View Public Profile
 
Old 07-08-2009, 12:09 AM Re: Inserting when NULL using PHP and mySQL
Lashtal's Avatar
wherenomanhasgonebefore

Posts: 680
Name: Lashtal
Trades: 0
because $table, $column, $column2 are Variables

and '$var1' , '$var2' are Values

---

one uses Variables to access the Values, not Values to access the Variables.


if you put the Variables into single quotes (''), you would get a parse error. Why is this?

because PHP will not be able to reference the Variables, it will not be able to return the Values of those Variables.
__________________
Currently Reading:
Please login or register to view this content. Registration is FREE
Lashtal is offline
Reply With Quote
View Public Profile
 
Old 07-08-2009, 12:18 AM Re: Inserting when NULL using PHP and mySQL
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Quote:
Originally Posted by orionoreo View Post
not to hijack thread but I've always wondered about that

"UPDATE $table SET $column='$var1' WHERE $column2='$var2'";

how come table, column, and column2 doesn't have to be in '' but $var1, and 2 has to be?
Like Lashtal said, $var1 and $var2 are values. Things like table and column are references to a defined instance. It is common practice to use a non-breaking name for these types of instances such as field_name and column_name, however you can rather use a tic mark (`) to enclose these types, even with a space such as `Table Name` and `Column Name`
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 07-08-2009, 01:01 PM Re: Inserting when NULL using PHP and mySQL
orionoreo's Avatar
Ultra Talker

Posts: 335
Name: Jerry
Trades: 0
understood... thanks!
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
orionoreo is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Inserting when NULL using PHP and mySQL
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.45003 seconds with 12 queries