PHP INSERT Bad data

This is a discussion on PHP INSERT Bad data within the PHP Language forums, part of the PHP Programming Forums category; I have the code below. It builds an INSERT command, and it gets run on the database. Everything works fine ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-29-2004
Adrian Parker
 
Posts: n/a
Default PHP INSERT Bad data

I have the code below. It builds an INSERT command, and it gets run on the
database. Everything works fine except the database is only receiving the
first 6 characters of the phone number.

The field in the database can hold 10 digits (confirmed). The echo
statement shows that the phone number is indeed 10 characters long.

The second echo statement show the $query looks similar too:
INSERT INTO students Values ('', '1', 'SomeFirstname', 'SomeLastName',
'License', 'adrian.parker@sympatico.ca', ' 6135315960', ' ', ' ', ' ')

When I run this query in mySQL when logged onto the terminal, the phone
number appears fine.

How could truncation be happening?


if (isset($_POST["firstName"]))
{
$query = "INSERT INTO students Values ('', '1', '" . $_POST["firstName"] .
"', '
" . $_POST["lastName"] . "', '
" . $_POST["licenseNumber"] . "', '
" . $_POST["eMail"] . "', '
" . $_POST["phone"] . "', '
" . $_POST["city"] . "', '
" . $_POST["address"] . "', '
" . $_POST["birthDate"] . "')";

echo "Phone number is: " . $_POST["phone"] . "<br><br>";
echo "The query statement is: " . $query . "<br>";

$result = mysql_query($query) or die("Adding the student to the database
failed: " . mysql_error());
}



<Ade
--
Adrian Parker. Ordained priest. <adrian.parker@sympatico.ca>

"A society that views graphic violence as entertainment ...should not be
surprised when senseless violence shatters the dreams of it's youngest and
brightest..." - Ensign (March 2004)


Reply With Quote
  #2 (permalink)  
Old 04-29-2004
Jan Pieter Kunst
 
Posts: n/a
Default Re: PHP INSERT Bad data

In article <_0akc.41055$OU.957555@news20.bellglobal.com>,
"Adrian Parker" <adrian.parker@NOSPAMsympatico.ca> wrote:

> The field in the database can hold 10 digits (confirmed). The echo
> statement shows that the phone number is indeed 10 characters long.
>
> The second echo statement show the $query looks similar too:
> INSERT INTO students Values ('', '1', 'SomeFirstname', 'SomeLastName',
> 'License', 'adrian.parker@sympatico.ca', ' 6135315960', ' ', ' ', ' ')


I would guess the space between the first ' and the 6 is messing things
up. I don't know how the phone number column in your database is
defined, but could there be some silent character-to-number conversion
going on here, with unwanted results? Try it without that first space
and see what happens.

JP

--
Sorry, <devnull@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Reply With Quote
  #3 (permalink)  
Old 04-29-2004
Adrian Parker
 
Posts: n/a
Default Re: PHP INSERT Bad data


"Jan Pieter Kunst" <devnull@cauce.org> wrote in message
news:devnull-466947.18525729042004@news1.news.xs4all.nl...
> In article <_0akc.41055$OU.957555@news20.bellglobal.com>,
> "Adrian Parker" <adrian.parker@NOSPAMsympatico.ca> wrote:
>
> > The field in the database can hold 10 digits (confirmed). The echo
> > statement shows that the phone number is indeed 10 characters long.
> >
> > The second echo statement show the $query looks similar too:
> > INSERT INTO students Values ('', '1', 'SomeFirstname', 'SomeLastName',
> > 'License', 'adrian.parker@sympatico.ca', ' 6135315960', ' ', ' ', ' ')

>
> I would guess the space between the first ' and the 6 is messing things
> up. I don't know how the phone number column in your database is
> defined, but could there be some silent character-to-number conversion
> going on here, with unwanted results? Try it without that first space
> and see what happens.


It's being stuffed into a string, so there should be no problem. There is
no middleware between the database and my script, so there should be nothing
altering the data.

.... ok, removed the leading space, it fixed it!

Why would a leading space cause problems when inserting into a text field?


Adrian


Reply With Quote
  #4 (permalink)  
Old 04-29-2004
Kelly Thompson
 
Posts: n/a
Default Re: PHP INSERT Bad data

On Thu, 29 Apr 2004 18:52:57 +0200
Jan Pieter Kunst <devnull@cauce.org> wrote:

> In article <_0akc.41055$OU.957555@news20.bellglobal.com>,
> "Adrian Parker" <adrian.parker@NOSPAMsympatico.ca> wrote:
>
> > The field in the database can hold 10 digits (confirmed). The
> > echo statement shows that the phone number is indeed 10 characters
> > long.
> >
> > The second echo statement show the $query looks similar too:
> > INSERT INTO students Values ('', '1', 'SomeFirstname',
> > 'SomeLastName','License', 'adrian.parker@sympatico.ca', '
> > 6135315960', ' ', ' ', ' ')

>
> I would guess the space between the first ' and the 6 is messing
> things up. I don't know how the phone number column in your database
> is defined, but could there be some silent character-to-number
> conversion going on here, with unwanted results? Try it without that
> first space and see what happens.


If the field is INT the space won't matter, but an INT is 4 bytes. It
lets you store x such that -2147483648 < x < 2147483647. If it's INT,
then it won't fit. If it's varchar(10), then '6135315960' will, but
a string such as ' 6135315960' won't.
Reply With Quote
  #5 (permalink)  
Old 04-29-2004
Adrian Parker
 
Posts: n/a
Default Re: PHP INSERT Bad data


"Kelly Thompson" <kthompson_11_11@hotmail.com> wrote in message
news:20040429150809.6abd4f7a.kthompson_11_11@hotma il.com...
> On Thu, 29 Apr 2004 18:52:57 +0200
> Jan Pieter Kunst <devnull@cauce.org> wrote:
>
> > In article <_0akc.41055$OU.957555@news20.bellglobal.com>,
> > "Adrian Parker" <adrian.parker@NOSPAMsympatico.ca> wrote:
> >
> > > The field in the database can hold 10 digits (confirmed). The
> > > echo statement shows that the phone number is indeed 10 characters
> > > long.
> > >
> > > The second echo statement show the $query looks similar too:
> > > INSERT INTO students Values ('', '1', 'SomeFirstname',
> > > 'SomeLastName','License', 'adrian.parker@sympatico.ca', '
> > > 6135315960', ' ', ' ', ' ')

> >
> > I would guess the space between the first ' and the 6 is messing
> > things up. I don't know how the phone number column in your database
> > is defined, but could there be some silent character-to-number
> > conversion going on here, with unwanted results? Try it without that
> > first space and see what happens.

>
> If the field is INT the space won't matter, but an INT is 4 bytes. It
> lets you store x such that -2147483648 < x < 2147483647. If it's INT,
> then it won't fit. If it's varchar(10), then '6135315960' will, but
> a string such as ' 6135315960' won't.


It's a Varchar(10) field.

Why again does the leading space remove 4 bytes from the end of the string?


Adrian


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 08:34 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0