473,804 Members | 3,228 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
14 3903
> > 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_yea r">
<?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="selec ted"';
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($_POS T["submit"]) and
($_POST["submit"] == "insert"))) {
if(isset($debug )) print "<!-- submit is set -->\n";
$query =" INSERT INTO jobs SET";
if(isset($_POST["PatientLastNam e"]))
$query = $query." PtNameLast = \"".
mysql_escape_st ring($_POST["PatientLastNam e"])."\", ";
if(isset($_POST["PatientFirstNa me"]))
$query = $query." PtNameFirst = \"".
mysql_escape_st ring($_POST["PatientFirstNa me"])."\", ";
if(isset($_POST["Prescriber "]))
$query = $query." Prescriber = ".$_POST["Prescriber "].", ";
if(isset($_POST["office"]))
$query = $query." OriginatingOffi ce = ".$_POST["office"].", ";
if(isset($_POST["DueDate"]))
$query = $query."DueDate = \"".
mysql_escape_st ring($_POST["DueDate"])."\" ,";
if(isset($_POST["arrivialDa te"]))
$query = $query."arrivia lDate = \"".
mysql_escape_st ring($_POST["arrivialDa te"])."\", ";
if(isset($_POST["RxDate"]))
$query = $query."RxDate = \"".
mysql_escape_st ring($_POST["RxDate"])."\",";
if(isset($_POST["WOnumber"]))
$query = $query." WOnumber = \"".
mysql_escape_st ring($_POST["WOnumber"])."\"";
if(isset($debug )) print "<!-- $query -->\n";
$result = mysql_query($qu ery) 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
3534
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 form for a number of platforms from our download pages at http://www.mysql.com/downloads/ and mirror sites.
0
3953
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 version of this document can be found at: http://prdownloads.sourceforge.net/souptonuts/README_mysql.txt?download
1
2563
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 transfer to a MySQL database. No special data, constraints etc that MySQL not will handle. My solution is to reverse engineer the database from ERStudio and then produce a SQL script that will insert the data into the MySQL engine. But I can't do...
1
3384
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 uses his own php shopping cart to receive customer orders. The configuration was done via cPanel with no external modifications - which produced no protests when built, ran and connected with no
1
2832
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. Despite entering the required lines to "my.ini" (the new my.cnf) through notepad AND MySQL Administrator, the cache does not work. So, today I took a peek at the 'Health' tab in MySQL Administrator.
3
6095
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: UPDATE MainInfo SET dAddress='38 Holland Blvd', dCity='miami', dState='FL', dZip='33000', dCountry='USA', dPhone='999987565', dNum='AC15857', dName='Michael A Scott' WHERE did=22'
1
15399
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, lastname, from associates where username like 'nancianne' but now fails with: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from associates
3
8518
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 the function load_image is called. Would anyone be able to point me to what I might be doing wrong? Thanks in advance, C Newbie
221
367773
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 needs to store entire files, the preferred method is to save the file onto the server’s file-system, and store the physical location of the file in your database. This is generally considered to be the easiest and fastest way to store files. ...
0
9708
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10589
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10085
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7625
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6857
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3828
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.