How to do insert with values posted in textarea

0

I would like to know how I can make use of textarea along with php and mysqli to do the following I need to send 12 items inside a textarea and that these items are put with line break and that apply insert of those 12 items in mysqli being that if you do not post all 12 of error I only need that in max only allow 12 items per textarea, if post less than equal to 12 works perfectly.

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

In case I need to send the text textarea and SQL below so that each position in the case is a column in the media table that goes from 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 ;
    
asked by anonymous 26.07.2015 / 23:12

1 answer

1

The way that you are doing this is not one of the best, nor the most correct way to do it, it would be ideal to create fields of input="text", and with a button go replicating and another to remove, with limit of up to the 12 inputs all as the same name name="urls []", and there in php gather the $ _POST ['urls'] inside a foreach and write the url list, validating each one. However, to do as you want, the way to do this is basically validating both in javascript and in php. In javascript, you limit the field to the maximum number of breaks, which in this case are 11, since the last one does not need to break. And in php, you put all the items in an array, and make a foreach of 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 registry:

$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';";  
    
27.07.2015 / 22:00