473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

MySQL Query

If I have a table set up like this:

Name | VARCHAR
Email | VARCHAR
Age | TINYINT | NULL (Default: NULL)

And I want the user to enter his or her name, email, and age - but AGE
is optional.

My insert would look something like:

INSERT INTO data (Name, Email, Age) VALUES ('$name', '$email', $age)

This is all good, except if the user doesn't enter an age. Then $age is
an empty variable. I thought that since the table is set up where Age
can be NULL, that this should be fine. But MySQL is giving me an error.
If $age is a number, it is no problem.

Anyway to make it accept $age as an empty variable (and just make it
NULL)? I know I could say:

if (empty($age)) { $age = 0; }

and write it that way, but this is just an example and there are many
more variables that could be empty.

Thanks.
Aug 29 '05 #1
14 3831
Either don't include them in the insert:

insert into data (name, email) values ('$name', '$email')

or make the variable 'null' (literally). Something like:

$age = !empty($age) ? $age : 'null'; //'null' is a string containing
the word 'null'

Then do your query as before.

Aug 29 '05 #2
ZeldorBlat wrote:
Either don't include them in the insert:

insert into data (name, email) values ('$name', '$email')

or make the variable 'null' (literally). Something like:

$age = !empty($age) ? $age : 'null'; //'null' is a string containing
the word 'null'

Then do your query as before.


I was trying to do it without having to take it out of the query or
assign it NULL... just because there are a large number of variables
that could be empty.
Aug 29 '05 #3
If you want the value to be null in the database, then you need some
way of telling MySQL that's what you want. One way is to ommit it from
the insert -- in which case the default value (in this case, null) is
used. The other way is to specify the value as the literal string
'null'.

Aug 29 '05 #4
set default value to 0 gor AGE field in structure of database!

Aug 30 '05 #5
just dont enter in that field while executing anu INSERT query it will
automatically take that field DEFAULT value NULL without any error.

Aug 30 '05 #6
Dont include that field in ur INSERT clause while inserting record it
will take DEFAULT value as NULL as u have specified and set NULL
attribute as 'yes' in ur structure of table and DEFAULT value as 'null'

OR IF,

ur getting values dynamically from user then store all values in
variable and then execute INSERT query even if any value will come null
it will be store in variable and that all variables u used in ur INSERT
query!

Aug 30 '05 #7
Just change your insert query with

INSERT INTO data (Name, Email, Age) VALUES ('$name', '$email', '$age')

don't use $age, instead use '$age'

this means when query will be executed it will look like
INSERT INTO data (Name, Email, Age) VALUES ( 'myname', 'email', '' ) if
user is not specifying his/her age.

currently your query looks like this
INSERT INTO data (Name, Email, Age) VALUES ( 'myname', 'email', ) if
user is not specifying his/her age.
and it gives you syntax error.

Do you understan wat i mean?

just do it for all your numeric vaule to be entered.

and recall your PHP fundamentals.

KERUL
['ProDesignZ']

Aug 30 '05 #8
On Mon, 29 Aug 2005 11:24:55 -0400, Dave Thomas wrote:
If I have a table set up like this:

Name | VARCHAR
Email | VARCHAR
Age | TINYINT | NULL (Default: NULL)

And I want the user to enter his or her name, email, and age - but AGE
is optional.

My insert would look something like:

INSERT INTO data (Name, Email, Age) VALUES ('$name', '$email', $age)

This is all good, except if the user doesn't enter an age. Then $age is
an empty variable. I thought that since the table is set up where Age
can be NULL, that this should be fine. But MySQL is giving me an error.
If $age is a number, it is no problem.

Anyway to make it accept $age as an empty variable (and just make it
NULL)? I know I could say:

if (empty($age)) { $age = 0; }

and write it that way, but this is just an example and there are many
more variables that could be empty.

Thanks.

BTW, a field containg an AGE has no real value. What will happen net year.
Youd'better take care of the birth day, a fixed data.

Johan

Aug 30 '05 #9
"johan" <va*******@hotmail.com> kirjoitti
viestissä:pa****************************@hotmail.c om...
On Mon, 29 Aug 2005 11:24:55 -0400, Dave Thomas wrote:
If I have a table set up like this:

