Reply
Problem inserting array into mysql table
Old 01-15-2009, 04:53 AM Problem inserting array into mysql table
Junior Talker

Posts: 3
Trades: 0
Hi

I am very new to this, so any help would be greatly appreciated.
I have an array which splits numbers by semi-colon. It then posts it to my database, however it just put the number value as the word "array" in the table.
Everything splits fine into seperate rows my only problem is that the "numbers" that should appear in the number column in the table appear as the word "array".

Below is the code that I am using

<?php
$cellno = $_POST['number'];
$text = $_POST['message'];
$userid = $_POST['userid'];
$str = $cellno;
$strList = ltrim($str,";");
$strList = rtrim($str,";");
//echo $strList."<br>";
$strListExp = explode(";",$strList);
$strListSplit = array_chunk($strListExp,1);
$NumberOfstr = sizeof($strListSplit);
//print_r($strListSplit);
include 'opendb.php';
// Performing SQL query
foreach($strListSplit as $number)
{
$sql = "INSERT INTO smsouttest (number,text,userid) VALUES ('$number','$text','$userid')";
echo $sql;
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
}
// Closing connection
mysql_close($conn);
?>

Thanks.
Carl
cpilk is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 01-15-2009, 05:24 AM Re: Problem inserting array into mysql table
stoot98's Avatar
Ultra Talker

Posts: 427
Name: Stuart
Location: Glasgow, Scotland
Trades: 0
The database doesnt understand that that variable is an array object, hence why it just inserts Array (if it was a class object it would insert 'Object'). The only way to store them in one field would be delimiter them with a comma or a semicolon. When you pull the data out of the table you would then split it as you are doing now and then deal with the array.
stoot98 is offline
Reply With Quote
View Public Profile
 
Old 01-15-2009, 06:20 AM Re: Problem inserting array into mysql table
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
array_chunk always returns another array, so you will end up storing Array in your database this way.

I don't see why you are using array_chunk at all.
What it does is this:
PHP Code:
$array = array(1,2,3,4,5);
$chunk array_chunk($array,1);
print_r($chunk);
/* 
Array
(
    [0] => Array
        (
            [0] => 1
        )
    [1] => Array
        (
            [0] => 2
        )
    [2] => Array
        (
            [0] => 3
        )
    [3] => Array
        (
            [0] => 4
        )
    [4] => Array
        (
            [0] => 5
        )
)
*/ 
I think you want to remove the array_chunk completely and loop $strListExp in the foreach instead.
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 01-15-2009, 06:22 AM Re: Problem inserting array into mysql table
Junior Talker

Posts: 3
Trades: 0
Thanks, do you per chance have an example of this. Also I would like to mention that in essance this is a uploading cell numbers into different rows but still maintaing the other details in each row.
With the print command it shows me all the correct values, but when I try to insert this into mysql that is when I end up with the word "Array" displaying and not the actual value.

Could you possibly give me an example of the above, as I am still completely new to this.

Thanks
cpilk is offline
Reply With Quote
View Public Profile
 
Old 01-15-2009, 06:32 AM Re: Problem inserting array into mysql table
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
Try this
PHP Code:
<?php
$cellno 
$_POST['number'];
$text $_POST['message'];
$userid $_POST['userid'];
$str $cellno;
$strList ltrim($str,";");
$strList rtrim($str,";");
//echo $strList."<br>";
$strListExp explode(";",$strList);
include 
'opendb.php';
// Performing SQL query
foreach($strListExp as $number)
{
$sql "INSERT INTO smsouttest (number,text,userid) VALUES ('$number','$text','$userid')";
echo 
$sql;
$result mysql_query($sql) or die('Query failed: ' mysql_error());
}
// Closing connection
mysql_close($conn);
?>
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 01-15-2009, 07:03 AM Re: Problem inserting array into mysql table
Junior Talker

Posts: 3
Trades: 0
You are a star.

It worked like a charm, thank you very much Insensus.
cpilk is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Problem inserting array into mysql table
 

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.30174 seconds with 13 queries