PHP/MySQL Row Buttons - php

<?php
$query = "SELECT * FROM table1";
$result = mysql_query($query)
or die ("Query Failed: " . mysql_error());
echo "<TABLE BORDER = '1'>";
echo "<TR>";
echo "<TH>Unique ID</TH><TH>URL</TH><TH>First Name</TH><TH>Last Name</TH><TH>Mail</TH><TH>Time Entered</TH>";
echo "</TR>";
while ($row = mysql_fetch_array($result))
{
echo "<TR>";
echo "<TD>",
$row['Unique_ID'], "</TD><TD>",
$row['url'], "</TD><TD>",
$row['fname'], "</TD><TD>",
$row['lname'], "</TD><TD>",
$row['mail'], "</TD><TD>",
$row['time'], "</TD>";
echo "</TR>";
}
echo "</TABLE>";
echo "<br>";
?>
I have the above piece of code, and I would like to automatically insert buttons at the end of each row: edit & delete. Please help?
Also, in the same piece of code-these buttons-I would like to create them as simple MySQL quereid, but then ho will the script know for which row to apply the queries?

echo '<td><form action="/delete.php" method="POST">';
echo '<input type="hidden" name="id" value="'.$row['Unique_ID'].'">';
echo '<input type="submit" value="Delete"></form>';
echo '<form action="/edit.php" method="GET">';
echo '<input type="hidden" name="id" value="'.$row['Unique_ID'].'">';
echo '<input type="submit" value="Edit"></form></td>';
Something like that?
EDIT: cleaned up a bit for clarity

Related

PHP MySQL Get material and quantity for an order form

I need to create an order form that has a checkbox for each product in a table and have a quantity box next to each product.
I need to process the products that have been checked and update an orders table with the checked products and the quantity entered.
I don't know how to link the product checkbox to the quantity field.
$sql="select * from tproducts";
$result=mysqli_query($con, $sql) or die(mysqli_error($con));
echo "<table>";
echo "<tr>";
echo "<th>Product Names</th>";
echo "</tr>";
if(mysqli_num_rows($result)>0)
{
echo "<form action=welcome1.php method=post>";
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo '<td><input type="checkbox" value="' . $row['intProductID'] . '" name="materialcode[]">' . $row['strProductName'] . "</td>";
echo "<td>Enter Quantity </td>";
echo "<td><input type='text' name='qty[]' value='qty' size=5></td>";
echo "</tr>";
}
}
echo "</table>";
echo '<input type="submit" name="submit" value="submit" />';
echo "</form>";
echo "</html>";
welcome1.php
<?php
echo "materialcode<br>";
print_r($_POST['materialcode']);
echo "<br>";
echo "qty<br>";
print_r($_POST['qty']);
?>
PHP will keep the key name you supply in the fields html.
$productIndex = 0;
while ($row = mysqli_fetch_array($result) )
{
echo "<tr>";
echo '<td><input type="checkbox" value="' . $row['intProductID'] . '" name="materialcode[$productIndex]">' . $row['strProductName'] . "</td>";
echo "<td>Enter Quantity </td>";
echo "<td><input type='text' name='qty[$productIndex]' value='qty' size=5></td>";
echo "</tr>";
$productIndex++;
}
This example uses numeric indexes but you could probably use $row['intProductId'] as well.
In your php:
$_POST['materialCode'][0] will be the analog to $_POST['qty'][0] but only the checked checkboxes will be supplied. You might end up with a $_POST['materialCode'] array that has index 0, 5, and 7 if those were the only ones checked.
So use a foreach loop instead of a for loop.
foreach( $_POST['materialCode'] as $key => $value ){
$productId = $value;
$quantity = $_POST['qty'][$key];
}

i need to break the line after a reach of 10 buttons

I need to break the line after a reach of 10 buttons.
Please be specific what are the code to be used for breaking the line.
Here is the code:
<?php
session_start();
require('connection.php');
$result = mysql_query("SELECT * FROM tbCandidates ");
if($result){
}
echo "select Project to Reward for";
$_SESSION['a']="project name-->";
echo "<body><form method='post' action='part1.php' ><input type='hidden' value='abhishek' name='aa'/>";
echo "<table><tr>";
while ($row=mysql_fetch_array($result)){
echo "<td>";
echo $row["project_id"];
echo '<input type="radio" name="n1" value="';echo $row["project_id"]; echo '">'; echo"</input>";
$_SESSION['b']=$row['candidate_name'];
$_SESSION['c']=$row['candidate_position'];
$_SESSION['c']=$row['candidate_nominee'];
echo "</td>";
}
echo "</tr></table><input type='submit'/></form></body>";
If you mean by BREAKING the line is creating another row for your table then try to use the following codes:
<?php
session_start();
require('connection.php');
$result = mysql_query("SELECT * FROM tbCandidates ");
if($result){
}
echo "select Project to Reward for";
$_SESSION['a']="project name-->";
echo "<body><form method='post' action='part1.php' ><input type='hidden' value='abhishek' name='aa'/>";
echo "<table>";
$counter = 0;
while ($row=mysql_fetch_array($result)){
$counter++;
if($counter==1){
echo "<tr>";
}
echo "<td>";
echo $row["project_id"];
echo '<input type="radio" name="n1" value="';echo $row["project_id"]; echo '">'; echo"</input>";
$_SESSION['b']=$row['candidate_name'];
$_SESSION['c']=$row['candidate_position'];
$_SESSION['c']=$row['candidate_nominee'];
echo "</td>";
if($counter==10){
echo "</tr>";
$counter = 0;
}
}
echo "</table><input type='submit'/></form></body>";
Let me know if it works.

