/    Sign up×
Community /Pin to ProfileBookmark

trouble inserting int in mysql

Hi, i’ve got a problem when trying to insert int data type into mysql. The type of the data is int but sometimes this value is left blank. When the user does not type a value for the field the mysql query fails with a syntax error.

$sql = “INSERT INTO proy (name,date,number) VALUES (‘pete’,’2006-11-22′,)”;

When the user type a value the query does not fail.

Whats the problem?, i have others sql querys in others tables succeding.
Thanks all

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@mahfoozNov 23.2006 — try this

$sql = "INSERT INTO proy (name,date,number) VALUES ('pete','2006-11-

22','$number')";

or

$sql = "INSERT INTO proy (name,date) VALUES ('pete','2006-11-22')";
Copy linkTweet thisAlerts:
@coppocksNov 23.2006 — Well, you could try something like this:


<i>
</i>if($number == '') {
$number = '0';
}

$sql = "INSERT INTO proy (name,date,number) VALUES ('$name','$date','$number')";



This would always force something into the number field assuming that 0 would be OK if nothing was entered by the user.
Copy linkTweet thisAlerts:
@Pete_G_authorNov 23.2006 — i see, even int types must be enclosed in quotes if no value is given. So, better check out the value or enclose the variable.

Thanks you two.
Copy linkTweet thisAlerts:
@JayMNov 23.2006 — A more convenient approach is to apply a DEFAULT row column constraint in your mysql table. For example:

<i>
</i>number INT NOT NULL DEFAULT 0,
Copy linkTweet thisAlerts:
@coppocksNov 23.2006 — i see, even int types must be enclosed in quotes if no value is given. So, better check out the value or enclose the variable.

Thanks you two.[/QUOTE]

Let me clarify... when I did this:

<i>
</i>if($number == '') {
$number = '0';
}


I did that to ensure "something" was assigned. Thus the 'quotes'. Mostly out of habit with 0 (zero) because I've coded where some languages treat 0 as null when I really wanted a zero there. Now had I wanted to assign that with any other number or integer, I would have said, for example: $number= 5;

And as for the SQL statement, yes all values there should be like: '$number'

Cheers!
Copy linkTweet thisAlerts:
@coppocksNov 23.2006 — Absolutely! That's what I generally set in my tables too ... Your code is the better approach. But I addressed his incorrect statement and didn't look any further. Kudos Man!
×

Success!

Help @Pete_G_ spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...