Name | VARCHAR
Email | VARCHAR
Age | TINYINT | NULL (Default: NULL)

And I want the user to enter his or her name, email, and age - but AGE
is optional.

My insert would look something like:

INSERT INTO data (Name, Email, Age) VALUES ('$name', '$email', $age)

This is all good, except if the user doesn't enter an age. Then $age is
an empty variable. I thought that since the table is set up where Age
can be NULL, that this should be fine. But MySQL is giving me an error.
If $age is a number, it is no problem.

Anyway to make it accept $age as an empty variable (and just make it
NULL)? I know I could say:

if (empty($age)) { $age = 0; }

and write it that way, but this is just an example and there are many
more variables that could be empty.

Thanks.

BTW, a field containg an AGE has no real value. What will happen net year.
Youd'better take care of the birth day, a fixed data.

Asking age is easier on the user in my opnion, but he could store
(date('Y') - $age) instead, to get the birthyear.

--
SETI @ Home - Donate your cpu's idle time to science.
Further reading at <http://setiweb.ssl.berkeley.edu/>
Kimmo Laine <et****************@5P4Mgmail.com>
Aug 30 '05 #10
> > BTW, a field containg an AGE has no real value. What will happen net year.
Youd'better take care of the birth day, a fixed data.


Asking age is easier on the user in my opnion, but he could store
(date('Y') - $age) instead, to get the birthyear.

He could also use HTML <select> and show "available" ages in it, but
use birth year as values (or show both but send year only). Something
like:

Age: <select name="birth_year">
<?php
$curr_year = intval( date( 'Y' ) );
$birth_year = @intval( $_REQUEST['birth_year'] );
for( $i = ($curr_year-110); $i <= $curr_year; $i++ )
{
echo ' <option value="', $i, '"';
if ($i == $birth_year)
echo ' selected="selected"';
echo '>', ($curr_year - $i), ' (', $i, ')</option>', "\n";
}
?>
</select>
Hilarion
Aug 30 '05 #11
On Tue, 30 Aug 2005 11:52:35 +0200, johan wrote:
On Mon, 29 Aug 2005 11:24:55 -0400, Dave Thomas wrote:
If I have a table set up like this:

Name | VARCHAR
Email | VARCHAR
Age | TINYINT | NULL (Default: NULL)

And I want the user to enter his or her name, email, and age - but AGE
is optional.

My insert would look something like:

INSERT INTO data (Name, Email, Age) VALUES ('$name', '$email', $age)

This is all good, except if the user doesn't enter an age. Then $age is
an empty variable. I thought that since the table is set up where Age
can be NULL, that this should be fine. But MySQL is giving me an error.
If $age is a number, it is no problem.

Anyway to make it accept $age as an empty variable (and just make it
NULL)? I know I could say:

if (empty($age)) { $age = 0; }

and write it that way, but this is just an example and there are many
more variables that could be empty.

Thanks.

BTW, a field containg an AGE has no real value. What will happen net year.
Youd'better take care of the birth day, a fixed data.

Johan


This is a response to the OP....
When I have items that are nullable, I build the list and the values
sections as I parse the page. So if itemx is there, I build the DB_ItemX
field into the the ongoing column list:
(DB_ItemA, DB_ItemB, DB_ItemX)
This is relatively easy using:
$insertList.= ',DB_ItemX';
and then it's value in the value list:
$valueList.= ",'".$formItemX."'";

So when you get to the point of building the runnable SQL:
$insertSQL = "INSERT to your.table (";
$insertSQL.= $insertList ;
$insertSQL.= ") Values (";
$insertSQL.= $valueList ;
$insertSQL.= ")" ;

Also note that if you are supplying values for all of the columns in the
table, SQL does not require the column list. The values then need to be
in the exact same order as defined in the table though!!

Chris

Aug 31 '05 #12
Dave Thomas wrote:
My insert would look something like:
INSERT INTO data (Name, Email, Age) VALUES ('$name', '$email', $age)
This is all good, except if the user doesn't enter an age.


