Multiple update buttons on single form - php

Please forgive me for the terribly structured code. I'm attempting to update different customers on each line with an individual submit button. The delete function works great. I'm trying to pass the ID of the row when the submit button is clicked for each row. However, I'm getting the ID for the last row, no matter which row I click. I've attached a screenshot as an example. Any help is appreciated!
<form action="" method="post">
<table class="table table-striped table-bordered table-responsive">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
<th>Product</th>
<th>Firmware Version</th>
<th>Purchase Date</th>
<th>Delete</th>
</tr>
</thead>
<?php
$pdo = new PDO("mysql:host=localhost;dbname=project", $username, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
$query = $pdo->prepare("select * from customers");
$query->execute();
while($customers = $query->fetch()){
$ID = $customers['ID'];
echo '<tr><td><input type="text" name="name" value="';
echo $customers['name'];
echo '"></td>';
echo "<td>" . $customers['email'] . "</td>";
echo "<td>" . $customers['phone'] . "</td>";
echo "<td>" . $customers['address'] . "</td>";
echo "<td>" . $customers['product'] . "</td>";
echo "<td>" . $customers['firmware'] . "</td>";
echo "<td>" . $customers['purchase_date'] . "</td>";
echo '<td align="center"><input type="hidden" name="id" value="';
echo $ID;
echo '"><input type="submit" name="delete" value="X"> </td></tr>';
echo '<tr><td colspan="8"><input type="hidden" name="id_update" value="';
echo $ID;
echo '"><input type="submit" name="update" value="Update">';
echo $ID . '<--This is the ID for each row';
echo '</td></tr>';
}
// Delete customer
if(isset($_POST['delete'])) {
try{
$ID = $_POST['id'];
$query = $pdo->prepare("delete from customers where ID = :ID");
$query->bindParam(':ID', $ID);
$query->execute(array(
':ID' => $ID
));
echo "Customer successfully deleted.";
echo '<META http-equiv="refresh" content="1;URL=view_edit.php">';
}catch(PDOException $e){
echo "Failed to delete the MySQL database table ... :".$e->getMessage();
} //end of try
} //end of isset delete
// Edit customer
if(isset($_POST['update'])) {
echo "Update " . $_POST['id_update'] . '<-- This is the result of clicking update for each row';
} //end of isset update
?>
</table>
</form>

Have you tried doing a form per row as opposed to one form that wraps all the rows (I know you are asking about doing it in 1 form, so you may be just wanting to do it that way only)? It may or may not be what you are looking for. I do this when I need multi-rows with per-row updates.
<?php
function UserForm($customers = array())
{
ob_start(); ?>
<form action="" method="post"><?php
$ID = $customers['ID']; ?>
<tr>
<td><input type="text" name="name" value="<?php echo $customers['name']; ?>"></td>
<td><?php echo $customers['email']; ?></td>
<td><?php echo $customers['phone']; ?></td>
<td><?php echo $customers['address']; ?></td>
<td><?PHP echo $customers['product']; ?></td>
<td><?php echo $customers['firmware']; ?></td>
<td><?php echo $customers['purchase_date']; ?></td>
<td align="center">
<input type="hidden" name="id" value="<?php echo $ID; ?>">
<input type="submit" name="delete" value="X">
</td>
</tr>
<tr>
<td colspan="8">
<input type="hidden" name="id_update" value="<?php echo $ID; ?>" />
<input type="submit" name="update" value="Update" />
<?php echo $ID; ?><--This is the ID for each row -->
</td>
</tr>
</form>
<?php
$data = ob_get_contents();
ob_end_clean();
return $data;
} ?>
<table class="table table-striped table-bordered table-responsive">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
<th>Product</th>
<th>Firmware Version</th>
<th>Purchase Date</th>
<th>Delete</th>
</tr>
</thead>
<?php
$pdo = new PDO("mysql:host=localhost;dbname=project", $username, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
$query = $pdo->prepare("select * from customers");
$query->execute();
while($customers = $query->fetch()){
echo UserForm($customers);
}
// Delete customer
if(isset($_POST['delete'])) {
try{
$ID = $_POST['id'];
$query = $pdo->prepare("delete from customers where ID = :ID");
$query->bindParam(':ID', $ID);
$query->execute(array(':ID' => $ID));
echo "Customer successfully deleted.";
echo '<META http-equiv="refresh" content="1;URL=view_edit.php">';
}catch(PDOException $e){
echo "Failed to delete the MySQL database table ... :".$e->getMessage();
} //end of try
} //end of isset delete
// Edit customer
if(isset($_POST['update'])) {
echo "Update " . $_POST['id_update'] . '<-- This is the result of clicking update for each row';
} //end of isset update
?>
</table>

Related

how to display data from database in html text boxes using php

i am displaying my table data in html page but i am getting blank page.....
i am new to php coding please help me.....
this is my html code
<!DOCTYPE html>
<html>
<head>
<script>
function validate(){
var phonenumber=document.myform.phonenumber.value;
if(phonenumber.length>=10){
alert("Phonenumber must be at least 10 characters");
return false;
}
}
</script>
</head>
<body font-color="red">
<form name ="myform" action="dis.php" method="post" submit="return validate()">
<table border='0' width='480px' cellpadding='0' cellspacing='0' align='center'>
<center><tr>
<td><h1><marquee> Employee Details</marquee></h1></td>
</tr><center>
<table border='0' width='480px' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td align='center'>Name:</td>
<td><input type='text' name='name' required></td>
</tr>
<tr> <td> </td> </tr>
<tr>
<td align='center'>id:</td>
<td><input type='text' name='id'></td>
</tr>
<tr> <td> </td> </tr>
<tr>
<td align='center'>roll number:</td>
<td><input type='text' name='rollnumber'></td>
</tr>
<tr> <td> </td> </tr>
<tr>
<td align='center'>Address:</td>
<td><input type='text' name='address'></td>
</tr>
<tr> <td> </td> </tr>
<tr>
<td align='center'>Phonenumber:</td>
<td><input type='text' name='phonenumber'></td>
</tr>
<tr> <td> </td> </tr>
<table border='0' cellpadding='0' cellspacing='0' width='480px' align='center'>
<tr>
<td align='center'><input type='submit' name='insert' value="insert"></td>
</tr>
<tr>
<td align='center'><input type='submit' name='update' value="update"></td>
</tr>
</table>
</table>
</table>
</form>
</body>
</html>
this is my php code
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
$connection = mysql_connect('localhost', 'root','');
if (!$connection)
{
die("Database Connection Failed" . mysql_error());
}
$select_db = mysql_select_db( "emp",$connection);
if (!$select_db)
{
die("Database Selection Failed" . mysql_error());
}
else
{
session_start();
error_reporting(0);
$name = $_POST['name'];
$id = $_POST['id'];
$rollnumber = $_POST['rollnumber'];
$address = $_POST['address'];
$phonenumber = $_POST['phonenumber'];
if(isset($_POST['insert']))
{
$sql = mysql_query("SELECT * FROM venu ");
$result = mysql_query($sql) or die(mysql_error());
$n=mysql_num_rows($result);
if($n > 0)
{
echo "<table>";
echo "<th>name</th>";
echo "<th>id</th>";
echo "<th>rollnumber</th>";
echo "<th>address</th>";
echo "<th>phonenumber</th>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['rollnumber'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['phonenumber'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
else
{
echo "No records matching your query were found.";
}
}
else
{
echo "ERROR: Could not able to execute.";
}
}
mysql_close($connection);
?>
</body>
</html>
i am getting error
only blank page displaying...
please help me....
You are using mysql_query() twice. Change this:
$sql = mysql_query("SELECT * FROM venu ");
$result = mysql_query($sql) or die(mysql_error());
Into:
$sql = "SELECT * FROM venu ";
$result = mysql_query($sql) or die(mysql_error());

Return only searched rows in table and hide other

If I write something in search box and press search , it should only return matched rows and hide other rows.
Here is my code, it works perfects only issue is it gives me searched record + all record list of table.
What can I do to show only searched data in table.?
<div id="pageContent"><br />
<div class="search" align="right">
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
</div>
<div class="container">
<table id="employee-grid" width="auto" cellpadding="1" cellspacing="1" border="1" class="table table-hover">
<?php
include_once '../storescripts/connect_to_mysql.php';
$num_rec_per_page=5;
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else
{
$page=1;
}
$start_from = ($page-1) * $num_rec_per_page;
$result= mysql_query("SELECT * FROM products LIMIT $start_from, $num_rec_per_page");
?>
<thead>
<tr class="success">
<th>Id</th>
<th>Product Name</th>
<th>Price</th>
<th>Status</th>
<th>Quantity</th>
<th>Details</th>
<th>Category</th>
<th>Subcategory</th>
<th>Date Added</th>
<th colspan="2">Functions</th>
</tr>
</thead>
<?php
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM products WHERE product_name LIKE '%".$term."%' or price LIKE '%".$term."' or details LIKE '%".$term."'";
$r_query = mysql_query($sql);
if($r_query>1)
{
while ($row = mysql_fetch_array($r_query)){
echo "<tr bgcolor='red'>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['product_name']."</td>";
echo "<td>".$row['price']."</td>";
echo "<td>".$row['status']."</td>";
echo "<td>".$row['quantity']."</td>";
echo "<td>".$row['details']."</td>";
echo "<td>".$row['category']."</td>";
echo "<td>".$row['subcategory']."</td>";
echo "<td>".$row['date_added']."</td>";
echo "<td><a href='product_listing_edit.php?id=".$row['id']."'>Edit</a></td>";
echo "<td><a name='delete' href='product_listing_delete.php?id=".$row['id']."'>Delete</a></td><tr>";
echo "</tr>";
}
}
else{
echo "Nothing should be displayed";
}
}
?>
<?php
while($row=mysql_fetch_array($result))
{
echo "<tr class='danger'>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['product_name']."</td>";
echo "<td>".$row['price']."</td>";
echo "<td>".$row['status']."</td>";
echo "<td>".$row['quantity']."</td>";
echo "<td>".$row['details']."</td>";
echo "<td>".$row['category']."</td>";
echo "<td>".$row['subcategory']."</td>";
echo "<td>".$row['date_added']."</td>";
echo "<td><a href='product_listing_edit.php?id=".$row['id']."'>Edit</a></td>";
echo "<td><a name='delete' href='product_listing_delete.php?id=".$row['id']."'>Delete</a></td><tr>";
echo "</tr>";
}
?>
</table>
Just keep single while loop and run the query and search query as shown below it will solve the issue:
<div id="pageContent"><br />
<div class="search" align="right">
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
</div>
<div class="container">
<table id="employee-grid" width="auto" cellpadding="1" cellspacing="1" border="1" class="table table-hover">
<?php
include_once '../storescripts/connect_to_mysql.php';
$num_rec_per_page = 5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = 1;
};
$start_from = ($page - 1) * $num_rec_per_page;
$sql = "SELECT * FROM products LIMIT $start_from, $num_rec_per_page";
?>
<thead>
<tr class="success">
<th>Id</th>
<th>Product Name</th>
<th>Price</th>
<th>Status</th>
<th>Quantity</th>
<th>Details</th>
<th>Category</th>
<th>Subcategory</th>
<th>Date Added</th>
<th colspan="2">Functions</th>
</tr>
</thead>
<?php
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM products WHERE product_name LIKE '%" . $term . "%' or price LIKE '%" . $term . "' or details LIKE '%" . $term . "'";
}
$r_query = mysql_query($sql);
if ($r_query > 1) {
while ($row = mysql_fetch_array($r_query)) {
echo "<tr bgcolor='red'>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['product_name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "<td>" . $row['details'] . "</td>";
echo "<td>" . $row['category'] . "</td>";
echo "<td>" . $row['subcategory'] . "</td>";
echo "<td>" . $row['date_added'] . "</td>";
echo "<td><a href='product_listing_edit.php?id=" . $row['id'] . "'>Edit</a></td>";
echo "<td><a name='delete' href='product_listing_delete.php?id=" . $row['id'] . "'>Delete</a></td><tr>";
echo "</tr>";
}
} else {
echo "Nothing should be displayed";
}
?>
</table>

