0

I've created a local website to inventory and catalog my comics and I'm wondering how I can possibly expedite the addition of certain records.

For instance, I might want to add 10 sequential issues of a series without having to type mostly redundant data in my form over and over again. I'm wondering if when I'm adding a record I could add a checkbox for multiple issues and maybe have an input for a number with a starting issue parameter or something like that?

I have a form and my insert method:

<form action="addBooks.php" method="post">
<div class="row">
        <div class="col-xs-3">
            <label>Title:</label>
            <input type="text" class="form-control" name="title" required>
        </div>
        <div class="col-xs-3">
            <label>Main Character:</label>
            <input type="text" class="form-control" name="mainCharacter" required>
        </div>
        <div class="col-xs-3">
            <label>Author:</label>
            <input type="text" class="form-control" name="author" required>
        </div>
        <div class="col-xs-3">
            <label>Illustrator:</label>
            <input type="text" class="form-control" name="illustrator" required>
        </div>
    </div>
    <br>
    <div class="row">
        <div class="col-xs-3">
            <label>Issue:</label>
            <input type="text" class="form-control" name="issue" required>
        </div>
        <div class="col-xs-3">
            <label>Cover:</label>
            <input type="text" class="form-control" name="cover" required>
        </div>
        <div class="col-xs-3">
            <label>Year Published:</label>
            <input type="text" class="form-control" name="yearPub" required>
        </div>
        <div class="col-xs-3">
            <label>Comments:</label>
            <textarea class="form-control" name="comments"></textarea>
        </div>
    </div>
<button type="submit" class="btn btn-default">Submit</button>

INSERT INTO comics.comic_inventory 
        (Title,Author,Illustrator,Main_character,issue,cover,publish_date,comments,date_added)
    VALUES ('$_POST[title]',
            '$_POST[author]',
            '$_POST[illustrator]',
            '$_POST[mainCharacter]',
            '$_POST[issue]',
            '$_POST[cover]',
            '$_POST[yearPub]',
            '$_POST[comments]',
            current_timestamp()
            )";

if ($conn->query($sql) === TRUE) {
header("Location: index.php");
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

So I would want to have it enter 10 records with the same data except for the issue number. Maybe I could have an input for starting issue, so if I put '1' as the starting issue it would insert 10 records with issue going from 1 through 10. Does that make sense?

1
  • 1
    If I understand what your attempting to accomplish, what about a simple Javascript function that would populate all the fields with the same data that you entered in the first one? Is that what your trying to do? Commented Nov 19, 2018 at 2:56

1 Answer 1

1

One solution would be to allow the use of a string such as 1-10 in the issue input of your form. Then you could do something like this in your PHP:

if (strpos($_POST['issue'], '-') !== false) {
    $issues = range(...explode('-', $_POST['issue']));
}
else {
    $issues = array($_POST['issue']);
}
foreach ($issues as $issue) {
    $sql = "INSERT INTO comics.comic_inventory (Title,Author,Illustrator,Main_character,issue,cover,publish_date,comments,date_added)
        VALUES ('$_POST[title]',
                '$_POST[author]',
                '$_POST[illustrator]',
                '$_POST[mainCharacter]',
                '$issue',
                '$_POST[cover]',
                '$_POST[yearPub]',
                '$_POST[comments]',
                current_timestamp()
                )";    
    if ($conn->query($sql) === false) {
        echo "Error: " . $sql . "<br>" . $conn->error;
        exit;
    }
}
header("Location: index.php");

You can see the issue number splitting code working on 3v4l.org

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.