S
S
SimBioT192015-09-24 13:01:46
PHP
SimBioT19, 2015-09-24 13:01:46

How to organize a request correctly?

I have 8 inputs for photos, which the user fills in by specifying a link to the desired image.
How will it be correct to organize the insertion of links to images into the database, provided that the user does not necessarily fill in all the fields, and he can also "skip" any of them? For example, enter links only 5 and 8 input.
How will it be correct to compose a query using PDO?
It turns out that 8 (maximum) rows are sent to the database, but how to insert the required number in one request?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey Ukolov, 2015-09-24
@alexey-m-ukolov

Empty inputs are not sent to the server, you will receive two values ​​in $_POST['your-field']. What is the problem?

S
Sergey Zelensky, 2015-09-24
@SergeyZelensky-Rostov

make simple requests through a loop through an array of images like this:

foreach ($imageUrl as $item) { 
      $sth = $db->prepare('INSERT INTO `imageCollection`(url,alt,title) VALUES(?,?,?)');
      $sth->execute(array(item['url'],item['alt'],item['title']));
   }

V
Vladimir Martyanov, 2015-09-24
@vilgeforce

It can be done through several requests: in a cycle, check the fields and if the field is not empty, add it.
Second option: INSERT INTO pictires (`ur`) VALUES ($url1), ($url2), ... and add urls if they are given.

N
nozzy, 2015-09-24
@nozzy

$sth = $db->prepare('INSERT INTO `imageCollection`(url,alt,title, .... etc.) VALUES(?,?,? ..... etc.) ');
$sth->execute(array(
isset($_POST['url']) : $_POST['url'] ? null,
isset($_POST['alt']) : $_POST['alt'] ? null,
isset($_POST['title']) : $_POST['title'] ? null,
.... etc.
));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question