Having an issue with deleting entry from database on my website

I have echoed my database table onto my website and have tick option next to the table where you can tick it and delete any of the entries from the website, it should delete it from the database. For some reason its not working, I'm a beginner and any help would be greatly appreciated thanks.
<form method="" action="tester.php">
<?php
include 'connect_to_mysql.php';
$count=mysql_num_rows($result);
$result = mysql_query("SELECT * FROM booking ORDER BY ID ASC");
echo "<table border='1'>
<tr>
<th>DEL</th>
<th>Name</th>
<th>Email ID</th>
<th>Phone Number</th>
<th>Collection Address</th>
<th>Collection Date</th>
<th>Collection Time</th>
<th>Make & Model</th>
<th>Message</th>
</tr>";
while($row = mysql_fetch_array($result))
{
?>
<td align="center" bgcolor="#FFFFFF"><input name="delete_these[]" type="checkbox" id="checkbox[]" value="<?php echo $row['ID']; ?>"></td>
<?php
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['phonenumber'] . "</td>";
echo "<td>" . $row['collectionaddress'] . "</td>";
echo "<td>" . $row['collectiondate'] . "</td>";
echo "<td>" . $row['collectiontime'] . "</td>";
echo "<td>" . $row['makemodel'] . "</td>";
echo "<td>" . $row['message'] . "</td>";
echo "</tr>";
}
echo "</table>";
?> <br>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
// Check if delete button active, start this
if(isset($_GET['delete'])) {
for($i=0;$i<$count;$i++){
$id = $checkbox[$i];
print_r($_GET['delete_these']);
$ids = implode(', ', $_POST['delete_these']);
$sql = "DELETE FROM booking WHERE id IN($ids)";
echo "<br />SQL: $sql<br />";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
}
}
mysql_close();
?>
</form>
you have add method in your form tag--
<form method="post" action="tester.php" >
and also replace if(isset($_GET['delete'])) to if(isset($_POST['delete']))
Replace your form tag by:
<form method="post" action="tester.php">
then these 3:
if(isset($_POST['delete']))
$id =(int)$_POST['delete_these'][$i];
$sql = "DELETE FROM booking WHERE id=$id";
at the place of these 3:
if(isset($_GET['delete']))
$id = $checkbox[$i];
$sql = "DELETE FROM booking WHERE id IN($ids)";
Your $id = $checkbox[$i]; works only if register_globals directive is on, which is now deprecated (and removed as of PHP 5.4) because of security issues it causes, and if it exists in the version of PHP you are using most hosting services turn it off.
You'll be sending the data through post so you access them through $_POST. the (int) before the $_POST is to cast anything to integer so if the user try to send a string it will become 0, this will protect you from sql injection.
SIDE NOTE you should stop using mysql_* functions as they are deprecated. Check out PDO or mysqli

