FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Defalut Value in MySql





abhinav_shah
I created a table in mysql :
create table mytab(name varchar(20), address varchar(20) not null default 'none');

Now from a html form i take the value for address. Now if somebody dosen't enter anything in the address field the default value should go into the table. But this is not happening. Instead nothing is going in the address field and when I display the table contents , address field comes as a blank.

Can somebody help...
raver
so let me understand....you probably have something like:

if ($address="") {
INSERT //...default value }
else {
INSERT //real value }

if you have something like this it should work...
better post the code so we can tell what is the problem...
mathiaus
I'm ill at the moment so my head isnt working 100% so please excuse me if I'm getting the wrong end of the stick.

I'm thinking you have a normal INSERT where you have something like
INSERT INTO mytab (id,name,address) VALUES ('','$name','$address')

now then if $address variable is blank it will insert a blank value into the database. The default value in the structure is for if $address is not inserted like below
INSERT INTO mytab (id,name,address) VALUES ('','$name','')

To get a default value to come into your database you can do1 of a number things. I personally would use the first one below.
If the address is not set set its value to 'none'
Code:
if (empty($address))
$address="none";


You could also use something like this example
Code:
if (empty($address)) {
INSERT INTO mytab (id,name,address) VALUES ('','$name','')
} else {
INSERT INTO mytab (id,name,address) VALUES ('','$name','$address')
}



hope this helps you and makes things clearer
Matt Smile
kv
This should work
Code:

if (empty($address)) {
INSERT INTO mytab (id,name) VALUES ('','$name')
} else {
INSERT INTO mytab (id,name,address) VALUES ('','$name','')
}
abhinav_shah
Quote:

To get a default value to come into your database you can do1 of a number things. I personally would use the first one below.
If the address is not set set its value to 'none'
Code:
if (empty($address))
$address="none";


hope this helps you and makes things clearer
Matt Smile


Thanx Man I tried this and it's working perfectly fine.
Related topics
E-Cards with PHP & MySQL
Build an online dictionary by PHP/MySQL
Make search engine With PHP and mySQL, for your site
Installing a SMF based forum
MySQL statement error (got me going)
PHP Tutorial: Basic Shoutbox w/ MySQL
An Example to mySQL class
[MySQL (& php)] += in SQL?
Need help understanding MySQL "Resource" type retu
MySql error: Column count doesn't match value count at row 1
[man]Azlo tu mismo ShoutBox en PHP y mysql
mysql gives the wrong result [solved]
Need Help integrating php variable into mySQL SELECT query
mySQL Query/PHP code - get the highest value...
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.