How to save an array to separate lines in DB

Asked

Viewed 83 times

0

How can I send a textarea containing multiple names, one per line, so that each line is a record in the database? in addition also to each line receive the amount sent per school and class, I am trying so, but only will a record and in the field nome It’s going blank on DB, I’m using the code below based on Pedro Augusto’s response

Follows form:

  <form action="post.php" method="POST" id="usrform">
  Cidade: <input type="text" name="cidade"><br>     
  Escola: <input type="text" name="siem-id"><br>
  Nivel: <input type="text" name="nivel"><br>
  Turma: <input type="text" name="turma"><br>

  <br>
  Digite um nome por linha</br>
  <textarea rows="4" cols="50" name="nome"></textarea>
  <input type="submit">
  </form>

I’m doing like this:

    <?php 

 //RECEBE OS VALORES VIA POST
 $siem_id = $_POST["siem_id"];
 $cidade = $_POST["cidade"];
 $turma = $_POST["turma"];
 $nivel = $_POST["nivel"];
 $nome_post = $_POST["nome"];

 //QUEBRA A TEXTAREA POR "\n"
 $nome = explode("\n",$nome_post);

 //IMPRIMIR OS VALORES
 echo "Escola: ".$siem_id."<br>";
 echo "Cidade: ".$cidade."<br>";
 echo "Nivel: ".$nivel."<br>";
 echo "Turma: ".$turma."<br>";
 echo "Nome: <br>";
 for($i=0;$i<count($nome);$i++){
     $linha = $nome[$i];
     echo $linha."<br>";

    }

//conectando com o localhost - mysql
$conexao = mysql_connect("localhost","root");
if (!$conexao)
    die ("Erro de conexão com localhost, o seguinte erro ocorreu -> ".mysql_error());
//conectando com a tabela do banco de dados
$banco = mysql_select_db("simrede",$conexao);
if (!$banco)
    die ("Erro de conexão com banco de dados, o seguinte erro ocorreu -> ".mysql_error());

//insere os dados
$query = "INSERT INTO `alunos` (`siem_id`, `cidade`, `nivel`, `turma`,  `nome`) 
VALUES ('$siem_id','$cidade', '$nivel', '$turma', '$linha')";

mysql_query($query,$conexao);

echo " <b>Cadastrados Com Sucesso!</b> "; 

Even prints the correct values on the screen, but in the database the field nome is going blank

  • @Pedroaugusto I believe they are different things, in the post you mentioned is sent everything as a single record, or am I mistaken? this I know how to do,

1 answer

2


First we will correct your form:

<form action="/action_page.php" method="POST" id="usrform">
  Escola: <input type="text" name="escola">
  Turma: <input type="text" name="turma">
  <br>
  Digite um nome por linha
  <textarea rows="4" cols="50" name="nomes"></textarea>
  <input type="submit">
</form>

Now the file that will receive the POST:

    <?php 

//conectando com o localhost - mysql
    $conexao = mysql_connect("localhost","root");
    if (!$conexao)
        die ("Erro de conexão com localhost, o seguinte erro ocorreu -> ".mysql_error());
    //conectando com a tabela do banco de dados
    $banco = mysql_select_db("simrede",$conexao);
    if (!$banco)
        die ("Erro de conexão com banco de dados, o seguinte erro ocorreu -> ".mysql_error());


     //RECEBE OS VALORES VIA POST
     $siem_id = $_POST["siem_id"];
     $cidade = $_POST["cidade"];
     $turma = $_POST["turma"];
     $nivel = $_POST["nivel"];
     $nome_post = $_POST["nome"];

     //QUEBRA A TEXTAREA POR "\n"
     $nome = explode("\n",$nome_post);

     //IMPRIMIR OS VALORES
     echo "Escola: ".$siem_id."<br>";
     echo "Cidade: ".$cidade."<br>";
     echo "Nivel: ".$nivel."<br>";
     echo "Turma: ".$turma."<br>";
     echo "Nome: <br>";
     for($i=0;$i<count($nome);$i++){
         $linha = $nome[$i];
         echo $linha."<br>";
         //insere os dados
         $query = "INSERT INTO `alunos` (`siem_id`, `cidade`, `nivel`, `turma`,  `nome`) VALUES ('$siem_id','$cidade', '$nivel', '$turma', '$linha')";
         mysql_query($query,$conexao);
        }

     echo " <b>Cadastrados Com Sucesso!</b> "; 
  • I’ll test it and get back to you, thank you

  • You do not need to blow up by ' n' to insert into the bank, just insert into the bank by variable $name_post

  • 3

    It really could, but according to the question: "so that each line is a record in the database", so the.

  • Peter only one record is made and the field names is writing "array"

  • 2

    I voted as duplicate, because the answer given is the same thing as the second answer of the closing post (and it is the appropriate solution, make a explode and a loop); In fact the question already starts wrong, because the problem is not what was described. The real doubt is "how to save an array to separate lines of DB". (and the simplest path is to generate a sequence of VALUES: in format INSERT INTO tabela (campos) VALUES (linha1), (linha2), (linha3).... making 1 Insert only for multiple lines (in this case, only separating into more than 1 Insert when the query data volume gets too large)

  • 1

    @Miguelsilva in the answer you should use the query INSERT INTO alunos ( escola, nivel , turma , nomes) &#xA;VALUES ('$escola', '$nivel', '$turma', '$linha') (replacing the $names variable with the $line variable)

  • @Gabriel.Pelegrina, I did as requested, but now even prints on the screen the names of students textarea, but in the database the field name is empty

  • @Bacco, I edited the question as per orientation and added other information, thank you

  • 1

    @Miguelsilva Look at the answer I edited. All right, just run the query and connect to the database

  • @Pedroaugusto, sorry I haven’t been able yet, I edited my question and added the full code I’m using, I can’t find error but the field nome is getting empty in DB.

  • 1

    Place the routine to insert into the for loop. Outside you will not be able to catch the values

  • 1

    @Miguelsilva redacted the answer

  • Perfect! @Pedroaugusto was this, I was putting out of the loop like you said, vlw even, Thanks.

Show 8 more comments

Browser other questions tagged

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