Deleting rows from table on web retrieved from database

I am having issue with deleting rows from a database that I echoed onto my website, I have used tick check boxes and when multiples are selected they should be deleted. But it's just NOT HAPPENING! Nothing is getting deleted from the database! please help!
<form method="" action="tester.php">
<?php
include 'connect_to_mysql.php';
$count=0;
$count=mysql_num_rows($result);
$result = mysql_query("SELECT * FROM booking ORDER BY ID ASC");
echo "<table border='1'>
<tr>
<th>DEL</th>
<th>Name</th>
<th>Email ID</th>
<th>Phone Number</th>
<th>Collection Address</th>
<th>Collection Date</th>
<th>Collection Time</th>
<th>Make & Model</th>
<th>Message</th>
</tr>";
while($row = mysql_fetch_array($result))
{
?>
<td align="center" bgcolor="#FFFFFF"><input name="delete_these[]" type="checkbox" id="checkbox[]" value="<?php echo $row['ID']; ?>"></td>
<?php
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['phonenumber'] . "</td>";
echo "<td>" . $row['collectionaddress'] . "</td>";
echo "<td>" . $row['collectiondate'] . "</td>";
echo "<td>" . $row['collectiontime'] . "</td>";
echo "<td>" . $row['makemodel'] . "</td>";
echo "<td>" . $row['message'] . "</td>";
echo "</tr>";
}
echo "</table>";
?> <br>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
// Check if delete button active, start this
if(isset($_GET['delete'])) {
for($i=0;$i<$count;$i++){
$id =(int)$_POST['delete_these'][$i];
$sql = "DELETE FROM booking WHERE ID='$id'";
print_r($_GET['delete_these[]']);
$sql = "DELETE FROM booking WHERE id IN($ids)";
echo "<br />SQL: $sql<br />";
$result = mysql_query($sql);
}
if($result){
}
}
mysql_close();
?>
</form>
First off you can just implode() all the gathered ids from the form and from there build the query.
Sample code:
<form method="POST" action="index.php">
<table>
<?php while($row = mysql_fetch_array($result)): ?>
<tr>
<td><input type="checkbox" name="delete_these[]" value="<?php echo $row['id']; ?>" /></td>
<td><?php echo $row['name']; ?></td>
</tr>
<?php endwhile; ?>
</table>
<input type="submit" name="delete" value="Delete Selected" />
</form>
<?php
$selected_values = array();
if(isset($_POST['delete'])) {
$selected_values = $_POST['delete_these'];
$ids = implode(',', $selected_values);
$query = mysql_query("DELETE FROM booking WHERE id IN($ids)");
// this becomes -> delete from booking where id in (1, 2, 3, ...)
}
?>
and while you still can, use mysqli or PDO, its free anyway

