How to Insert with values posted in textarea

Asked

Viewed 131 times

0

I would like to know how I can make use of textarea together with php and mysqli to do the following need to send 12 items within a textarea and that these items are put with line break and that apply these 12 items in mysqli and if not post all 12 not error only need max only allow 12 items per textarea, Pole case smaller than equal to 12 works perfectly.

Because I have in the table 12 fields to put the links.

In case I need you to send the form textarea and SQL below so that each position in the case and a column in the table media that goes from the column link1 to link12.

<textarea rows="12" name="links">
link1
link2
link3
link4
link5
link6
link7
link8
link9
link10
link11
link12
</textarea>

Mysql

CREATE DATABASE `media` ;
CREATE TABLE `media`.`medias` (
`id` INT( 5 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`link1` VARCHAR( 255 ) NOT NULL ,
`link2` VARCHAR( 255 ) NOT NULL ,
`link3` VARCHAR( 255 ) NOT NULL ,
`link4` VARCHAR( 255 ) NOT NULL ,
`link5` VARCHAR( 255 ) NOT NULL ,
`link6` VARCHAR( 255 ) NOT NULL ,
`link7` VARCHAR( 255 ) NOT NULL ,
`link8` VARCHAR( 255 ) NOT NULL ,
`link9` VARCHAR( 255 ) NOT NULL ,
`link10` VARCHAR( 255 ) NOT NULL ,
`link11` VARCHAR( 255 ) NOT NULL ,
`link12` VARCHAR( 255 ) NOT NULL ,
) ENGINE = MYISAM ;
  • what you have already managed to do? has some example to add in the question ?

  • Unfortunately I not only have the text area and Mysql data.

  • Show how data is sent in the textarea?

  • I edited the question

  • The ideal was a single link column plus the id from some other table. If it is not possible to change this from $link = explode("\n",$_POST['textarea']);

  • in case for me to put in the position I want I will have to do something like $link1 = $link[0]; until $link12 = $link[11]; right ?

Show 1 more comment

1 answer

1


The way you are doing this is not one of the best, nor the most correct way to do it, the ideal would be to create input fields="text", and with one button replicate and another to remove, with a limit of up to 12 inputs all as the same name name="urls[]", and there in php collect the $_POST['urls'] within a foreach and record the url list, validating each one. However, to do as you want, the way to do this is basically by validating both javascript and php. In javascript, you limit the field to the maximum number of breaks, which in this case are 11, since the latter need not break. And in php, you put all the items in an array, and you foreach the values:

Then it would look something like this:

Javascript to filter:

function checkUrl(str) {
  var pattern = new RegExp('^(https?:\/\/)?'+ // protocol
    '((([a-z\d]([a-z\d-]*[a-z\d])*)\.)+[a-z]{2,}|'+ // domain name
    '((\d{1,3}\.){3}\d{1,3}))'+ // OR ip (v4) address
    '(\:\d+)?(\/[-a-z\d%_.~+]*)*'+ // port and path
    '(\?[;&a-z\d%_.~+=-]*)?'+ // query string
    '(\#[-a-z\d_]*)?$','i'); // fragment locater
  if(!pattern.test(str)) {
    return false;
  } else {
    return true;
  }
}

function getListURL(valor, total) {
var listValid = 0;
var urlValida = [];
   var urls = valor.split("\n");
   for (var i = 0; i < total - 1; i++) {
      if (checkUrl(urls[i])) {
           urlValida[listValid] = urls[i];
           listValid++;
      }
   }
  return urlValida;
}

function setFilter() {
var arrUrls = getListURL(document.urls, 12);
    document.sendURLs.urls = arrUrls.join("\n");
    document.sendURLs.submit();
}

PHP to capture and update the record:

$urls = explode("\n", $_POST['urls']);
$id = $_POST['id'];

$cps = array();
$pos = 1;

foreach ($urls as $url) {
  if (filter_var($url, FILTER_VALIDATE_URL) !== false && $pos <= 12) {
         $cps[]  = "link" . $pos ." = '{$url}'";
        $pos++;
  }
}

$campos = implode(", ",$cps);
 $sql = "UPDATE media SET $campos WHERE id = '$id';";  

Browser other questions tagged

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