Page 1 of 1

data to be saved in db gets truncated???

Posted: Mon Dec 29, 2008 12:00 pm
by joomoo79
Using amfphp and flex, I'm trying to make a service that would register a new customer into a db. Having tested it directly in the amfphp's service browser, the service itself works fine BUT... say the address is "1234 blablah avenue", only "1234" gets saved.
You guys have any idea what I have to change in order to grab the whole address?

I know the code below is very basic, but once it fully works, I'll look into preventing sql injections, etc.

Code: Select all

<?php
 
include_once("con_sql.php");
class Customer
{
    var $dbhost = HOSTNAME;
    var $dbname = DATABASE;
    var $dbuser = USERNAME;
    var $dbpass = PASSWORD;
 
 
    function Customer()
    {
        $this->methodTable = array(
            "insertCustomer" => array(
                "description" => "Inserts a new client",
                "access" => "remote", // available values are private, public, remote
                "arguments" => array ("eml", "pwd", "fname", "lname", "address", "city", "state", "zip", "phone")
            )
        );
            $this->conn = mysql_pconnect($this->dbhost, $this->dbuser, $this->dbpass);
            mysql_select_db ($this->dbname);
    }
 
    function insertCustomer($eml, $pwd, $fname, $lname, $dob, $address, $city, $state, $zip, $phone)
     {
      //Insert query
        $result = mysql_query("Insert into customers values('', '".$eml."', '".$pwd."', '".$fname."', '".$lname."', '".$dob."', '".$address."', '".$city."', '".state."', '".$zip."', '".$phone."')");
        if($result) return mysql_insert_id($this->conn);
        else return "error";
    }
}
?>
 

Re: data to be saved in db gets truncated???

Posted: Mon Dec 29, 2008 12:23 pm
by Peter Anselmo
Double check your table structure in MySQL. If the address field is of type "INT" then it would behave as you describe - truncating after the numbers. If you're using phpMyAdmin, it's fairly simple to check, otherwise, if you have MySQL command line access, try the command "DESCRIBE customers;"

Re: data to be saved in db gets truncated???

Posted: Mon Dec 29, 2008 12:34 pm
by joomoo79
Peter Anselmo wrote:Double check your table structure in MySQL. If the address field is of type "INT" then it would behave as you describe - truncating after the numbers. If you're using phpMyAdmin, it's fairly simple to check, otherwise, if you have MySQL command line access, try the command "DESCRIBE customers;"
Mmm... well, my address field is of type varchar(250) collate utf8_unicode_ci NOT NULL...

Re: data to be saved in db gets truncated???

Posted: Mon Dec 29, 2008 1:21 pm
by Peter Anselmo
Try having it display the constructed query before executing. That will tell you in a hurry whether the full address is even making it to MySQL or is being truncated by PHP before that.

Code: Select all

 $query = "Insert into customers values('', '".$eml."', '".$pwd."', '".$fname."', '".$lname."', '".$dob."', '".$address."', '".$city."', '".state."', '".$zip."', '".$phone."')";
die( $query )
$result = mysql_query( $query );

Re: data to be saved in db gets truncated???

Posted: Mon Dec 29, 2008 2:19 pm
by joomoo79
If I do that, then the service browser returns an error message... (I should precise that I started learning php only within the amfphp context, so take me out of the regular class stuff and I'm a boob :oops: ... but willing to improve)

But anyways, I think you are right, it seems as if PHP trims off the address before sending it. To be exact, I have noticed that if I enter the address w/o the street number, it works... Therefore, I can see PHP takes either the text or the number, not both.
I have tried doing a regular insertion zithout using the service, and it worked. The full address got inserted.
The thing is I have no idea what to change... I have tried a couple of different combinations to perfom the insertion and it didn't really work. :cry:
Peter Anselmo wrote:Try having it display the constructed query before executing. That will tell you in a hurry whether the full address is even making it to MySQL or is being truncated by PHP before that.

Code: Select all

 $query = "Insert into customers values('', '".$eml."', '".$pwd."', '".$fname."', '".$lname."', '".$dob."', '".$address."', '".$city."', '".state."', '".$zip."', '".$phone."')";
die( $query )
$result = mysql_query( $query );

Re: data to be saved in db gets truncated???

Posted: Mon Dec 29, 2008 3:29 pm
by Peter Anselmo
What is the error message? I realized I forgot a semicolin after the "die()" statement (figures), I hope that didn't throw you off.

Re: data to be saved in db gets truncated???

Posted: Wed Dec 31, 2008 12:55 pm
by shannah78
This can happen if the address contains non-unicode characters. This often happens if the input is coming from something like microsoft word and your are not properly sanitizing the input. If you are receiving this data from an HTML form, make sure that your form tag has accept-charset specified as "UTF-8"
e.g.

Code: Select all

 
<form accept-charset="UTF-8" ... >
...
 
Also make use of the utf8_encode() function to convert non-utf8 strings into utf-8 before inserted it into your database.

Steve Hannah
http://weblite.ca