Cant seem to EDIT/MODIFY my php table by id

<?php
$con = mysql_connect("localhost","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
if(!isset($_POST['submit'])){
$result = mysql_query("SELECT * FROM pleasework ORDER BY ID");
$row = mysql_fetch_array($result);
}
?>
<form action="?php echo $_SERVER['PHP_SELF'];?>" id="form2" method="post" name="form2">
<img id="close1" src="X.png" width="25" height="25" onclick ="div_hide1()">
<h2><font size="6">Please change existing data</font></h2>
<hr>
<br>
<font color="yellow">Change Name to: </font><input type="text" name="New" value="<?php echo $row['Name'];?>"/><br><br>
<font color="yellow"> Change Cause to: </font> <input type="text" name="New1" value="<?php echo $row['Cause'];?>"/><br><br>
<font color="yellow">Change Symptom to: </font><input type="text" name="New2" value="<?php echo $row['Symptom'];?>"/><br><br>
<font color="yellow"> Change Gene_affected to: </font><input type="text" name="New3"value="<?php echo $row['Gene_affected'];?>" /><br><br>
<input type="hidden" name="id" value="<?php echo $_GET['ID'];?>"/>
<input type="submit" onclick="clicked(event)" />
</form>
<?php
if(isset($_POST['submit'])){
mysql_query("UPDATE pleasework SET Name= '$_POST[New]' WHERE ID='$_POST[id]'");
mysql_query("UPDATE pleasework SET Cause= '$_POST[New1]' WHERE ID='$_POST[id]'");
mysql_query("UPDATE pleasework SET Symptom= '$_POST[New2]' WHERE ID='$_POST[id]'");
mysql_query("UPDATE pleasework SET Gene_affected= '$_POST[New3]' WHERE ID='$_POST[id]'");
echo "Change Successful<br>" ;
header("Location: databse.php");
mysql_close($con);
}
else {}
?>
This is my php file.
while($row = mysql_fetch_array($result))
{
echo "<TR>";
echo "<TD>" . $row['ID'] ."</TD>";
echo "<TD>" . $row['Name'] . " </TD>";
echo "<TD>" . $row['Cause'] . " </TD>";
echo "<TD>" . $row['Symptom']. " </TD>";
echo "<TD>" . $row['Gene_affected'] . " </TD>";
echo "<TD><font color='red'>Delete row</font> </TD>";
echo "<TD><font color='red'>modify</font> </TD>";
echo "</TR>";
}
And this is the section which has a modify button that links to the edit.php file. The error here is that is doesnt bring over the values in the table to the editing page and then submitting the form doesnt work too. help please
Your code appears a bit confused.
First of all, why to put the modify routine after output the form? Especially since after modify you send the header function, that fails if previously there are some output.
Note also a typo: you forgot to properly open the php tag in the form declaration. Change-it in this way:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" id="form2" method="post" name="form2">
The main problem is that you check if the $_POST[submit] if set, but this is not set, due to the absence of attribute name.
Change it in this way:
<input type="submit" name="submit" onclick="clicked(event)" />
Now your script should work (I don't have tested the sql).
Please also note that your UPDATE routine is redundant: you can reduce the 4 statement to only one in this way:
$result = mysql_query
(
"UPDATE pleasework SET Name='{$_POST[New]}', Cause='{$_POST[New1]}', Symptom='{$_POST[New2]}', Gene_affected='{$_POST[New3]}' WHERE ID={$_POST[id]}"
);
About PHP Original MySQL API:
This extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0
NOTE: mysql_* deprecated, so try to use PDO or mysqli_*.
Simple way:
<?php
if(isset($_POST['submit'])){
$result = mysql_query("UPDATE pleasework
SET Name='".$_POST['New']."',
Cause='".$_POST['New1']."',
Symptom='".$_POST['New2']."',
Gene_affected='".$_POST['New3']."'
WHERE ID=".$_POST['id'].");
if($result ){
echo "Change Successful<br>" ;
header("Location: databse.php");
}
mysql_close($con);
}
YOUR PHP:
while($row = mysql_fetch_array($result))
{ $spaces = " ";
echo "<TR>";
echo "<TD>" . $row['ID'] ."</TD>";
echo "<TD>" . $row['Name'] . $spaces."</TD>";
echo "<TD>" . $row['Cause'] . $spaces."</TD>";
echo "<TD>" . $row['Symptom']. $spaces."</TD>";
echo "<TD>" . $row['Gene_affected'] . $spaces."</TD>";
echo "<TD><a href='delete.php?id=".$row['ID'] ."'>";
echo "<font color='red'>Delete row</font></a>".$spaces."</TD>";
echo "<TD><a href='edit.php?id=" . $row['ID'] ."'>";
echo "<font color='red'>modify</font></a>".$spaces."</TD>";
echo "</TR>";
}

how to save selected value from drop down list to mysql in php

<?php
$status=&$_POST['status'];
$from=&$_POST['date_from'];
$to=&$_POST['date_to'];
$conn=mysqli_connect('localhost','root','','punbus') or die("Database not connected".mysqli_error());
if(isset($_POST['sub'])){
$ins="insert into driver_status(driver_name,status,date_from,date_to)
select Driver_name,'$status','$from','$to' from driver_master";
if(mysqli_query($conn,$ins)){
echo "added";
}
else{
echo "NOT".mysqli_error($conn);
}
}
$sel='select Driver_name from driver_master';
$query=mysqli_query($conn,$sel);
echo "<form action='driver_status.php' method='post'>";
echo "<table cellpadding=5>";
echo "<tr>";
echo "<th>Driver Name</th>";
echo "<th>Status</th>";
echo "<th>From</th>";
echo "<th>To</th>";
echo "</tr>";
while($row=mysqli_fetch_assoc($query)){
echo "<tr>";
echo "<td>".$row['Driver_name']."</td>";
$sel1='select d_status from status';
$query1=mysqli_query($conn,$sel1);
?>
<td>
<select name="status">
<?php
while($row1=mysqli_fetch_assoc($query1)){
$st=$row1['d_status'];
echo "<option value='$st'>$st</option>";
}
?>
</select>
</td>
<?php
echo "</tr>";
}
echo "</table>";
echo '<input type="submit" name="sub" value="Update"/>';
echo "</form>";
?>
That is my code. I want to save option selected from 4 drop down list to mysql. When I submit the form, the value selected from last box are getting saved in all rows of mysql table. Now, please tell me what should I do?
I am getting drop down box values from database table properly so what is the problem?
this is your code.
<?php
$status = $_POST['status'];
$driver_name= $_POST['driver_name'];
$from = $_POST['date_from'];
$to = $_POST['date_to'];
$conn = mysqli_connect('localhost', 'root', '', 'punbus') or
die("Database not connected" . mysqli_error());
if(isset($_POST['sub'])) {
foreach($status as $k=>$s){
$ins = "insert into driver_status(driver_name,status,date_from,date_to) VALUES
('".$driver_name[$k]."','$s','$from','$to')";
if (mysqli_query($conn, $ins)) {
echo "added";
} else {
echo "NOT" . mysqli_error($conn);
}
}
}
$sel = 'select Driver_name from driver_master';
$query = mysqli_query($conn, $sel);
echo "<form action='driver_status.php' method='post'>";
echo "<table cellpadding=5>";
echo "<tr>";
echo "<th>Driver Name</th>";
echo "<th>Status</th>";
echo "<th>From</th>";
echo "<th>To</th>";
echo "</tr>";
while($row=mysqli_fetch_assoc($query)){
echo "<tr><td>".$row['Driver_name']
."<input type=\"hidden\" name=\"driver_name[]\" value=\"".$row['Driver_name']."\"/></td>";
$sel1='select d_status from status';
$query1=mysqli_query($conn,$sel1);
echo "<td><select name=\"status[]\">";
while($row1=mysqli_fetch_assoc($query1)){
echo "<option value=\"".$row1['d_status']."\">".$row1['d_status']."</option>";
}
echo "</select></td></tr>";
}
echo "</table>";
echo '<input type="submit" name="sub" value="Update"/>';
echo "</form>";
?>
this is similar to other textbox
$op="select * from client where active=0";
$op1=mysql_query($op);
echo '<select name="c" id="c" style="width:160px;" required>';
while ($row = mysql_fetch_array($op1))
{
echo $s=$row["c_name"];
if($company!=$s)
{
echo '<option value="'.$row["c_name"].'">'.$row["c_name"].'</option>';
}
}
$c=$_REQUEST['c'];
$sql="insert into project c_name) values('$c')";
$sql1=mysql_query($sql);

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

Categories