PHP Basket quantity variable - php

I am trying to create a php page where the materials from database are populated. Users should be able to enter the quantity next to the item they wish to order and I have created a qty text field for this
<?php
session_start();
include("db.php");
$pagename="Order Material";
echo "<html>";
echo "<title>".$pagename."</title>";
echo "<h2>".$pagename."</h2>";
include ("detectlogin.php");
echo "<link rel=stylesheet type=text/css href=mystylesheet.css>";
$sql="select * from material";
$result=mysqli_query($con, $sql) or die(mysqli_error($con));
echo "<table border=1>";
echo "<tr>";
echo "<th>Material Name</th>";
echo "<th>Material Description</th>";
echo "<th>Toxicity Level</th>";
echo "</tr>";
while ($arraymaterials=mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>".$arraymaterials['materialName']."</td>";
echo "<td>".$arraymaterials['materialDescrip']."</td>";
echo "<td>".$arraymaterials['materialToxicity']."</td>";
echo "<td>Enter Quantity</td>";
echo "<td><input type=text name=qty value=qty size=5></td>";
echo "<form action=request_material.php method=post>";
echo "<input type=hidden name=materialcode value=".$arraymaterials['materialCode'].">";
echo "<td><input type=submit value='Request'></td>";
echo "</form>";
echo "</tr>";
}
echo "</table>";
?>
However, I cannot successfully post the value of qty on to the next page even though I have $qty=$_POST['qty']; on my request_material.php. Do you know why this value entered in the qty field cannot be posted onto the request_material.php page? Do I need a session variable?
thanks

Because input tag name="qty" is outside the form tag
echo "<td><input type=text name=qty value=qty size=5></td>";// outside form tag
echo "<form action=request_material.php method=post>";
echo "<input type=hidden name=materialcode value=" . $arraymaterials['materialCode'] . ">";
echo "<td><input type=submit value='Request'></td>";
echo "</form>";
You need to add it inside your form tag as
echo "<form action=request_material.php method=post>";
echo "<td><input type=text name=qty value=qty size=5></td>";// add inside it
echo "<input type=hidden name=materialcode value=".$arraymaterials['materialCode'].">";
echo "<td><input type=submit value='Request'></td>";
echo "</form>";

Please try this: I have updated the code:
echo "<table border=1>";
echo "<tr>";
echo "<th>Material Name</th>";
echo "<th>Material Description</th>";
echo "<th>Toxicity Level</th>";
echo "</tr>";
if(mysqli_num_rows($result)>0)
{
echo "<form action=request_material.php method=post>";
while ($arraymaterials=mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>".$arraymaterials['materialName']."</td>";
echo "<td>".$arraymaterials['materialDescrip']."</td>";
echo "<td>".$arraymaterials['materialToxicity']."</td>";
echo "<td>Enter Quantity</td>";
echo "<td><input type='text' name='qty[]' value='qty' size=5></td>";
echo "<input type=hidden name='materialcode[]' value=".$arraymaterials['materialCode'].">";
echo "<td><input type=submit value='Request'></td>";
echo "</tr>";
}
echo "</form>";
}
echo "</table>";
In request_material.php check value of $qty.It will be an array.for more details print_r($_POST) in request_material.php

Related

Save button for each row in the table. PHP MYSQL

Please help, each row in the table has save button for editing/adding content. I have 3 textboxes in each row,PersoninCharge,PIC_Comments and Status. The user can add/edit these textboxes whenever they click the save button on that particular row. The problem is, whenever I add/edit data in one row, the save button can't read what particular row I edited.
The save button is perfectly running if I searched the invoice number first, but what I want is to directly edit/add the data in each row without searching it first.
Here's the code:
For the table:
<?php
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
echo"<tr class=output2>";
echo "<td>$row[1]</td>";
echo "<td>$row[2]</td>";
echo "<td>$row[3]</td>";
echo "<td>$row[4]</td>";
echo "<td>$row[5]</td>";
echo "<td>$row[6]</td>";
echo "<td>$row[7]</td>";
echo "<td>$row[8]</td>";
echo "<td>$row[9]</td>";
echo "<td>$row[10]</td>";
echo "<td>$row[11]</td>";
echo "<td>$row[12]</td>";
echo "<td><input type='text' name='pic' value='$row[17]'></td>";
echo "<td><input type='text' name='comt' value='$row[18]'></td>";
echo "<td><input type='text' name='stat' value='$row[19]'></td>";
echo "<td><form name='update' method='POST'><input type='submit' name='save_btn' value='SAVE' style='font-size:1em;'/></form></td>";
echo "<td><input type='hidden' name='idtxt' value='$row[0]'/></td>";
echo "</tr>";
}
}
else
{
echo '<h3>No result found! </h3><br>';
}
$con->close();
For Save button:
if(isset($_POST['save_btn']))
{
$query2="UPDATE invalid_invoice SET UpdateBy='".$_SESSION['login_user']."', UpdateDateTime=NOW(), PersoninCharge='".$_POST['pic']."', PIC_Comments='".$_POST['comt']."', Status='".$_POST['stat']."' WHERE ID='".$_POST['idtxt']."'";
$con->query($query2);
$con->close();
echo '<h3 class="datasuccess">Data successfully added!</h3>';
}
I have edited your form. please have a look
<?php
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
echo"<form name='update' method='POST'><tr class=output2>";
echo "<td>$row[1]</td>";
echo "<td>$row[2]</td>";
echo "<td>$row[3]</td>";
echo "<td>$row[4]</td>";
echo "<td>$row[5]</td>";
echo "<td>$row[6]</td>";
echo "<td>$row[7]</td>";
echo "<td>$row[8]</td>";
echo "<td>$row[9]</td>";
echo "<td>$row[10]</td>";
echo "<td>$row[11]</td>";
echo "<td>$row[12]</td>";
echo "<td><input type='text' name='pic' value='$row[17]'></td>";
echo "<td><input type='text' name='comt' value='$row[18]'></td>";
echo "<td><input type='text' name='stat' value='$row[19]'></td>";
echo "<td><input type='submit' name='save_btn' value='SAVE' style='font-size:1em;'/></td>";
echo "<td><input type='hidden' name='idtxt' value='$row[0]'/></td>";
echo "</tr></form>";
}
}
else
{
echo '<h3>No result found! </h3><br>';
}
$con->close();

track change in an array then submit the array to the same page

I have the following php code (I omitted the irrelevant part of code):
<?php
echo "<form action=$_SERVER[PHP_SELF] method=\"POST\">";
echo "<table>";
for ($i=0;$i<$num_rows;$i+=1) {
echo "<tr>";
echo "<td align=center>$variable1[$i]</td>";
echo "<td align=center>$variable2[$i]</td>";
echo "<td align=center><input type=\"text\" name=\"variable3[$i]\" value=\"$variable3[$i]\" /></td></tr>";
echo "<input type=\"hidden\" name=\"variable1[$i]\" value=\"$variable1[$i]\">";
echo "<input type=\"hidden\" name=\"variable2[$i]\" value=\"$variable1[$i]\">";
echo "<input type=\"hidden\" name=\"variable3[$i]\" value=\"$variable1[$i]\">";
}
echo "<tr><td colspan=\"3\"><input name=\"action\" type=\"submit\" value=\"Update\"></td></tr>";
echo "</table>";
echo "</form>";
?>
I want to pass the variables through POST method to the same page. The code retrieves data from the database and lists it in a table. However, I want to be able to manually modify variable3 and submit the array after I do this. The problem is that the array is submitted before I modify the variable3. How can I submit the array with the value of variable3 modified? How can I check if variable3 was modified and do the submitting afterwards? Thanks.
delete line 11 like this
<?php
echo "<form action=$_SERVER[PHP_SELF] method=\"POST\">";
echo "<table>";
for ($i=0;$i<$num_rows;$i+=1) {
echo "<tr>";
echo "<td align=center>$variable1[$i]</td>";
echo "<td align=center>$variable2[$i]</td>";
echo "<td align=center><input type=\"text\" name=\"variable3[$i]\" value=\"$variable3[$i]\" /></td></tr>";
echo "<input type=\"hidden\" name=\"variable1[$i]\" value=\"$variable1[$i]\">";
echo "<input type=\"hidden\" name=\"variable2[$i]\" value=\"$variable1[$i]\">";
}
echo "<tr><td colspan=\"3\"><input name=\"action\" type=\"submit\" value=\"Update\"></td></tr>";
echo "</table>";
echo "</form>";
?>
Now you can see if it has been changed by cheking the value of $_POST['variable3'].

