Answer the question
In order to leave comments, you need to log in
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
Empty inputs are not sent to the server, you will receive two values in $_POST['your-field']. What is the problem?
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']));
}
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.
$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 questionAsk a Question
731 491 924 answers to any question