Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP to MYSQL - Putting a PHP Array into a MYSQL table and updating it periodically?
    text
    copied!<p>Sorry if this has been answered, but if it had been answered already, I can't find it anywhere. So, from the beginning then. Lets say I had a <code>member</code> table and I want to store in it what their top 3 interests are. There is <code>$row</code> that has all the usual things to identify them <code>userID</code> and <code>password</code> etc.. and I had a further 3 columns which were labelled <code>top3_1</code>, <code>top3_2</code> &amp; <code>top3_3</code> to put each of their interests in from a post form.</p> <p>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?</p> <p>At the moment all what 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?</p> <p>For example: </p> <p>If we had a form which collected the top 3 interests to put in a table called users,</p> <pre><code>&lt;form action="back_to_same_page_for_processing.php" method="post" enctype="multipart/form-data"&gt; &lt;input name="top3_01" type="text" value="enter interest number 1 here" /&gt; &lt;input name="top3_02" type="text" value="enter interest number 2 here" /&gt; &lt;input name="top3_03" type="text" value="enter interest number 3 here" /&gt; &lt;input type="submit" name="update_button" value=" Save and Update! " /&gt; &lt;/form&gt; // If my quick code example for this form is not correct dont worry its not the point im getting at :) </code></pre> <p>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 --></p> <pre><code>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' } </code></pre> <p>With me so far? If I had a table which had 3 columns (1 for each interest) I could put something like - </p> <pre><code>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'"); </code></pre> <p>And hopefully if I've 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 what's 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.</p> <p>Continuing the example then, assuming I've changed the table for the 'top3' column instead of individual columns, I could put something like this - </p> <pre><code>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"); </code></pre> <p>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 <code>serialize()</code> &amp; <code>unserialize()</code> functions 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?</p> <p>If so please let me know and I'll be your friend forever, lol, ok maybe not but I'd really appreciate the help anyway. 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?</p> <p>I hope I've explained myself enough here, but if not say so and I'll have another go. Thanks very much people, Novice-ish.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload