Getting the value of an array derived from a table - php

I have a list of products the secretary can choose. These products are looked up from the product table and an array was used to display them.
The secretary can select up to any number of the products by checking the checkboxes corresponding to the product. When submitted, the checked products are then displayed in a new page.
Supposedly, the secretary would be able to input the amount of boxes, weight, date of production, and price for each selected product. As I have to insert the information to the table, I'm wondering how I'll be able to do it with an array whose value is not know to me. I've seen examples of inserting arrays into tables, but not arrays that were derived from a table.
EDIT: Added in the code.
$products = $mysqli->query("SELECT ProdName FROM product");
$numofprod = mysqli_num_rows($products);
$i = 0;
while($i < $numofprod) {
while($row = $products->fetch_assoc()){
$prodlist = $row["ProdName"];
echo "<input type='checkbox' class='prodlist' name='check_list[]' id='prodlist";
echo "$i'";
echo " value='";
echo $prodlist;
echo "'>";
echo $prodlist;
echo "</input>";
$i++;
}
}
// upon submitting, it will execute the code below
if(isset($_POST['add_purchase_submit'])){//to run PHP script on submit
if(!empty($_POST['prodlist'])){
echo "<table><tr><th></th><th> Boxes </th> <th> Weight </th> <th> Price </th> <th> Production Date </th> </tr>";
// Loop to store and display values of individual checked checkbox.
foreach($_POST['prodlist'] as $selected){
echo "<tr><td>";
echo $selected;
echo "</td><td>";
echo "<input type='text' name='ppbox' size='10'>";
echo "</td><td>";
echo "<input type='text' name='ppweight' size='10'>";
echo "</td><td>";
echo "<input type='text' name='ppprice' size='10'>";
echo "</td><td>";
echo "<input type='text' name='ppproducion' size='15' placeholder='MM/DD/YY'>";
echo "</td><td></tr>";
}
}
}
I also realized I have an error in the names in the second form...

for($i = 0; i < array.size(); i++){
echo '<input name="array['.$i.']"'>;
}
This gives an amount of inputs abed on you array. Later you can read that as a array in php again:
foreach($_POST['array'] as $entry){
.. do things ..
}
Only a basic scatch, but this shows how you can use arrays as names for inputs. The same way you could use them for checkboxes or other felds you need.

Related

Php form array not passing values to other pages

I am working on a project where I want users to choose their space for parking and book it. I have a MySQL database which holds information about parking slots. I am fetching those values and display it using table and form. I have put checkboxes to make choice. and once they make choice they should be directed to payment page.
I am having problem with checkbox. I can see that in value fields it has values from database but when I hit submit button it doesn't pass any values to next page.
below is my code
<body>
<form method="POST" action="book.php">
<?php
//we create a table
echo "<table>";
// create table th
echo "<tr > <th> Parking Slot No </th> <th> Status </th>";
$sql=" select ParkingSlotNo,Status from fleming_dwing ";
$st=$conn->prepare($sql);
$st->execute();
$total=$st->rowCount();//get the number of rows returned
if($total < 1 ){//if no row was returned
echo "<tr> <td style> No Data: DataBase Empty </td> ";//print out error message
echo "<td> No Data: DataBase Empty </td> ";//print out error message
$ing = "<img src='img/occupied.png'/>" ;
}
else{
while($res = $st->fetchObject()){//loop through the returned rows
echo "<tr>";
if($res->ParkingSlotNo and $res->Status=='OCCUPIED')
{echo "<td> $res->ParkingSlotNo </td> ";
echo "<td> <img src='img/occupied.png'/> </td>";
echo"<td><a href=''> </a> </td>";
}
else
{
echo "<td> $res->ParkingSlotNo </td> ";
echo "<td> <img src='img/vacant.png'> </td>";
echo"<td><input type='checkbox' value='$res->ParkingSlotNo'></td>";
}
echo"</tr>";
}
}
?>
</table>
<input type="submit" value="Submit">
</form>
</body>
</html>
and this is the code for booking page
<?php
require_once("dbconfigpdo.php");
print_r($_POST);
?>
The checkboxes do not have name attributes. A form control can't be successful (included in the name=value pairs of data that are submitted) without one.
Any input must have a name attribute.
By name you can use a value. So, you need to add name="your name" to your checkboxes.
You need to set an attribute name to your input field, or it will not be processed.
Something like:
<input name='parkingslot' type='checkbox' value='$res->ParkingSlotNo'>

How to input data into a database from two different tables with different forms

Hi im doing my final project this year. I need to input data into a database together with month and day. However, month and day is recalled from another database. Some of codes does not work. Btw im using php and mysql.
there are 3 pages.
This is the first page: pilih.php
<html>
<form action="pilih_process.php" method="POST">
<table>
<tr>
<td>Month:</td>
<td><select name="month"/>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
</select>
</td>
</tr>
<tr>
<td>Day:</td>
<td><select name="day"/>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</td>
</tr>
<tr>
<td>Group:</td>
<td><select name="group"/>
<option>1</option>
<option>2</option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" name="submit" value="submit"/></td>
</tr>
</table>
</form>
</html>
This is the second page: pilih_process.php
<html>
<?php
include ('connect_database.php')
$query1 = mysql_query("SELECT Student_ID, Student_Name FROM student_details WHERE Group_ID=$group");
$month = $_POST['month'];
$day = $_POST['day'];
$group = $_POST['group'];
echo "<table>";
echo "<tr>";
echo "<td>Month: $month</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Day: $day</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Group: $group</td>";
echo "</tr>";
echo "</table>";
echo "<form action='pilih_process2.php' method='POST'>";
echo "<table>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "</tr>";
while ($row = mysql_fetch_array($query1) )
{
echo "<tr>";
echo '<td>' . $row['Student_ID'] . '</td>';
echo '<td>' . $row['Student_Name']. '</td>';
echo "<td><input type='checkbox' name='status[]' value='Expert'/></td>";
echo "<td><input type='checkbox' name='status[]' value='Intermediate'/></td>";
echo "<td><input type='checkbox' name='status[]' value='Amature'/></td>";
echo "<td><input type='checkbox' name='status[]' value='Noob'/></td>";
echo "</tr>";
}
echo "<tr>";
echo "<td><input type='submit' name='submit' value='Submit'/></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
?>
</html>
This is the third page: pilih_process2.php
<html>
<?php
include ('connect_database.php');
$month = $_SESSION['month']; //From First page: pilih.php
$day = $_SESSION["day"]; //From First page: pilih.php
$student_id = $_SESSION["Student_ID"]; //From Second page: pilih_process.php
$student_name = $_SESSION["Student_Name"]; //From Second page: pilih_process.php
$group = $_POST['group']; //From First page: pilih.php
$status = $_POST['status']; //From Second page: pilih_process.php
mysql_query("INSERT into mencuba(Month_ID, Day_ID, Student_ID, Student_Name, Group_ID, Status) VALUES('$month', '$day', '$student_id', '$student_name', '$group', '$status')");
/*
$status_test = $_GET['status']; //I DONT KNOW WHETHER THIS ARRAY METHOD IS CORRECT
for($i=0; $i < count($checked); $i++){
echo "Selected " . $checked[$i] . "<br/>";
}
*/
?>
Here how it goes:
From the First page, the user have to choose which month, day and group and submit.
On the Second page, it displays the Student ID, Student Name, Group and Status which is based on what group.
For the status, the user has to pick one and submit.
On the third page. The data Month_ID, Day_ID, Student_ID, Student_Name, Group_ID and Status will be inserted into the database.
PROBLEMS:
When I insert the data, only Month_ID, Day_ID, Group_ID and Status is inserted into the database. I was unable to call the variable Student_ID and variable Student_Name.
it only insert one data. if there are 100 data to enter obviously ihave to use array which i do not really sure how to use.
We are taught simple PHP and MySql
As noted in other answers, there are a few things going on here.
You are trying to use the variable $group before you've set it
You are using raw POST data in a query - this is very very bad practice - even if it's a project you'll score better for doing it better. At the very least wrap that in mysql_real_escape_string so it looks like you tried.
$group = mysql_real_escape_string($_POST['group']);
As for the main issue:
You need to pass the first forms parameter to the second form. Just putting them in the table will not save them. Pass them as hidden form values
echo "<input type="hidden" name="group" value="'.$group.'" />";
Beyond that, if you expect to have multiple results for your query (ie: multiple people from one group) then you need to change your logic again because each one will just be overriding the form values from the one before.
You place $group in your query before you initiate it. you must initiate it first and then place it to query.
like this :
$month = $_POST['month'];
$day = $_POST['day'];
$group = $_POST['group'];
$query1 = mysql_query("SELECT
Student_ID, Student_Name
FROM student_details
WHERE Group_ID=$group");
About status thing
you have to use select instead of checkbox , if you want to give multiple select use another name property for each field in your form.

How to create submit form in a row and relate input to that row?

Hey Guys, I have a question for you.
Imagine that I wanted to be able to keep track of how many miles I've ran every week, so that I could
compare it to the goals I've set for each week. So i've created this table by the use of mysql_fetch_row.
$result=mysql_query("SELECT * FROM randomtable ORDER BY week ASC");
echo "<Table id='result' cellspacing='0'>
<tr class='toprow'>
<th>Week</th>
<th>Goal</th>
<th>Actual Number of Miles</th>
</tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td><form><input method='post' type='number'></form></td>";
echo "</tr>";
}
echo "</table>";
This piece of code resultet in a table with 10 weeks with 10 goals - and a column for the actual number of miles. This column should include 10 input forms where the actual number of miles can be submitted. But how do I relate the input from the submit form to the row in which the submit form is positioned?
The primary key is the week - so this would be the one to relate to.
Hope you understand what my problem is:)
To do this you would use a hidden input field.
When you echo each row, and the form in that row, you would simply add an extra line:
`<input type="hidden" name="row_id" value="' . $row['id_column'] . '" />';
In full, your code would be:
$result=mysql_query("SELECT * FROM randomtable ORDER BY week ASC");
echo "<Table id='result' cellspacing='0'>
<tr class='toprow'>
<th>Week</th>
<th>Goal</th>
<th>Actual Number of Miles</th>
</tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td>
<form>
<input method='post' type='number'>
<input type='hidden' name='row_id' value='" . $row['id_column'] . "' />
</form>
</td>";
echo "</tr>";
}
echo "</table>";
I think there should be some modifications that has to be done in loop.
echo "<td><form method='post'><input type='number' value='".$rows['col_name']."'><input type='submit' ></form></td>";
This code adds a submit button to each row. But, this shouldn't be what I think.
It should be rather this way,
echo "<form method='post'> ";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td><input type='number' value='".$rows['col_name']."'></td>";
echo "</tr>";
}
echo "<input type='submit' ></form>";
Or make the input field look like this.
'<input type="text" name="row['.$row['id_column'].'][miles]" />';
It will return you an array when you post it.
foreach($_POST['row'] as $key => $value){
// $key is your primary key
// $value['miles'] is your input value
}

How to access all the <form> elements that have been dynamically generated by PHP-MySQL in $_GET upon submitting the form?

I have a simple DB and a small table in it. This table named STOCK has only one column - names of different items in a stationery.
I am dynamically generating a form in PHP-MySQL which fetches Item from STOCK (such as Pencil, Pen etc.) and displays them by using echo() function. So I have generated a form which displays Item and correspondingly an <input> text box. Upon submitting this form, how can I access all the Names and their text inputs by traversing through the $_GET array?
The dynamically generated form is:
$res = mysql_query("SELECT * FROM Stock");
echo "<form name='input' action='add_stock_later.php' method='get'>";
echo "<table align='left' border='1'>
<tr>
<th>Item</th>
<th>Quantity</th>
<th>Remarks</>
</tr>";
while($row = mysql_fetch_array($res))
{
$item = $row['Item'];
echo "<tr>";
echo "<td>" . $item . "</td>";
echo "<td><input type='text' name='$item'></td>";
echo "<td><input type='text' name='Remarks'></td>";
echo "</tr>";
}
echo "<tr><td></td>
<td align='middle'><input type='submit' value='Submit'></td></tr>";
echo "</table>";
Now in the add_stock_later.php, how can I access all these $item and it's corresponding input without having to manually do it using $_GET['Pencil'], $_GET['Pen'] and so on.
First edit your form.
echo "<td><input type='text' name='".$item."[quantity]'></td>";
echo "<td><input type='text' name='".$item."[remarks]'></td>";
Then use foreach().
foreach ($_GET as $stationery => $info) {
$stationery; // name of the stationery
$info["quantity"]; // quantity of the stationery
$info["remarks"]; // remarks for the stationery
}
Something like so, add to it of course.
$required = array('pencil', 'pen', 'paper', 'staples');
foreach ($required as $getKey){
if (isset($_GET[$getKey]) && $_GET[$getKey] !=''){
$$getKey = trim($_GET[$getKey]);
}
}
This turns $_GET['pencil'] into $pencil and you don't need to worry about knuckleheads trying to inject garbage.

PHP Checkbox Values

I'm trying to create a checkout page in PHP. The page that lists the items is called shop.php. On this page, I display a number items to be sold with a checkbox beside each item. I'm pulling the item name and price information from the database. Here's my main code.
$row = mysql_fetch_array($query);
while($row) {
echo "<TR>";
echo "<TD><B>$row[Name] </B></TD>";
echo "<TD><B>"."$"."$row[Price] </B></TD>";
echo "<TD><input type=checkbox name=foods[] value='$row[Price]' /></TD>";
echo "</TR>";
$row = mysql_fetch_array($query);
}
<input type="submit" name="buy" value="Buy Now" />
So I am setting the name of the checkboxes to foods[] which'll be an array that'll contain values of all checkboxes submitted. When the user clicks on the "Buy Now" button, they'll be redicted to the cart.php page, that'll display the name and price of the item they've purchased. I know that I can output the prices by looping through the $items array. The problem I have is that I don't understand how would I display the item names. I thoght of doing the following:
$row = mysql_fetch_array($query);
while($row) {
echo "<TR>";
echo "<TD><B>$row[Name] </B></TD>";
echo "<TD><B>"."$"."$row[Price] </B></TD>";
echo "<TD><input type=checkbox name='$row[Name]' value='$row[Price]' /></TD>";
echo "</TR>";
$row = mysql_fetch_array($query);
}
This time, both the name and the price of the items are included in the checkbox as its name and value. But again, the problem is how would I display them on the cart.php page without hard coding. What I means is that how would I display the item name and its price without doing something like this:
(Assume that one of the items name is Burger which was populated as the name of the checkbox.)
if (isset($_POST['Burger'])){
echo "<td>$_POST['Burger']</td>";
}
I'd appreciate any suggestions/tips on this question. Thank you.
If you name the checkboxes like:
<input type='checkbox' name='foods[{$row['Name']}]' value='{$row['Price']}' />
You can access the array $_POST['foods'] and the key/value will be Name/Price. So you can do:
foreach($_POST['foods'] as $name => $price) {
echo "<tr><td>$name</td><td>$price</td></tr>";
}
Set name to each checkbox as foods[$row[name]]. You should have following code:
$row = mysql_fetch_array($query);
while($row) {
echo "<TR>";
echo "<TD><B>$row[Name] </B></TD>";
echo "<TD><B>"."$"."$row[Price] </B></TD>";
echo "<TD><input type=checkbox name='foods[{$row[Name]}]' value='$row[Price]' /></TD>";
echo "</TR>";
$row = mysql_fetch_array($query);
}
<input type="submit" name="buy" value="Buy Now" />
On POST you will have associated array of item names and prices.

Categories