PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > mysql.general > integers fields and strings
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
integers fields and strings

Réponse
 
LinkBack Outils de la discussion
Vieux 14/03/2008, 21h04   #1
Les Fletcher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut integers fields and strings

I am having some issues with some integer fields and string input. The
table setup is the following:

| Field | Type | Null | Key | Default | Extra |
| intfield1 | smallint(6) | YES | | 0 | |
| intfield2 | smallint(6) | YES | | NULL | |
| intfield3 | smallint(6) | YES | | NULL | |

I have values coming into the database from an HTML form select box.
The default value from the select box is an empty string if nothing is
chosen. So we have an update statement that looks like the following:

UPDATE table SET intfield1='', intfield2='', intfield3='' WHERE ...;

This will then set everything to be 0's. Know, I know that there should
probably be better preprocessing before going to the database, but this
just brought to my attention this issue.

It seems to me that if the value that is being set is invalid for the
field it should set it to NULL or at least the default value for the
field instead of 0. Why is it set to 0 regardless?

Les
  Réponse avec citation
Vieux 14/03/2008, 21h31   #2
Rob Wultsch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: integers fields and strings

On Fri, Mar 14, 2008 at 12:04 PM, Les Fletcher <les@affinitycircles.com> wrote:
> | Field | Type | Null | Key | Default | Extra |
> | intfield1 | smallint(6) | YES | | 0 | |
> | intfield2 | smallint(6) | YES | | NULL | |
> | intfield3 | smallint(6) | YES | | NULL | |
> UPDATE table SET intfield1='', intfield2='', intfield3='' WHERE ...;
>
> It seems to me that if the value that is being set is invalid for the
> field it should set it to NULL or at least the default value for the
> field instead of 0. Why is it set to 0 regardless?
>
> Les


1. http://dev.mysql.com/doc/refman/5.0/...lid-data.html\
"If you try to store a string that doesn't start with a number into a
numeric column, MySQL Server stores 0. "
2. You are treating an integer like a string. An integer does not need
apostrophes. Without the apostrophes you would get an error. Type cast
it to an int. Better yet use a bind variable.
3. I suggest using a strict sql_mode. You would then get an error for
the blank string.



--
Rob Wultsch
  Réponse avec citation
Vieux 14/03/2008, 21h42   #3
Les Fletcher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: integers fields and strings

Thanks for the link. That s a lot.

Rob Wultsch wrote:
> On Fri, Mar 14, 2008 at 12:04 PM, Les Fletcher <les@affinitycircles.com> wrote:
>
>> | Field | Type | Null | Key | Default | Extra |
>> | intfield1 | smallint(6) | YES | | 0 | |
>> | intfield2 | smallint(6) | YES | | NULL | |
>> | intfield3 | smallint(6) | YES | | NULL | |
>> UPDATE table SET intfield1='', intfield2='', intfield3='' WHERE ...;
>>
>> It seems to me that if the value that is being set is invalid for the
>> field it should set it to NULL or at least the default value for the
>> field instead of 0. Why is it set to 0 regardless?
>>
>> Les
>>

>
> 1. http://dev.mysql.com/doc/refman/5.0/...lid-data.html\
> "If you try to store a string that doesn't start with a number into a
> numeric column, MySQL Server stores 0. "
> 2. You are treating an integer like a string. An integer does not need
> apostrophes. Without the apostrophes you would get an error. Type cast
> it to an int. Better yet use a bind variable.
> 3. I suggest using a strict sql_mode. You would then get an error for
> the blank string.
>
>
>
>

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h28.


Édité par : vBulletin® version 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09085 seconds with 11 queries