PHP MYSQL INSERT DATE PROBLEM

Archived from the Xataface Users forum.

shardison — Tue Sep 25, 2012 6:33 pm

Hi, I am trying to insert into a table data that I am collecting from a different table. It works fine except the date field will not insert. When I echo back the value of the date field it comes back as ‘Array’. This doesn’t make sense the orginal table shows the date and saves the date just fine. Here is the code:

$con = mysql_connect(“localhost:8889”,”root”,”root”);
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}

mysql_select_db(“ccap2012”,$con);

$client = $record->val(‘client_id’);
$street = $record->val(‘street’);
$city = $record->val(‘city’);
$state = $record->val(‘state’);
$zip = $record->val(‘zip_code’);
$move = $record->val(‘move_in’); //DATE VALUE HERE

$sql=”INSERT INTO Addresses (client_id, street, city, state, zip_code, move_in)
VALUES (‘$client’,’$street’,’$city’,’$state’,’$zip’,’$move’)”; //DATE VARIABLE PROBLEM
if (!mysql_query($sql,$con))
{
die(‘Error: ‘ . mysql_error());
}
echo “1 record added”;
echo $move; // ECHO COMES BACK AS ‘ARRAY’ ???
mysql_close($con);
}

The $move variable is the date value I am trying to insert. All of the other fields insert into the table as expected. Thanks for the help


Jean — Wed Sep 26, 2012 4:43 am

Code: Select all
$move = $record->strval('move_in'); //DATE VALUE HERE

strval because a date is a string.

Jean


shannah — Wed Sep 26, 2012 8:58 am

I also recommend you escape (ie addslashes) to all of the inputs as this opens the possibility of an SQL injection.

-Steve


shardison — Wed Sep 26, 2012 12:55 pm

Jean, and Steve
Thank you very much, That worked like a charm