0

I need to build a page where the user can enter the name of the categories of products of an webstore. I created a page with dynamic input fields so the user will be able to add all the categories (which i don't know the ammount). I have to get the name of those categories and put into a database. I got a script online and made a few changes but i dont know how to proceed further on. I need help with the php script, how to get the array and insert into the database.

This is my form script:

<form method="post" action="setup3.php">
    <div class="input_fields_wrap">
        <button class="add_field_button">Add More Fields</button>
        <div>
            <input type="text" name="mytext[]">
        </div>
    </div>
    <div>
        <input type="submit" value="Next" />
    </div>
</form>
5
  • what do you get when you put some values in the form above and do var_dump($_POST) on setup3.php page ? Commented Apr 14, 2015 at 20:54
  • I get this: array(1) { ["mytext"]=> array(3) { [0]=> string(5) "test1" [1]=> string(5) "test2" [2]=> string(5) "test3" } } Commented Apr 14, 2015 at 21:00
  • i think that all I need is a loop where a get the current element of the array and add it to the database, but i dont know how to do it, not even start. This is the first time i programm for web. Commented Apr 14, 2015 at 21:01
  • 1
    Google: "php iterate through arrays". You need to put in work from your side and then post the problem here if you run into any issues. You might want to start with some basic PHP tutorials first. Commented Apr 14, 2015 at 21:03
  • php.net/manual/en/control-structures.foreach.php Commented Apr 14, 2015 at 21:06

1 Answer 1

1

I think this would point you in the right direction:

<?php
if (! empty($_POST['mytext'])) {
    foreach ($_POST['mytext'] as $entry) {
        // perform your insert here.
    }
}

I would strongly recommend using an ORM like Doctrine (http://www.doctrine-project.org/) since it already tackles a lot of security issues for you.

If you wish to stay pretty close to low-level database interactions, writing plain SQL queries, I'd recommend using PDO (http://php.net/manual/en/book.pdo.php)

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.