Jump to content

PHP to MYSQL - Putting a PHP Array into a MYSQL table??


Recommended Posts

Hello everyone,

 

Sorry if this has been answered but if it has I can't find it anywhere. So, from the begining then.  Lets say I had a member table and in it I wanted to store what their top 3 interests are.  Their$ row has all the usual things to identify them userID and password etc.. and I had a further 3 columns which were labled top3_1 top3_2 & top3_3 to put each of their interests in from a post form.

 

If instead I wanted to store this data as a PHP Array instead (using 1 column instead of 3) is there a way to store it as readable data when you open the PHPmyadmin?

 

At the moment all it says is array and when I call it back to the browser (say on a page where they could review and update their interests) it displays 'a' as top3_01 'r' as top3_02 and 'r' as top3_03 (in each putting what would be 'array' as it appears in the table if there were 5 results. Does anyone know what I mean?

 

For example -

 

If we had a form which collected the top 3 interests to put in a table called users,

 

<form action="back_to_same_page_for_processing.php" method="post" enctype="multipart/form-data">
    
    <input name="top3_01" type="text" value="enter interest number 1 here" />
    <input name="top3_02" type="text" value="enter interest number 2 here" />
    <input name="top3_03" type="text" value="enter interest number 3 here" />
    <input type="submit" name="update_button" value=" Save and Update! " />

    
    </form> // If my quick code example for this form is not correct dont worry its not the point im getting at 

 

And they put 'bowling' in top3_01, 'running' in top3_02 and 'diving' in top3_03 and we catch that on the same page with some PHP at the top -->

 

        if (isset($_POST)['update_button']) {
    
    $top3_01 = $_POST['top3_01']; // i.e, 'bowling' changing POST vars to local vars
    $top3_02 = $_POST['top3_02']; // i.e, 'running'
    $top3_03 = $_POST['top3_03']; // i.e, 'diving'

 

With me so far? If I had a table which had 3 columns (1 for each interest) I could put something like -

 

   include('connect_msql.php');
    
    mysql_query("Select * FROM users WHERE id='$id' AND blah blah blah");
    
    mysql_query("UPDATE users SET top3_01='$top3_01', top3_02='$top3_02', top3_03='$top3_03' WHERE id='$id'");

 

And hopefully if ive got it right, it will put them each in their own little column. Easy enough huh? But heres the thing, I want to put all these into an array to be stored in the 1 column (say called 'top3') and whats more have them clearly readable in PHPmyadmin and editable from there yet still be able to be called back an rendered on page when requested.

 

Continuing the example then, assuming ive changed the table for the 'top3' column instead of individual colums,  I could put something like this -

 

 

            if (isset($_POST)['update_button']) {
        
        $top3_01 = $_POST['top3_01']; // i.e, 'bowling' changing POST vars to local vars
        $top3_02 = $_POST['top3_02']; // i.e, 'running'
        $top3_03 = $_POST['top3_03']; // i.e, 'diving'
    
    $top3_array = array($top3_01,$top3_02,$top3_03);
    
    include('connect_msql.php');
    
    	mysql_query("UPDATE members SET top3='$top3_array' WHERE id='$id' AND blah blah blah");

 

But it will appear in the column as 'Array' and when its called for using a query it will render the literal string. a r r in each field instead.  Now I know you can use the 'serialize()' & 'unserialize()' funtcions but it makes the entry in the database practically unreadable. Is there a way to make it readable and editable without having to create a content management system?

 

If so please let me know and I'll be your friend forever, lol, ok maybe not but I'd really appreciate the help anyways.  The other thing is, If you can do this or something like it, how am I to add entries to that array to go back into the data base?

 

I hope ive explained myself enough here, but if not say so and I'll have another go.  Thanks very much people,  L-PLate (P.s if I sort this out on my own ill post it all here)

Hi

 

I'm not 100% following what you're trying to acheive but... If you want to store multiple values in the same database record from an array, and you want it to be easily readable, you could implode your array before storing it.

 

$comma_separated = implode(",", $your_array_name_here);

 

The delimter in this case is the comma. So you could add additional records stright to the database by just adding a comma.

 

Then when you pull the record from the database again you can just use the explode function to separate it backout into array values.

 

Probably not the most sophisticated way of doing it but a possible solution :)

 

Drongo

 

here is another thread that answer more or less exactly what you are asking for

 

http://www.phpfreaks.com/forums/index.php/topic,348646.0.html

 

in short... the "magic" idea is just bad design... you should use another table to store the user's interest

Yeah agree with miko that what i've suggested is a dirty way of doing it and concentrating on proper structure in your tables is the preferable route to go down. It was just the simplest way of doing it imo if the task is a small one but if your database is likely to grow a lot then yes, contentrate on table structure and let the database do the processing.

 

:)

 

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.