Delete MySQL entry from HTML page

I'm trying to design a control page for my website that allows admins to delete requests to use a 3D printer at my school. The page displays a query of the schedule from the MySQL database and puts a delete button next to every entry. However, I would like to link the information in each entry to its respective delete button and have no idea how to do that. You can see the page itself here: http://kepler.covenant.edu/~techclub/PHP/manage%20printer%20schedule.php and this is my code for displaying the information
if ($result = $mysqli->query($query)) {
// see if any rows were returned
if ($result->num_rows > 0) {
// yes
// print them one after another
echo "<table cellpadding=10 border=1>";
echo "<tr>";
echo "<th>Control</th>";
echo "<th>Student ID</th>";
echo "<th>Last Name</th>";
echo "<th>First Name</th>";
echo "<th>Date and Time</th>";
echo "<th>Description</th>";
echo "</tr>";
$key = 1;
while($row = $result->fetch_array()) {
echo '<form method="POST" name="deleterequest" action = "delete request.php">';
echo "<tr>";
echo "<td><input name='".$row[5]."'type='submit' value='Delete' ></td>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
}
else {
// no
// print status message
echo "No upcoming prints found!";
}
// free result set memory
$result->close();
}
I can't figure out how to link the information in a row with the delete button that I put in the row to send to the delete request.php program (which I have no code for yet)
you should put hidden field that hold each record id.
As below
while($row = $result->fetch_array()) {
echo "<tr>";
echo "<td>";
echo '<form method="POST" name="deleterequest" action = "delete request.php">';
echo "<input name='recored_id' type='hidden' value='".$row['id']."' >";
echo "<input name='delete'type='submit' value='Delete' >";
echo "</form>";
echo "</td>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "</tr>";
echo "</form>";
}
Now from your request.php page you should do something like this
$recored_id = (int) $_POST['recored_id'];
$sql = "DELETE FROM table_name WHERE recored_id='$recored_id'";
Don't forget to give this code some security validation
all the best,
You're going to need some hidden inputs, like so:
while($row = $result->fetch_array()) {
echo "<tr>";
echo '<td><form method="POST" name="deleterequest" action = "deleterequest.php">';
echo "<input type='hidden' name='val0' value='" . $row[0] . "'>";
echo "<input type='hidden' name='val1' value='" . $row[1] . "'>";
echo "<input type='hidden' name='val2' value='" . $row[2] . "'>";
echo "<input type='hidden' name='val3' value='" . $row[3] . "'>";
echo "<input type='hidden' name='val4' value='" . $row[4] . "'>";
echo "<input name='".$row[5]."'type='submit' value='Delete' >";
echo "</form></td>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "</tr>";
}
You should change the name of these inputs though

PHP - How do you select a specific index of a row given by mysqli_fetch_array()?

Please refer to the image below:
http://i.stack.imgur.com/6hBPC.png
For instance, if a user clicks the button on the row which says "You have a quiz for math", the "Quiz ID" value of THAT row would then be passed to another PHP file.
Here's my current code:
<?php
$con=mysqli_connect("127.0.0.1", "root", "", "quizmaker");
if (mysqli_connect_errno($con))
{
echo "MySqli Error: " . mysqli_connect_error();
}
$now=date("m/d/Y");
$sql=mysqli_query($con,"SELECT * FROM quiz_query WHERE quiz_date='$now'");
$count=mysqli_num_rows($sql);
if($count>=1)
{
echo "<table border='1' width='50%'>";
echo "<form action='answer_quiz.php' method='post'>";
echo "<tr>
<td>You have a pending quiz!</td><td> </td><td> </td>
</tr>";
$number=1;
while($result=mysqli_fetch_array($sql))
{
echo "<tr>";
echo "<td>You have a quiz for " . $result['subject'] . "</td>";
echo "<td>Quiz ID: " .$result['quiz_ID']. "</td>";
echo "<td><input type='submit' name='button' id='button' value='Take Quiz'>";
echo "<input type='hidden' name='quiz[$number]' value='$result[quiz_ID]'>";
echo "</td>";
echo "</tr>";
$number++;
}
echo "</form>";
echo "</table>";
}
else
{
"You have no quiz! :D";
}
mysqli_close($con);
?>
Move this line:
echo "<form action='answer_quiz.php' method='post'>";
Inside of the while loop.
Also, change
echo "<input type='hidden' name='quiz[$number]' value='$result[quiz_ID]'>"
with
echo "<input type='hidden' name='quizId' value='$result[quiz_ID]'>"
Now, in answer_quiz.php you'll receive $_POST['quizId'] with the value you need.
Change your while to :
while( $row = $result->fetch_array(MYSQLI_ASSOC)){
echo $row['subject'];
}
You are forgetting quotes around your variable:
Instead of
echo "<input type='hidden' name='quiz[$number]' value='$result[quiz_ID]'>";
It should be
echo "<input type='hidden' name='quiz[$number]' value='$result[\"quiz_ID\"]'>";

Why doesn't this $_GET work

I am just trying to get the values from a table, but for some reason GET isn't working for me, or I am doing something wrong. Here is how I create my table in one php file:
<?php
.
.
.
.
echo "<tr>";
echo "<td>Login ID</td>";
$j=1;
echo "<td><input name=$j type='text' value='$line[$j]' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Name</td>";
$j=2;
echo "<td><input name=$j type='text' value='$line[$j]' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Password</td>";
$j=3;
echo "<td><input name=$j type='text' value='$line[$j]' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Birthday</td>";
$j=4;
echo "<td><input name=$j type='text' value='$line[$j]' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Address</td>";
$j=5;
echo "<td><input name=$j type='text' value='$line[$j]' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Email</td>";
$j=6;
echo "<td><input name=$j type='text' value='$line[$j]' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Phone Number</td>";
$j=7;
echo "<td><input name=$j type='text' value='$line[$j]' size=20/></td>";
echo "</tr>";
?>
The names should be the number 1-7 right?
In another php file I attempt to access the values in those fields with he following code:
<?php
.
.
.
.
$login_id = $_GET['1'];
$name = $_GET['2'];
$pw = $_GET['3'];
$bday = $_GET['4'];
$address = $_GET['5'];
$email = $_GET['6'];
$phno = $_GET['7'];
echo "new: $login_id, $name, $pw, $bday, $address, $email, $phno";
?>
Here is what I end up getting back: new: , , , , , ,
So what is it that I am doing wrong? I can't seem to find anything wrong with my code. I know I should probably be using $_POST for the password.
<?php
echo "<form action='otherfile.php' method='get'><tr>";
echo "<td>Login ID</td>";
$j=1;
echo "<td><input name=$j type='text' value='$j' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Name</td>";
$j=2;
echo "<td><input name=$j type='text' value='$j' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Password</td>";
$j=3;
echo "<td><input name=$j type='text' value='$j' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Birthday</td>";
$j=4;
echo "<td><input name=$j type='text' value='$j' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Address</td>";
$j=5;
echo "<td><input name=$j type='text' value='$j' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Email</td>";
$j=6;
echo "<td><input name=$j type='text' value='$j' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Phone Number</td>";
$j=7;
echo "<td><input name=$j type='text' value='$j' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Submit Data</td>";
echo "<td><input name='submitdata' type='submit' value='submit' size=20/></td>";
echo "</tr><form>";
?>
if you want submit then get the all value in otherfile.php file
try its working...
You have not put the <form> tag. Add tag in begining
<form action='another_page.php' method='get'>
and in the end write
<input type='submit' name='submit>
</form>
What it will do on submit it will forward the values to the another page.
<form name="login" method="get" action="youraction.php">
echo "<td><input name=$j type='text' value=$j /></td>";
</form>
youraction.php
---------------
<?php
if($_GET)
{
print_r($_GET);
}
?>

Categories