0

I have a table named item in my mySql database with the columns named item and price. With jquery, I used a button that generates fields for the input of multiple items and their price. But what should be the php codes for looping through the inputs for inserting them respectively? (Like item 1 with price 1) Some thing like this that I'm trying to achieve

<script>
$(document).ready(function() {
    var count = 0;

    $('p#add_field').click(function(e) {
        e.preventDefault();
        count += 1;
        $('#container').append(
            'Item: <input type="text" id="item_' + count + '" name="item[]' + '"/>\n\
             Unit price: <input type="text" id="price_' + count + '" name="price[]' + '"/><br>'
        );
    });

});
</script>

<?php
if (isset($_POST['btnSubmit'])) {
    if ($_POST['item']) {

        foreach ($_POST['item'] as $key => $value) {
            $sql_item    = sprintf("INSERT INTO item (item) VALUES ('%s')", mysql_real_escape_string($value));
            $result_item = $db->query($sql_item);
        }        
    }
    echo "<h1>User Added, <strong>" . count($_POST['fields']) . "</strong> item(s) added for this user!</h1>";
    $db->kill();
}
?>
<?php if (!isset($_POST['btnSubmit'])) { ?>
    <form action="" method="POST">
        <div id="container">
            <label>Available items</label>
            <p id="add_field"><button type="button" href="#">Add new item</button></p>
        </div>
        <button type="submit" name="btnSubmit">Save items</button>
    </form>
<?php } ?>
1
  • Could you not just make an items table with an auto increment ID column, item name and item price column? Also you could remove the else statement after the if ($_POST['item']). Commented Sep 23, 2017 at 7:47

1 Answer 1

1

Your storage of data is abnormal, change the item table so it has an auto increment ID column and an item name and item price column.

Change your code in the foreach loop to reflect the table change.

Your approach to sanitizing your SQL query is not the best way, you are using mySQLi to connect to the database so use the class' built in function to prepare and execute queries.

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

4 Comments

Can you kindly give an example how I should write the foreach loop with the changed table structure that you've suggested? Really appreciate your help.
I was trying something like this - foreach(array_combine($_POST[‘item_name’] , $_POST[‘item_price’]) as $iname=> $iprice) { $a = mysql_real_escape_string($iname); $b = mysql_real_escape_string($iprice); $sql = mysql_query ("INSERT INTO item (item_name, item_price) VALUES ('".$a."','".$b.”’)”); } It somehow does the job but I'm not sure if I'm doing it the right way.
You don't need to do that, did you check the link in my answer?
@RajibBiswas check the link I have given and provide an attempted solution we can help you from there.

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.