drop down menu not appearing in the following php code, but every other element is in the table

I'm using php to create a table from the data extracted from MySQL database. I'm using SELECT * From table_name and all data is appearing as I wish. But I want to create a Drop Down menu in the last column for that I'm using the function displayDMenu. But the drop down menu is still not appearing in the table. Can you please suggest what is causing the problem and solution? Or alternate solutions? Here's the code:
<div id="main">
<?php
include("config.php");
$result = mysql_query("SELECT * FROM schedule");
echo "<table border='1'>
<tr>
<th>Appoint No.</th>
<th>CR number</th>
<th>Time</th>
<th>Date</th>
<th>Month</th>
<th>Year</th>
<th>Department</th>
<th>Status</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td style='padding:3px'>" . $row['S_ID'] . "</td>";
echo "<td style='padding:3px'>" . $row['crnumber'] . "</td>";
echo "<td style='padding:3px'>" . $row['ScdTime'] . "</td>";
echo "<td style='padding:3px'>" . $row['ScdDate'] . "</td>";
echo "<td style='padding:3px'>" . $row['ScdMonth'] . "</td>";
echo "<td style='padding:3px'>" . $row['ScdYear'] . "</td>";
echo "<td style='padding:3px'>" . $row['DName'] . "</td>";
echo "<td 'style='padding:3px'>" . displayDMenu() . "</td>";
echo "</tr>";
}
echo "</table>";
function displayDMenu() {
$r = '';
$r .='<form method="post" action="ScdApproval.php">';
$r .='<select name="Status">';
$r .='<option value="approved" selected>Approve</option>';
$r .='<option value="disapproved">Disapprove</option>';
$r .='</select>';
$r .='</form>';
}
?>
</div> <!--main ends here -->
Not sure why you need the user function its not doing anything logical its just spitting back html which there is no reason not to put that html in the while loop.
<?php
include("config.php");
$result = mysql_query("SELECT * FROM schedule");
?>
<style>#main table tr td{padding:3px;}</style>
<div id="main">
<table border="1">
<tr>
<th>Appoint No.</th>
<th>CR number</th>
<th>Time</th>
<th>Date</th>
<th>Month</th>
<th>Year</th>
<th>Department</th>
<th>Status</th>
</tr>
<?php
if(mysql_num_rows($result) > 0):
while($row = mysql_fetch_array($result)): ?>
<tr>
<td><?php echo $row['S_ID']?></td>
<td><?php echo $row['crnumber']?></td>
<td><?php echo $row['ScdTime']?></td>
<td><?php echo $row['ScdDate']?></td>
<td><?php echo $row['ScdMonth']?></td>
<td><?php echo $row['ScdYear']?></td>
<td><?php echo $row['DName']?></td>
<td><form method="post" action="ScdApproval.php">
<select name="Status">
<option value="approved" selected>Approve</option>
<option value="disapproved">Disapprove</option>
</select>
</form>
</td>
</tr>
<?php
endwhile;
else: ?>
<tr>
<td rowspan="8">No results</td>
</tr>
<?php endif;?>
</table>
</div> <!--main ends here -->
Also:
Really you should also switch to using PDO or mysqli prepared querys for new code.
You need to return the value $r in the function displayDMenu()
ie
function displayDMenu()
{
.........
return $r;
}

Categories