if ($age>0) $q = "INSERT INTO data VALUES ('$name', '$email', $age)";
else $q = "INSERT INTO data VALUES ('$name', '$email')";
$rv = mysql_query($q);

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Sep 3 '05 #13
Dave Thomas wrote:
If I have a table set up like this:

Name | VARCHAR
Email | VARCHAR
Age | TINYINT | NULL (Default: NULL)

And I want the user to enter his or her name, email, and age - but AGE
is optional.

My insert would look something like:

INSERT INTO data (Name, Email, Age) VALUES ('$name', '$email', $age)

This is all good, except if the user doesn't enter an age. Then $age is
an empty variable. I thought that since the table is set up where Age
can be NULL, that this should be fine. But MySQL is giving me an error.
If $age is a number, it is no problem.

Anyway to make it accept $age as an empty variable (and just make it
NULL)? I know I could say:

if (empty($age)) { $age = 0; }

and write it that way, but this is just an example and there are many
more variables that could be empty.

Thanks.

I do it this way:
if (isset($_POST["Cases"])){
if(isset($debug)) print "<!-- checking for submition of post data
-->\n";
if((isset($_POST["submit"]) and
($_POST["submit"] == "insert"))) {
if(isset($debug)) print "<!-- submit is set -->\n";
$query =" INSERT INTO jobs SET";
if(isset($_POST["PatientLastName"]))
$query = $query." PtNameLast = \"".
mysql_escape_string($_POST["PatientLastName"])."\", ";
if(isset($_POST["PatientFirstName"]))
$query = $query." PtNameFirst = \"".
mysql_escape_string($_POST["PatientFirstName"])."\", ";
if(isset($_POST["Prescriber"]))
$query = $query." Prescriber = ".$_POST["Prescriber"].", ";
if(isset($_POST["office"]))
$query = $query." OriginatingOffice = ".$_POST["office"].", ";
if(isset($_POST["DueDate"]))
$query = $query."DueDate = \"".
mysql_escape_string($_POST["DueDate"])."\" ,";
if(isset($_POST["arrivialDate"]))
$query = $query."arrivialDate = \"".
mysql_escape_string($_POST["arrivialDate"])."\", ";
if(isset($_POST["RxDate"]))
$query = $query."RxDate = \"".
mysql_escape_string($_POST["RxDate"])."\",";
if(isset($_POST["WOnumber"]))
$query = $query." WOnumber = \"".
mysql_escape_string($_POST["WOnumber"])."\"";
if(isset($debug)) print "<!-- $query -->\n";
$result = mysql_query($query) or die("Query failed");
}
if(isset($debug)) print "<!-- finished with submitted processing
-->\n";

that way if anything is missing it doesn't bomb out the whole thing, or
make it bomb out the wjole page and send back an error to the user if
something absolutely necessary is missing
Sep 16 '05 #14
Dave Thomas wrote:
If I have a table set up like this:

Name | VARCHAR
Email | VARCHAR
Age | TINYINT | NULL (Default: NULL)

Is it PHP that's bombing out or MySQL?
Sep 23 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Lenz Grimmer | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, MySQL 4.0.14, a new version of the popular Open Source/Free Software Database, has been released. It is now available in source and binary...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
1
by: Cern | last post by:
Is it somebody out there who has made a migration from an Oracle server to an MySQL server?? The scenario is as simply: I've got a Oracle 8 server with a database with content that I want to...
1
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer...
1
by: Good Man | last post by:
Hi there I've noticed some very weird things happening with my current MySQL setup on my XP Laptop, a development machine. For a while, I have been trying to get the MySQL cache to work....
3
by: Juan Antonio Villa | last post by:
Hello, I'm having a problem replicating a simple database using the binary log replication, here is the problem: When the master sends an update to the slave, an example update reads as follows:...
1
by: Ike | last post by:
Recently, I began using a different MySQL verver (i.e. different machine as well as different version#, going from 4.12a to 4.1.9 max). The following query used to work: select firstname,...
3
by: Me Alone | last post by:
Hello: I am trying to edit some C code I found in "The definitive guide to using, programming, and administering MySQL" by Paul DuBois. This C client program connects and then segfaults when...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.