Delete button next to every row - php

I've got this script where users can see a table of projects. But only the admin can delete said projects.
$query = ("select * from projectlist");
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo "<tr>";
while( list($key, $value) = each($row)){
//Print value
echo "<td>" . $value . "</td>";
}
if($_SESSION['rights'] == 'administrator'){
echo "<td><i class='fa fa-times-circle-o'></i></td>";
}
echo "</tr>";
}
Every row has an id called projectid, which get called with the $query = ("select * from projectlist");.
You can see that if the admin is logged in, there will be an icon displayed. What I want it to do is that if the user clicks on the icon, the row will be deleted. I'm not really good with php, it took me a while only to get this script working.
If anyobody can help me with this, I'd appreciate it.

try this,
$query = ("select * from projectlist");
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo "<tr>";
while( list($key, $value) = each($row)){
//Print value
echo "<td>" . $value . "</td><td id='del".$value."'><button></button></td>";
}
if($_SESSION['rights'] == 'administrator'){
echo "<td><i class='fa fa-times-circle-o'></i></td>";
}
echo "</tr>";

Instead of mysql_fetch_array($result, MYSQL_ASSOC) you can use mysql_fetch_array($result)
$query = ("select * from projectlist");
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($result))
{
$id = $result['id']; // you unique column in table
$col1 = $result['col1']; //similarly all other columns
echo "<td>$col1</td>";
if($_SESSION['rights'] == 'administrator'){
echo "<td><a href='your_current_page.php?id=$id'><i class='fa fa-times-circle-o'></i></a></td>";
}
echo "</tr>";
}
Code To Delete Row
if(isset($_GET['id']))
{
$id= $_GET['id'];
mysql_query("delete from table where id='$id'");
echo "deleted";//whatever you want to do
}

Ajax is not complicated at all.
I prefer jQuery when I need Ajax:
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type='text/javascript' src='//code.jquery.com/jquery-1.11.0.js'></script>
<script type='text/javascript'>
$(function() { // when page loded
$(".del").on("click",function(e) { // clicking on a class=del thing
e.preventDefault(); // stop any submission from the button
$.post("delete.php",{ "id":$(this).val()},function(data) {
$("#somemessageDiv").html(data);
});
});
);
</script>
using class instead of ID since ID must be unique
echo "<td>" . $value . "</td>";
if($_SESSION['rights'] == 'administrator'){
echo "<td><button class='del' value='".value."'>DELETE</button></td>";
}
Now you need delete.php to take an id and return success or error

Create a form for delete particular row.
Create one hidden field with value will be id of that row.
Create one deleterow.php and write code for delete that row from database using delete query of mysql.
if($_SESSION['rights'] == 'administrator') {
<form type='post' action ='deleterow.php'>
<input type="hidden" name='rowid' value='<?php echo $rowid ;?>' />
echo "<td><input type='submit' name="submit" value="delete" /></td>";
</form>
}

Related

I want a simple php table inside a while loop, with add or subtract

I am new to php and need some help please.
I'd like to create a simple php while loop.
Inside the loop I want to be able to add or subtract values.
My code seems to be doing what I want, except that i do not know what to put in the $count variable.
The aim is to have one table, and inside that table either add or subtract values of 5.
Here is my code.
<?php
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["id"] . "</td>";
echo "<td>" . $row["userName"] . "</td>";
echo "<td>" . $row["email"] . "</td>";
echo "<td>" . $row["balance"] . "</td>";
$count = $row["id"];
?>
<td>
<form name="form" method="post" action="">
<input type="submit" name="add" value="+">
<input type="submit" name="subtract" value="-">
<?php
if (isset($_POST['add'])) {
$sql2 = "UPDATE users SET balance = balance + 5 where id = $count ";
$result2 = $conn->query($sql2);
}
if (isset($_POST['subtract'])) {
$sql2 = "UPDATE users SET balance = balance - 5 where id = $count";
$result2 = $conn->query($sql2);
}
echo $row["id"];
?>
</form>
</td>
</tr>
<?php
}
} else {
echo "0 results";
}
?>
You must send the row id back to the php script, not just the action 'add' or 'subtract'. You can identify the row by the id you want to update. The code will look something like this:
if (isset($_POST['id'], $_POST['add'])) {
$stmt = $conn->prepare('UPDATE users SET balance = balance + 5 WHERE id = ?');
if (!$stmt) {
die('Prepare failed: '.$conn->error);
}
$stmt->bindParam('d', $_POST['id']);
$result = $stmt->execute();
if (!$result) {
die('Execute failed: '.$stmt->error);
}
}
if (isset($_POST['id'], $_POST['substract'])) {
// ...
}
You add this code before the while loop and remove the existing query stuff from inside the while loop.

