Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Posting an array in a database
Old 04-28-2009, 11:27 AM Posting an array in a database
ADeacon's Avatar
Experienced Talker

Posts: 32
Name: Andrew
Location: Texas
Trades: 0
Hello everyone,

I'm having trouble posting a serialized array in a database. It keeps giving me a syntax error, but I can't find where the issue resides. If anyone can give me any ideas, I would appreciate it.

Site in question:

http://drawninwardmedia.com/nursecore/new_full_app.php

Error received when form is submitted:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':3:{i:0;s:4:"Mesa";i:1;s:6:"Tucson";i:2;s:9:"Arli ngton";}) VALUES (NULL, 'F', NU' at line 1

The PHP at the head of the document is way too long to post here, but the code specific to serializing the array is posted below.

PHP at the head of the page:

$interestedin=serialize($_POST['interested_in']);

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "register")) {
$insertSQL = sprintf("INSERT INTO applications (id, type,
...
checklist2_cert_other2_dateattended, $interestedin) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['id'], "int"),
GetSQLValueString($_POST['type'], "text"),
...
GetSQLValueString($_POST['Date_Attended'], "text"),
GetSQLValueString($interested_in, "text"));

As I said before, the extent of the PHP I have to make this page run is too much text to post here, but if you need to see it to troubleshoot I can send it to you or post it in logical segments.

Thanks in advance for any help you can give me.

-Andrew Deacon-
ADeacon is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-28-2009, 12:28 PM Re: Posting an array in a database
nayes84's Avatar
Extreme Talker

Latest Blog Post:
Difference between ASP And JSP
Posts: 232
Name: John
Location: Tokyo
Trades: 0
try using mysql_real_escape_string with the serialized array

PHP Code:
GetSQLValueString(mysql_real_escape_string($interested_in), "text")); 
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

if(I'm("Helpful")) Add_Talkupation("nayes84");

Last edited by nayes84; 04-28-2009 at 12:29 PM..
nayes84 is offline
Reply With Quote
View Public Profile
 
Old 04-28-2009, 01:21 PM Re: Posting an array in a database
ADeacon's Avatar
Experienced Talker

Posts: 32
Name: Andrew
Location: Texas
Trades: 0
It is giving me the same syntax error. Thanks for the suggestion.
ADeacon is offline
Reply With Quote
View Public Profile
 
Old 04-28-2009, 01:23 PM Re: Posting an array in a database
ADeacon's Avatar
Experienced Talker

Posts: 32
Name: Andrew
Location: Texas
Trades: 0
Would it be better for me to use an implode function rather than serialize the array?
ADeacon is offline
Reply With Quote
View Public Profile
 
Old 04-28-2009, 01:42 PM Re: Posting an array in a database
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Your error is that you are swaping the column definition and the values of those columns.
An insert statement is formed that way:
Code:
insert into table_name (field_name1, field_name2, field_name3) values ('string_value', integer_value, 'another_string_value');
Your error message seems to indicate that you have put the serialized value in place of the field name.
And don't forget to escape the serialized value, or an single quote in a property of the object will break your query.

But, on a side note, are you really sure you want to do it that way ?
Because, if you need to search the value of a property of the object, you will not be able to do so in a reliable way.
Now, I'm asking because there are times where saving a serialized value makes sense.
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 04-28-2009 at 01:44 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 04-28-2009, 01:55 PM Re: Posting an array in a database
nayes84's Avatar
Extreme Talker

Latest Blog Post:
Difference between ASP And JSP
Posts: 232
Name: John
Location: Tokyo
Trades: 0
Quote:
Originally Posted by tripy View Post
Your error is that you are swaping the column definition and the values of those columns.
Oh I didn't notice you are using variable in the columns definitions
Code:
checklist2_cert_other2_dateattended, $interestedin)
probably you need to change it to
Code:
checklist2_cert_other2_dateattended, interestedin)
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

if(I'm("Helpful")) Add_Talkupation("nayes84");
nayes84 is offline
Reply With Quote
View Public Profile
 
Old 04-30-2009, 03:42 PM Re: Posting an array in a database
Average Talker

Posts: 28
Name: Amit Soni
Trades: 0
use base64_encode() to convert the data into simple text and base64_decode() at the time of reading. That's how binary data is sent in emails.

mysql_escape_string() may not fix the ";" in json strings.

Another popular technique is to store use an array2xml function and store the XML, which usually is not expected to contain characters that conflict with mysql.
__________________
--Amit

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

amitsoni is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Posting an array in a database
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.26422 seconds with 12 queries