How do I save an array in the column of a database record, using PHP?

0

I'm doing it in the following way

but in the bd it keeps me "array" and not the fix, can you help me please.

<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript" src="getmapa.js"></script>
    <link rel="stylesheet" type="text/css" href="style.css">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <?php include("conexion.php"); 
      $link=conectar();
    ?>

    <meta charset="utf-8">
    <title>Nuevo viaje</title>



  </head>
  <body >
  <?php include("head.php");?>
    <div class="container contedor">
    <div class="titulo-contenedor">Nuevo viaje</div>
      <div class="container-int">
        <form class="container-int" action="guardarviaje.php" method="post">
<select class="refor selectpicker" multiple name="paradas[]" multiple data-actions-box="true">
<?php
      $consulta = "SELECT * FROM wck_parada ORDER BY id";
      $ejecutar = mysql_query($consulta, $link);
      while ($fila = mysql_fetch_array($ejecutar)) {
        $id = $fila['id'];
        $nombre = $fila['nombre'];      
    ?>
<option type="checkbox" value="<?php echo utf8_encode($id); ?>" name="nombre"><?php echo utf8_encode($nombre); ?><br></option>
<?php }
          mysql_close();
          ?>
</select>
</form>

$paradas=$_POST['paradas'];

mysql_query ("SET NAMES 'utf8'");


mysql_query("insert into wck_viaje(paradas)values('$paradas')", $link);

          </div>

    </div>
  </body>
</html>
    
asked by Sergio 30.06.2017 в 03:14
source

2 answers

0

Let's do this simple, the development is not to complicate us. in the front it is done in this way:

in the backend you receive the data by POST method in the following way, if you want to separate it by commas it is:

$idbd = implode(', ', $_POST['id']);

and save in the bd with a

 mysql_query("insert into tabla(id)values("$id");

and ready to save it without quotes

    
answered by 30.06.2017 / 15:54
source
1

Considering this line

insert into wck_viaje(paradas)values('$paradas')

You could assume that you want to save the entire array in a single registry, so "the most practical solution" based on your database structure is to serialize the data you can use either:

Solution using json_encode

$dato = json_encode($paradas);
mysql_query("insert into wck_viaje(paradas)values('$dato')", $link);

Solution using serialize

$dato = serialize($paradas);
mysql_query("insert into wck_viaje(paradas)values('$dato')", $link);

The methods described above do nothing but codify a structure in the form of a string, once it is easy to insert them as a value in the field of a record. Although particularly to save a simple fix, I would use json_encode , since the purpose of serialize is the storage of values in PHP without losing its type and structure, for example when you want to save a connection object to the database.

Now when you want to retrieve the information, you must apply the reverse process using json_decode or unserialize for the respective cases.

PS: «Some recommendations»

  • You should not use the mysql API instead rehearse with mysqli or PDO, the latter allows you to use other database engines such as Postgres, firebird, etc.

      

    This extension is obsolete as of PHP 5.5.0, so it has been   deleted from PHP 7.0.0 ... link

  • You should reframe the design of your database a little bit, I do not see how you relate the table wck_viaje and also I do not think the design in general is very good.

answered by 30.06.2017 в 03:42