how to insert a variable that brings an array into the database?

Asked

Viewed 416 times

1

I have a page that has a textarea and in this textarea the user will put a list of records and in these records I separate the strings with the function explodes

this is my html index.php code

Mtms Ativos #login{ text-align: center text-align margin: 0 auto; padding:40px 15px; }

label{
    display: block;
    margin-right: 100px;

    padding: 5px;
}

#text{
    width: 500px;
    height: 480px;
}

Function valida(){ if(form.text.value==""){ Alert('Fill in field'); form.text.Focus(); Return false; } Alert("Material required successfully!"); }

Alert("test");

--> Material:

        <label for="submit"></label>
        <input type="submit" name="enviar" class="input_button" id="bt_logar" value="enviar" />
    </form>

this is the insert.php code that contains the query

<?php 

    $conexao = @mysql_connect("localhost","root","") or print("erro");
    if (!$conexao) die ("<h1>Falha na conexao com o servidor!</h1>");
    $db = @mysql_select_db("test");
    if (!$db) die ("<h1>Falha na conexao com o Banco de Dados!</h1>");



    $text = $_POST["text"];
    //echo $text;

    $alterado = array("-");
    $alterar = array("");
    $replace = str_replace($alterado,$alterar,$text);   

    $novaString = chunk_split($replace,12,".");
    //echo $novaString;

    $bat = explode('.',$novaString);
    print_r($bat)."<br>";

    //insere normalmente
    $sql = @mysql_query("insert into test.tab_mtm_ativos (MTM,planta) values ('$bat','B510')  on duplicate key update mtm = values(mtm)") or die ("erro");

    @mysql_close($conexao, $sql);
    header('location: mtm.php');

?>

  • I advised you to encode as json in the answer below, but remember that by default it is not correct to do this in a relational database. If you are looking for multidimensional and dynamic fields, look for a no-sql solution

1 answer

1

Well, in a relational database it is not possible to insert the array type into a table. You will have to insert as a string, what can be done is to choose a string format that covers vectors, such as JSON or XML.

As it is a simple array, I advise you to convert it to JSON before inserting it into the database with the json_encode function, and every time you search for this data decode it with the json_decode function.

After you explode the array, encode it like this:

 $bat = explode('.',$novaString);
 $bat = json_encode($bat);

This will make your $bat array, turn a string, when you need to fetch it to use it as an array again, use:

$bat_array = json_decode($bat);

For more information, visit:

http://php.net/manual/en/function.json-encode.php

and

http://php.net/manual/en/function.json-decode.php

  • I used this json_encode function to continue inserting in array in the database

  • i got it wrong, I thought you wanted to insert the array. You can paste here as it is being inserted into the database?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.