Create hyperlink on table result and fill editable form in other page

I'm having problems to find how to create an hyperlink in a table column result and then, on click, open another page with all fields (textboxes) filled. Imagine when a click an ID, i do a select * from table where column_id = ID... Is there a way to do it?
Thanks.
Best regards
I'm not completely sure what you are asking, but this may help you a bit.
First make a Javascript.
<script type="text/JavaScript">
function selectID() {
var ID = document.getElementById("ID").value;
document.location.href ="yoursite.php?ID="+ID;
}
</script>
Then connect to your database to query the table for a link ID (or more) for example by changing the variable $value.
<?php
//Connect to database
mysql_connect("host", "user", "pass");
mysql_select_db("db_name");
$value = 'something';
$ID = $_GET['ID'];
if (!$ID) {
$ID = 0;
}
if ($ID == 0) {
$query = "SELECT * FROM table WHERE `column_1` = '$value'";
$result = mysql_query($query);
echo "<table>";
while($myrow = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>";
echo "ID";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
elseif ($ID > 0) {
$query2 = "SELECT * FROM table WHERE `column_id` = '$ID'";
$result2 = mysql_query($query2);
while($myrow2 = mysql_fetch_array($result2)) {
$value1 = $myrow2['column_1'];
$value2 = $myrow2['column_2'];
}
echo "<form type=\"GET\" action=\"$PHP_SELF\">";
echo "<input type=\"text\" id=\"ID\" name=\"ID\" value=\"$ID\"><br>";
echo "<input type=\"text\" id=\"value1\" name=\"value1\" value=\"$value1\"><br>";
echo "<input type=\"text\" id=\"value2\" name=\"value2\" value=\"$value2\"><br>";
echo "<input type=\"hidden\" id=\"search\" name=\"search\" value=\"searching\">";
echo "<input type=\"submit\" id=\"submitbutton\" name=\"submitbutton\" value=\" Search \">";
echo "</form>";
}
?>

Get variable from another table in a WHILE loop

<?php
$id = $_SESSION['user_id'] ;
echo "<form method='post' action='#'>";
echo "</select>
<p>Which Hospital Would You Like to Submit To?</p>";
$queryitem = "SELECT * FROM vendor_hospital WHERE vendor_hospital.user_id = '$id' AND vendor_hospital.approval_status = '1'" or die('MYSQL error: ' . mysql_error());
if ($result = mysql_query($queryitem)) {
if ($success = mysql_num_rows($result) > 0) {
echo "<select name='hospital_name'>";
echo "<option>-- Select A Facility --</option>";
while ($row = mysql_fetch_array($result))
echo "<option value='$row[manufacturer_id]'>$row[manufacturer_id]</option>";
echo "</select><br><br>";
} else {
echo "No results found.";
}
} else {
echo "Failed to connect to database.";
}
echo "<input type='submit' value='Submit' name='submit' class='button' /></form>";
?>
For some reason I'm stuck here. I'm just trying to get the manufacturer name to show in my options instead of the manufacturer_id. The manufacturer name is a foreign key in another table so I can't simply call $row[manufacturer_id] in my option tag. What should I do here? My only thought is to run a query inside the option tag for every manufacturer_id listed as a value but I'm sure that is overkill. Can someone point me in the right direction of a more elegant solution than that?
Not really sure what you are trying to do...but try this.
Replace this:
while ($row = mysql_fetch_array($result))
echo "<option value='$row[manufacturer_id]'>$row[manufacturer_id]</option>";
echo "</select><br><br>";
With this:
while ($row = mysql_fetch_array($result)){
echo "<option value='".$row['manufacturer_id']."'>".$row['manufacturer_id']."</option>";
}
echo "</select><br><br>";
Also be sure to double check and make sure that you are pulling from the right database, that it is populated, you are calling the right table names, etc...

How To Edit/Delete Stored Data From Database In PHP

EDIT
I have a mysql table with fields as follows:
Products - serial, name, description, price, picture.
the viewproducts.php page is as follows:
<?php
$result = mysql_query("SELECT * FROM products ")
or die(mysql_error()); ;
if (mysql_num_rows($result) == 0) {
echo 'There Arent Any Products';
} else {
echo "<table border='0'><table border='1' width=100%><tr><th>Product Name</th><th>Description</th><th>Price</th><th>Image</th><th>Edit</th><th>Delete</th>";
while($info = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $info['name']. "</td>";
echo "<td>" . $info['description']. "</td>";
echo "<td>£" . $info['price']." </td>";
echo "<td>" . "<img src='../getImage.php?id=" . $info['serial'] ."'/></td>";
echo '<td> Edit</td>';
}
}
echo "</tr>";
echo "</table>";
?>
my edit.php page looks like this:
<?php
$product_id = $_GET['serial'];
$result = mysql_query("SELECT * FROM products WHERE serial = '$product_id'")
or die(mysql_error()); ;
if (mysql_num_rows($result) == 0) {
echo 'There Arent Any Products';
} else {
echo "<table border='0'><table border='1' width=100%><tr><th>Product Name</th><th>Description</th><th>Price</th><th>Image</th><th>Edit</th><th>Delete</th>";
while($info = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $info['name']. "</td>";
echo "<td>" . $info['description']. "</td>";
echo "<td>£" . $info['price']." </td>";
echo "<td>" . "<img src='../getImage.php?id=" . $info['serial'] ."'/></td>";
}
}
echo "</tr>";
echo "</table>";
?>
when i click on edit from thr viewproducts.php page, it goes to edit.php page where nothing is showing up. the serial id on the address bar is coming up as follows:
http://www.********.com/****/admin/edit.php?product_id=
I want to be able to edit any product clicked on from the viewproduct.php page and transfered to edit.php page. I dont think my edit.php page is set up corretly.
Please help,
Thanks
You can pass via $_GET the id of the product and then, in the edit/delete page, retrieve that parameter. Obviously you have to sanitize the input properly before using it. For example, the link of the each product should look like this:
echo '<td>Edit</td>';
In the edit page you should have something like:
$id = $_GET['id'];
// For example, if the product id is an integer
// you can sanitize it doing this
$id = (int) $id
You could pass it as an argument to your php file in wich you want to edit/delete the product:
Edit Product
Then in your edit.php you will pick up the id of the product and load it's data from the database.
[edit.php]
$product_id = isset($_GET['product_id']) ? intval($_GET['product_id']) : null;
if(!$product_id) {
exit();
}
// query your database for the product
$row = mysqli_query("SELECT * FROM <table> WHERE product_id = $product_id");
// then you output your html with fields populated from the result from the database

dynamic dropdown filled from mysql data

What i'm trying to do is display a drop down with all field names from mysql database, once the user picks one and submits the form i want to display a second dropdown filled with all the rows from the submitted field name, this is my code so far:
$result = mysql_query("select * from `parts`") or die(mysql_error());
echo "<form action='".$_SERVER['PHP_SELF']."' method='post'>";
echo "<select name='field_names'>";
$i = 0;
while ($i < mysql_num_fields($result)) {
$fieldname = mysql_field_name($result, $i);
echo '<option value="'.$fieldname.'">'.$fieldname.'</option>';
$i++;
}
echo "</select>";
echo "<input type='submit' value='submit'></input>";
echo "</form>";
if($_POST) {
$fields = $_POST['field_names'];
$result1 = mysql_query("select '".$fields."' from `parts`") or die(mysql_error());
echo '<select name="fields">';
while ($row = mysql_fetch_array($result1)) {
echo "<option value=".$row[$fields].">".$row[$fields]."</option>";
}
echo '</select>';
}
Can anyone spot where i'm going wrong, thanks
there is a mistake on the line number 29
$result1 = mysql_query("select '" . $fields . "' from `parts`") or die(mysql_error());
you are using ' instead of `. Do as follows
$result1 = mysql_query("select `" . $fields . "` from `parts`") or die(mysql_error());
Hope your problem is solved.
As it stands now, the second set of selects will be issued OUTSIDE of your </form> tag, so will never get submitted with the rest of the form. At best, you should move the form closing tag to below the POST handler.
here database details
mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');
$sql = "SELECT username FROM userregistraton";
$result = mysql_query($sql);
echo "<select name='username'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['username'] ."'>" . $row['username'] ."</option>";}
echo "</select>";
here username is the column of my table(userregistration)
it works perfectly

Categories