0

I can't think of an easy way to explain what I'm trying to accomplish..

Inserting data into MySQL using php is simple, yet I need to be able to give users the option to add more text inputs in one form...

Just for example purpose...

Users can create a shopping list, the page loads with 15 inputs for 15 items they wish to insert into their shopping list...

At the bottom, they can have the option to add another item, and when clicked, it will show an additional text input..

I've looked for examples but off the top of my head I can't think of any...

if(isset($_POST['createList']){
    $item=addslashes(strip_tags($_POST['item']));
}
mysqli_query("INSERT INTO shoppingLists (id,itemName) VALUES (``,`$item`)");

How do insert multiple items with a simple POST? I was hoping it's possible to use JQuery to add additional input fields.. but how is something like this accomplished on the PHP side?

I do hope I've explained this well enough haha.

3
  • 8
    for some reason this code looks highly injectable. Commented May 24, 2013 at 14:54
  • 1
    Arrays, you're gonna need them. Oh and your code is not safe. Commented May 24, 2013 at 14:56
  • 1
    1. Don't use $_POST directly in your queries. 2. Don't use mysql_* functions, they are deprecated. Use prepared statements instead. Commented May 24, 2013 at 15:57

3 Answers 3

3

You can use an array for your input name attribute

<input type="text" name="item[]" />

And you can browse it by looping through your variable $_POST['item'], that now contains an array with an entry for each field in your form.

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

4 Comments

I’d have given the same answer … and I think this is actually what he needs. (That he knows how to loop over an array I would just presuppose.)
Yes CBroe, maybe partial, but he should start with this. @ogc-nick: "How do insert multiple items with a simple POST" that is what I tried to answer.
Thank you! I've spent allot of time designing lately, and I know it's most likely a dumb question, but it needed an answer! Thank's again guys!
if(isset($_POST['finish'])){ $items = array_map('strip_tags',$items); $items = array_map('addslashes',$items); echo $items; }
1

I use jQuery .clone() for this.

html:

<div id=="ShoppingList">
    <input class="item" name="item[]" />
    <input type="button" onclick="addAnotherItem()" />
</div>

js:

function addAnotherItem(){
    $("#ShoppingList input.item:first").clone().val("").appendTo("#ShoppingList");
}

I use .val("") so that whatever value the first input has isn't copied to the new one.

1 Comment

Perfect too o/c-nick, my question was basically in two parts.. Both parts have been answered! Cheers mate :)
0

Sample Code For This inserting multiple images

            if(isset($_POST['addSpace'])){
                $spaceTitle = mysql_real_escape_string($_POST['title']);
                $spaceBody = mysql_real_escape_string($_POST['text']);




                if($_FILES['SliderImage']['tmp_name'] != "" ){
                    if (($_FILES["SliderImage"]["type"] == "image/jpeg")
                    && ($_FILES["SliderImage"]["size"] < 2000000))
                    {
                        if ($_FILES["SliderImage"]["error"] > 0)
                        {
                            echo "<div class='error_box'><p>Error :: " . $_FILES["SliderImage"]["error"] . ".</p></div>'";
                        }else{
                            $path = "../images/prisma-img/demo/services/";
                            $path2 = "images/prisma-img/demo/services/";
                            $num = mt_rand();
                            if (file_exists($path . $num.".jpg" ))
                            {
                                echo "<div class='error_box'>"."(".$num .")".
                                " already exists. "."</div>";
                            }else{
                                if(move_uploaded_file($_FILES["SliderImage"]["tmp_name"],$path . $num.".jpg"  )){
                                    $mysqlPath = $path2. $num.".jpg"  ;
                                    $result = $db->insert("pages","pageTitle, pageImage, pageBody, pageSlug ", "'$spaceTitle','$mysqlPath','$spaceBody','services'");
                                    if($db->affected_rows()){

                                        $id=mysql_insert_id();
                                        echo '<div class="valid_box"><p>Success :: Services successfully Added.</p></div>';
                                        echo "<meta http-equiv='refresh' content='1; url= add-services-slide.php?id=".$id."' />";
                                    }
                                }
                            }
                        }
                    }else{
                        echo '<div class="error_box"><p>Error :: Only JPEG file allowed.</p></div>';
                    }
                }
            }
        ?>

Hope that this will help u.

1 Comment

Please elaborate, explain what your code does and how it solves the problem stated in the question.

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.