Note that there are some explanatory texts on larger screens.

plurals
  1. POAutomate VALUEs based on how many inputs the form contains
    text
    copied!<p>How do I make an automated query that adds another VALUES row based on how many inputs with the names: name, ingredients and price there are?</p> <p><strong>The form looks like this:</strong></p> <p><img src="https://i.stack.imgur.com/3gRwf.png" alt="the form"> <em>When you press new row there will be another 'New item' box showing up and I want the SQL to record how many rows there are. Each 'New Item' = new value for the query.</em></p> <p><strong>SQL:</strong></p> <pre><code>if($_SERVER['REQUEST_METHOD'] == 'POST'){ $status_input = $stmt = $dbh -&gt;prepare(" INSERT INTO menues( restaurant_id, title, subtitle, name, ingredients, price, category, upload_date) VALUES (:restaurant_id,:title, :subtitle, :name, :ingredients, :price, :category, NOW()) (:restaurant_id,:title, :subtitle, :name, :ingredients, :price, :category, NOW()) (:restaurant_id,:title, :subtitle, :name, :ingredients, :price, :category, NOW()) "); $stmt-&gt;bindParam(":restaurant_id", $userdata[0]['user_id']); $stmt-&gt;bindParam(":title", $_POST['title']); $stmt-&gt;bindParam(":subtitle", $_POST['subtitle']); $stmt-&gt;bindParam(":name", $_POST['name']); $stmt-&gt;bindParam(":ingredients", $_POST['ingredients']); $stmt-&gt;bindParam(":price", $_POST['price']); $stmt-&gt;bindParam(":category", $userdata[0]['category']); $stmt-&gt;execute(); } </code></pre> <p>Restaurant_id, title, subtitle, category and upload_date should all be the same for each row.</p> <p><strong>jQuery and HTML:</strong></p> <pre><code>$(document).ready( function() { $('.newmenu').hide(); $('.zero h3 a').click(function() { $('.newmenu').slideToggle(); }); $('.newmenu &gt; ul li p.add').click(function(){ $('.newmenu &gt; ul li.edit').before("&lt;li class='newrow'&gt;&lt;h6&gt;New item&lt;/h6&gt;&lt;div&gt;&lt;p&gt;Name:&lt;/p&gt;&lt;input type='text' name='name' placeholder='name' /&gt;&lt;/div&gt;&lt;div&gt;&lt;p&gt;Ingredients:&lt;/p&gt;&lt;input type='text' name='ingredients' placeholder='ingredients' /&gt;&lt;/div&gt;&lt;div&gt;&lt;p&gt;Price:&lt;/p&gt;&lt;input type='text' name='price' placeholder='price' /&gt;&lt;/li&gt;"); }); }); &lt;form method='post' action='newmenu.php' class='newmenu'&gt; &lt;table&gt; &lt;tr&gt;&lt;td&gt;Namn:&lt;/td&gt;&lt;td&gt;&lt;input type='text' name='title' placeholder='namn' /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;Undertext:&lt;/td&gt;&lt;td&gt;&lt;input type='text' name='subtitle' placeholder='namn' /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;ul&gt; &lt;li class='newrow'&gt; &lt;h6&gt;New item&lt;/h6&gt; &lt;div&gt; &lt;p&gt;Name:&lt;/p&gt; &lt;input type='text' name='name' placeholder='name' /&gt; &lt;/div&gt; &lt;div&gt; &lt;p&gt;Ingredients:&lt;/p&gt; &lt;input type='text' name='ingredients' placeholder='ingredients' /&gt; &lt;/div&gt; &lt;div&gt; &lt;p&gt;Price:&lt;/p&gt; &lt;input type='text' name='price' placeholder='price' /&gt; &lt;div&gt; &lt;/li&gt; &lt;li class="edit"&gt; &lt;input type="submit" value="Submit"&gt; &lt;p class="add"&gt;New row&lt;/p&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/form&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload