Duplicate form entries: How to send through to mysql database - php

I have a page where a user can create a recipe. I have a javascript script that allows the user to keep adding form input elements for more ingredients. Say the recipe has 4 ingredients the user can continue to add inputs. However this changes the id to ingredient1/2/3/4 for the input ID/Name,and i can't understand how to get around getting the information from the form post as the amount of elements is upto user discretion. How would you go about loading the results into PHP variables and then inserting them into a mysql query.
the syntax for the html form
<div id="ingredient1" class="clonedInput">
<h2 class="first-column">Ingredient</h2>
<span style="float:left; padding-right: 10px;">
<input type="text" name="scroll" value="Add Ingredient" id="ingredient" class="ingredient"/>
</span>
</div>
ingredient1 would change to ingredient2 for the purpose 2 ingredient's being added.
A slightly confusing problem, so any help would be amazing!
JB

I think what you are looking for is using [] in your input element names. This will post the data as an array. So, say you have this as your form's ingredient input elements (after javascript has added several):
<input type="text" name="ingredients[]" value="" />
<input type="text" name="ingredients[]" value="" />
<input type="text" name="ingredients[]" value="" />
<input type="text" name="ingredients[]" value="" />
<input type="text" name="ingredients[]" value="" />
When the user POST's the data, you will be able to access the array of ingredients at $_POST['ingredients']

I will tell you the logic, not the code as only javascript here would not be enough.
you create a table ingredients. Anything you can allow them to use. id, ecc
while posting, instead of doing giving the user a freedom chosing their ingredient1, ingredient2 ... you do load from ingredients table the values and index them as ingredient[id_from_db] ecc..
you create a table recipe. It should have a unique id, its description
you create a relation table recipe_id, ingredient_id
then when the user post its data you create 1 row in the recipe and as many rows in the relation table as the ingredients they use.
When selecting later the recipe you also select everything which is in the relation table, it could be several rows there...
Hope it is of any help for you. Can't show you any code for this, but it is not hard to make if you understand how it goes.
maxim

Related

How can I use a number input to make an array?

edit: Somebody in the answers recommended a list, and I think that that is a good idea, but then I have no idea of how to implement that with the amount as well as the type. So if someone could explain that?
I have to make a fast food order form for school, and I decided to make you able to enter the amount as an input instead of checkboxes, but I still want them stored in an array. Is there any way to do that? Here's a little snippet of what I have in html:
<form>
<table class="menu" style="width:100%;">
<tr>
<td class="productlist">
<!-- Drinks -->
<input type="number" name="drinks[]" value="cokeclassic" id="cokeclassic" class="productCheckBox" min="0" max="20" placeholder="0">
<label for="cokeclassic" class="product">Coca Cola Classic $0.75<br><br></label>
</tr>
</table>
<input type="submit" value="Cart" class="submit">
</form>
If you want them stored in an array, you would need to have multiple input fields with name="drinks[]". All of your inputs will be sent as one array called drinks, containing numbers passed in each field.
If you want to pass name of a drink and an amount of it, you would need something like name="drinks[0].name" for name of your drink and name="drinks[0].amount" for amount
Why you would like to store them in an array?
When submitting the form, the input-tag can only deliver a single value anyways, because there is only a single input.
Try to do a list/stack with the orders or even try to create a dynamic multiselect including amounts when ordering.

Insert quantity of each types of item

I'm confused on how to actually let my form insert the data into the bag table that I have created. The system is supposed to store the bag of the customer where the bag_id is the customer id.
The number of rows generated for each customer in the bag table is dependent on the sizes of the bag they are storing.
The picture below is how I want the data to be inserted.
However, I can't figure out how to insert the data the way I wanted. This is what I try to do to get the number of each type of size:
<label>Bag Size</label></br>
<input type="text" name="big" id="big" class="form-control" style="width:5%;"/><label>Big</label>
<input type="text" name="medium" id="medium" class="form-control"style="width:5%;" /><label>Medium</label>
<input type="text" name="small" id="small" class="form-control"style="width:5%;" /><label>Small</label>
Any suggestion on the way to enter the data is appreciated.

How to structure php code to populate 280 fields in a form if a record exists in mysql

I have a html form with about 280 text, radios and select dropdown fields. I have to capture a lot of information about people's outgoings. Eg. Who their gas and electricity providers are, how much they pay, the rates, their bank accounts, loan accounts, broadband etc. The list of fields sounds huge, but it's laid out in tabs and it isn't too overwhelming and people don't have to fill in all of it anyway.
I present the blank form to the new user and then post every field value (blank or not) to the mysql dbase on the first submit.
The problem is.. how do I retrieve all that data and put it into the form the next time? I'm out of my depth with elegant solutions, so I guess I'd do a foreach loop through each database record retrieved and save it to a variable, but then I have to insert the 280 field values into the fields using an statement in the value="xxx" for each field, something like:
<div class="form-group row">
<label for="loan1name" class="col-lg-5 control-label text-right">Loan Name</label>
<div class="col-lg-3">
<input type="text" class="form-control" name="loan1name" placeholder="Car loan" value=<?php echo htmlspecialchars($loan1name) ?>>
</div>
</div>
If I had 10 fields it wouldn't matter, but 280 is unmanageable doing it that way. Is there any other way? Do I need to go about this a completely different way? Does it make sense to duplicate the form and use one for the blank one which does the INSERT, then another for the pre-populated version for the UPDATE query? Any help would be greatly appreciated. I just know there must be a better way, but I can't find any answer on here or anywhere else.
It doesn't seem simple because the fields are quite varied and one is even a set of pictures as a radio button sequence (it's pictures of types of locks you have on your front door - for your home insurance).
Mysql usually return an associative array. You can then use the key/value pairs in this array to dynamically create your form.
<?php foreach($fields AS $name => $value){ ?>
<input type="text" name="<?= $name ?>" value="<?= $value ?>" /><br />
<?php } ?>
Note: The downside is that you are still missing the label and placeholder of each input. Also, they would all be text input.

Add table to textbox in html

I have a food recipes site, users are able to submit their own recipes, on the page where you submit your recipes there are textboxes to enter the description of the recipe.
What I need is basically, when users open the page with the form, on the textbox I want to appear a table there (I already have the code for the table). I just don't know where to put that code. I was wondering if I could use value="table code here" for the table to appear in the text box? Or is there another way?
Here's the code of the textbox:
<label for="inputPassword3" class="col-sm-3 control-label"><?php echo RECIPE_DESCRIPTION; ?></label>
The tag does not allow you to pass in a tag. What I would recommend however is creating a and adding tags inside of your individual table row's cells.
Ex:
<table>
<tr><td><input type="text" /></td></tr>
</table>

Reading information from two tables into two forms at once

For my first attempt at programming, I am making a simple webapp with php and MySQL, to enter customer information and store information about orders.
So far I've made a form to enter customer information, a page to edit that information and another page to show the different customers, which is all working fine.
I've run into a stump when trying to implement the next stage however.
I have an editcustomers page with 3 columns.
Column 1 shows a form containing customer information, allowing it to
be edited.
Column 2 allows ordering of products and showing how many products were ordered
Column 3 shows the total paid so far, and the total owing.
The code for the page I have at present:
<html>
<?php
$id = $_GET['id'];
require_once('connect.php');
$sth = $dbh->query("SELECT * FROM customers where id = '$id'");
$sth->setFetchMode(PDO::FETCH_ASSOC);
?>
<div id="main">
<div id="left">
<form name="myForm" action ="process.php" method ="post" >
<?php while($row = $sth->fetch()){ ?>
<p><input type = "hidden" name ="id" value="<?php echo $row["id"] ?>"/>
<p><input type = "text" name ="firstName" size ="30" value=" <?php echo $row["firstName"]?>"/>
<p><input type = "text" name ="lastName" size ="30" value="<?php echo $row["lastName"]?>"/>
<p></p>
<input type="submit" value="Update" />
<?php }?>
</div>
<div id="mid">
<p>Amount owed<br />
<input type = "text" name ="moneyOwed" size ="30" /></p>
<input type="submit" value="Pay" />
<p>Number of aaaa<br />
<input type = "text" name ="numAaa" size ="30" /></p>
<p>Number of bbbb<br />
<input type = "text" name ="numBbb" size ="30" /></p>
<p>
<input type="submit" value="Update" />
</div>
<div id="right">
<b>Total Balance</b>
<p> Money owed: </p>
<p> aaa total: </p>
<p> bbb total: </p>
<p> Total: </p>
<input type = "text" name ="pay" size ="20" /></p>
<input type="submit" value="Make Payment" />
</div>
<?php
$dbh =null;
?>
</body>
</html>
I have the following database structure:
A customers table, basic information like firstname, lastname, etc along with an id field.
A products table, listing the different products, only 1 row containing the costs for each product.
A purchases table, with an id field , fields like numProductA, numProductB, showing the quantity of each product ordered
The problem I'm having is that in my editcustomerspage, I have a database query to read in information from the customers table to fill the fields in the form in my first column and a separate query to update it if that if the function chosen.
Updating the second formshould be OK, as I could use a hidden field to differentiate forms, however I am unsure how to read the information in from the Customers table and from the Purchases table so that I can populate the fields in my second form..
Part of the problem is that the purchases table may be empty for a given customer, if that customer has not yet placed an order.
I was considering getting rid of the purchases table, and tacking the fields onto the users table which would solve my problem, although I think that is generally considered bad practice?
The products will be fixed in the application, and are not going to change. Even so, would it still be better to have column names cost and name and each products as a record?
What are some approaches I could take to solving this problem?
Even what you currently are doing is considered bad practice because of the columns numProductA, numProductB. What happens when you'll have 1000 products and a user orders different amounts from all of them. This work only if the list of products is fixed (i.e. coded into the application) and requires a lot of coding.
You would probably want to create a purchase_item table that has fields like: id, purchase_id, product_id, num_product. It would be like a line on a standard bill: purchase_id identifies which bill the line belongs to; product_id would show the product that line is about and num_product would show the amount (multiplier) on that line.
In this manner you don't need to change the code when new products are added to the database.
Without a detailed description of the problems you've run into, it's hard to say how to fix them. It should be possible to implement what you want with the described design. However, you seem to be misunderstanding relational databases, which is why your database architecture is eyebrow-raising.
A database table is not a spreadsheet. While a table can have millions of rows, the number of columns is usually limited to a much smaller number. Even when the limit is not a problem, the way relational databases are designed does not lend itself well to storage of uniform data across a large number of columns. For example, your database won't be able to answer questions such as “what's the best-selling product”, “what products have been purchased by more than 100 customers”, etc.
The rule of thumb is that each table should contain a fixed number of columns. If you find that you'll have to add columns when you e.g. start selling new products, you're doing it wrong.
Here's what you should probably do in your application:
products should be a table with one row per available product, containing, for each product, a unique id, the price and other information such as name, manufacturer etc.
purchases should contain one row for every combination of (customer, product) where a customer has ordered the product. The row shouuld contain the customer id, the product id, and quantity ordered.
Using SQL, you can then easily construct a query that produces the set of products ordered by a specific customer. Using a join, this information can be enhanced with product names and prices.

Categories