User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 392,004 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,159 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 873 | Replies: 4
Reply
Join Date: Aug 2007
Posts: 50
Reputation: nil_gh_80 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nil_gh_80's Avatar
nil_gh_80 nil_gh_80 is offline Offline
Junior Poster in Training

MySQL datatype problem....

  #1  
Aug 23rd, 2007
Hi all,
I'm facing a big problem using MySQL...i've created a table using this code:
  1. CREATE TABLE table1(name char(20), age int(3));

Now this table can insert ineteger values in name field........which i don't want and the age field can take a value like e.g. 123456789...although it's length is 3.....will you help me to solve this problem.....plz.
Last edited by nil_gh_80 : Aug 23rd, 2007 at 2:00 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,599
Reputation: Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice 
Rep Power: 11
Solved Threads: 257
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Maven

Re: MySQL datatype problem....

  #2  
Aug 23rd, 2007
Since numbers are valid characters, if you don't want them in the field then you will have to check for that prior to saving them to the table.

The int(3) spec does not limit the values to three or less digits. Here is the section describing what it does from the MySQL docs:
Another extension is supported by MySQL for optionally specifying the display width of integer data types in parentheses following the base keyword for the type (for example, INT(4)). This optional display width is used to display integer values having a width less than the width specified for the column by left-padding them with spaces.
The display width does not constrain the range of values that can be stored in the column, nor the number of digits that are displayed for values having a width exceeding that specified for the column. For example, a column specified as SMALLINT(3) has the usual SMALLINT range of -32768 to 32767, and values outside the range allowed by three characters are displayed using more than three characters.
Once again, you will need to validate the data before saving it.
Reply With Quote  
Join Date: Aug 2007
Posts: 50
Reputation: nil_gh_80 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nil_gh_80's Avatar
nil_gh_80 nil_gh_80 is offline Offline
Junior Poster in Training

Re: MySQL datatype problem....

  #3  
Aug 23rd, 2007
How can I check those attributes......plz give me some code..
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,599
Reputation: Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice 
Rep Power: 11
Solved Threads: 257
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Maven

Re: MySQL datatype problem....

  #4  
Aug 23rd, 2007
See section 6.4 Validating User Input here:
http://hudzilla.org/phpwiki/index.php?title=HTML_forms

More complex validation can be performed with regex functions:
http://www.php.net/manual/en/ref.pcre.php

A tutorial showing form validation with preg_match can be found here:
http://www.htmlcenter.com/tutorials/...s.cfm/149/PHP/
Last edited by Ezzaral : Aug 23rd, 2007 at 4:42 pm.
Reply With Quote  
Join Date: Dec 2004
Location: Fort Bragg, NC
Posts: 189
Reputation: mikeSQL is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
mikeSQL's Avatar
mikeSQL mikeSQL is offline Offline
Junior Poster

Help Re: MySQL datatype problem....

  #5  
Aug 24th, 2007
Originally Posted by nil_gh_80 View Post
Hi all,
I'm facing a big problem using MySQL...i've created a table using this code:
  1. CREATE TABLE table1(name char(20), age int(3));

Now this table can insert ineteger values in name field........which i don't want and the age field can take a value like e.g. 123456789...although it's length is 3.....will you help me to solve this problem.....plz.

Hi nil_gh_80, try this below. Im sure it will work for I have tested this myself before replying here:
CREATE TABLE `table1` ( `name` VARCHAR( 20 ) NOT NULL ,
`age` VARCHAR( 3 ) NOT NULL
) ENGINE = MYISAM ;


and for the PHP Code its:
    $sql  = 'CREATE TABLE `table1` ('
        . '   `name` VARCHAR(20) NOT NULL, '
        . '   `age` VARCHAR(3) NOT NULL'
        . ' )'
        . ' ENGINE = myisam;';

I hope this helps you.
dynastyCODERS#1 when it comes to Programming Tutorials, Database designs and discussions, Operating Systems, you name it, check us out and drop us a line to tell us your opinions on any and everything in mind!;)
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 10:11 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC