473,503 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3851
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
3510
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
3924
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
2534
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
3358
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
2816
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
6072
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
15369
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
8465
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
366955
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
7198
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
7319
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...
1
6979
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5570
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.