Remove line break after each INSERT

Asked

Viewed 362 times

3

I own a textarea in my code, where the user will insert access vouchers separated by ENTER.

My PHP code treats the typed values as follows:

$voucher = explode("\n",$voucher);
      foreach ($voucher as &$varray)
      {
        $gravar = mysqli_query("INSERT INTO `vouchers` (voucher, tempo) VALUES ('$varray','$tempoBD')");
      }

Where: $voucher is the value of the textarea received via $_POST and $tempoBD is the value that the user select converted in format 00:00:00.

The problem is: The voucher field is saving the value of the line + line break (\n)!

What correction to my code need to be done to remove the line break in the voucher field for each record entered in the table?

Thank you!

2 answers

3

Use the trim() to remove line breaks.

$voucher = explode("\n",$voucher);
foreach ($voucher as &$varray){
    $gravar = mysqli_query("INSERT INTO `vouchers` (voucher, tempo) VALUES ('".trim($varray)."','$tempoBD')");
}
  • Yeah, his answer is even better. Put he needs to pick up the spaces, got!

  • I must be doing something wrong. You are saving in the bank as {Trim( + line value + )}. I will try to correct the syntax here

  • Dude, the syntax I solved. The problem is you kept saving it with a line break in the bank! If I give the UPDATE vouchers SET voucher = TRIM(REPLACE(REPLACE(voucher, CHAR(13),'), CHAR(10),')) command in the bank, it changes the records. Can you help me?

  • @Jhonatanjunio are you sure it’s line breaking? the trim() should solve. It is not a <BR>?

  • @Lucascarvalho crase goes in table names, views, identifier fields in general is the escape character. Simple quotes only goes in values.

  • I did not know that, I never actually used. Thanks rray

  • I do, @rray .. It’s just that I didn’t talk, I’m taking the values of a textarea and breaking ENTER with n. In the bank is coming the value of the field+a line break. In Heidisql it even says "Line break Mac OS detected"

Show 2 more comments

2


Use strip_tags next to Trim, strip_tags will remove any html or php code from your file and Trim will remove the spaces from the beginning and the end.

$voucher = explode("\n",$voucher);
foreach ($voucher as &$varray){
    $formata = strip_tags(trim($varray));
    $gravar = mysqli_query("INSERT INTO `vouchers` (voucher, tempo) VALUES ('".$formata."','$tempoBD')");
}
  • But this way does not solve my problem, because it is a textarea, as I explained in the question. I need to treat every line as a bank record

  • Try now @Jhonatanjunio

  • NOW YES! hehehe; Thank you very much, @Lucas Carvalho! Solved my problem. I didn’t know the strip_tags. Really, thank you!

Browser other questions tagged

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