Jump to content

Store an Array in Database - Im Stuck


louisesanders

Recommended Posts

I am trying to create a CMS – I am relatively new to PHP so please understand if this is really silly. I am trying to find the best way to store 6 different values (submitted via a form) as part of an array which I would then like to store in a database. I then need to be able to retrieve the array from the database and extract each value.

 

I have read about the implode and explode function but I have not seen any great examples, or ones that I can understand! I have posted an excerpt of the code below. Please could someone enlighten me with an easy explanation?

 

So in steps:

 

1. I want to enter values into the form

2. Submit form and store the 6 values in an array, to be stored in the database in the table called listing_images under the filed image_path.

3. Retrieve the array and extract each value.

 

----------------------------------------------------------------

 

 

   

<!--Excerpt from the form -->

<form action ="process-form.php" method="post">

<p align="center">  </p>

<table width="494" border="0" align="center" cellpadding="5" cellspacing="2">

  <tr bgcolor="#ededed">

 

<td width="189">Image 1  path </td>

<td width="279"><input name="image_path[]" type="text" id="imagepath[]" /></td>

  </tr>

  <tr bgcolor="#ededed">

    <td>Image 2 path </td>

    <td><input name="image_path[]" type="text" id="image_path[]" /></td>

  </tr>

  <tr bgcolor="#ededed">

    <td>Image 3 path </td>

    <td><input name="image_path[]" type="text" id="image_path[]" /></td>

  </tr>

  <tr bgcolor="#ededed">

    <td>Image 4 path </td>

    <td><input name="image_path[]" type="text" id="image_path[]" /></td>

  </tr>

  <tr bgcolor="#ededed">

    <td>Image 5 path </td>

    <td><input name="image_path[]" type="text" id="image_path[]" /></td>

  </tr>

  <tr bgcolor="#ededed">

    <td>Image 6 path </td>

    <td><input name="image_path[]" type="text" id="image_path[]" /></td>

  </tr>

    <tr bgcolor="#ededed">

    <td colspan="2"><div align="center">

      <input type = "submit" value = "SUBMIT" />

    </div></td>

    </tr>

</table>

</p>

</form>

 

 

<?php

// Define variable to be posted into database, in this case it needs to be an array of the 6 values entered in via the form - Is this correct ?

$image_path = $_POST['image_path'];

$image_path =htmlspecialchars($image_path);

 

 

//How do I insert an array into the database

$sql = "INSERT INTO listing_images SET

image_path = '$image_path'";

 

 

// How do I retrieve the array and extract the values.

?>

 

</body>

</html>

Link to comment
Share on other sites

Hey!  I can help ya out.  explode() is pretty awesome.  Here's what you could do:

 

// this is the code to put the image paths together
<?php
$data = $_POST['img1'] . '\t' . $_POST['img2'] . '\t' . $_POST['img3'] . '\t';
$data .= $_POST['img4'] . '\t' . $_POST['img5'] . '\t' . $_POST['img6'] . '\t';
$query = 'INSERT INTO listing_images SET image_path="' . $data . '"';
$result = mysqli_query($con, $query);
?>

// this is the code to use explode to pull them apart
<?php
$query = 'SELECT image_path FROM listing_images';
$result = mysqli_query($con, $query);
$row = mysqli_fetch_array($result);
$img_path = explode('\t', $row['image_path']); // this splits the string up at each tab
                                                              // and puts each value the array so
                                                              // $img_path[0] would have the first 
                                                              // image path, $img_path[1] would have
                                                              //  the second, and so on
?>

 

oh and make sure you name the html input forms correctly to match the $_POST[] stuff

 

example: <input name="img1" type="text" id="image_path[]" />

 

would be found in: $_POST['img1'] on your php page

Link to comment
Share on other sites

Most of the time, storing an array in a database field is a bad idea. Why? Well, it makes searching, sorting, deleting and updating all the more difficult. Not only that, but it gives you scope to change the number of allowed images and is almost certainly going to be more efficient both in terms of time taken and space used.

 

Almost every time you feel the need to do this, what you actually want to do is add another table. For example, you might add a another table called images with fields id,userid,imagepath. The field userid can then be used in a join to be able to select the relevant images for a particular user. If you're confused, this tutorial would be a good place to start.

Link to comment
Share on other sites

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.