Below is my code for a PHP echo delete that contains a delete button. When pressed I want that entry to be deleted from the database
$result = mysql_query("SELECT * ,CONCAT (HomeScore,'-',AwayScore) AS Score, CONCAT(Against) AS Game FROM Fixture WHERE TeamID='9' ORDER BY Date DESC");
echo "<table id='customers' border='1'>;
<tr>
<th>FixtureID</th>
<th>Competition</th>
<th>Match</th>
<th>Date</th>
<th>Time</th>
<th>Score</th>
<th>test</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FixtureID'] . "</td>";
echo "<td>" . $row['Competition'] . "</td>";
echo "<td>" . $row["Against"] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "<td>" . $row['Time'] . "</td>";
echo "<td>" . $row['Score'] . "</td>";
echo "<td><form method=post>
<input name=id type=hidden value='".$row['FixtureID']."';>
<input type=submit name=submit value=Delete>
</form></td>";
echo "</tr>";
echo "</tr>";
}
}
echo "</table>";
// delete record
if($_SERVER['REQUEST_METHOD'] == "POST")
{
if(isset($_POST['FixtureID']))
{
$id = FixtureID;
$sql = mysql_query("DELETE FROM Fixture WHERE FixtureID =$id");
if(!$sql)
{
echo ("Could not delete rows" .mysql_error());
}
}
}
How do I get this to work? Also FixtureID is stored as an integer in the database.
Try with these changes :
// delete record
if(isset($_POST['submit']) && isset($_POST['id']) && !empty($_POST['id'])) {
$id = $_POST['id'];
$sql = mysql_query("DELETE FROM Fixture WHERE FixtureID =$id");
if(!$sql) {
echo ("Could not delete rows" .mysql_error());
}
}
Edit :
// Delete record if ID submitted
if(isset($_POST['submit']) && isset($_POST['id']) && !empty($_POST['id'])) {
$id = $_POST['id'];
$sql = mysql_query("DELETE FROM Fixture WHERE FixtureID =$id");
if(!$sql) {
echo ("Could not delete rows" .mysql_error());
}
}
// Get datas from BDD
$result = mysql_query("SELECT * ,CONCAT (HomeScore,'-',AwayScore) AS Score, CONCAT(Against) AS Game FROM Fixture WHERE TeamID='9' ORDER BY Date DESC");
// Display data
echo "
<table id='customers' border='1'>
<tr>
<th>FixtureID</th>
<th>Competition</th>
<th>Match</th>
<th>Date</th>
<th>Time</th>
<th>Score</th>
<th>test</th>
</tr>";
// For each result
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FixtureID'] . "</td>";
echo "<td>" . $row['Competition'] . "</td>";
echo "<td>" . $row["Against"] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "<td>" . $row['Time'] . "</td>";
echo "<td>" . $row['Score'] . "</td>";
echo "<td><form method=post>
<input name=id type=hidden value='".$row['FixtureID']."';>
<input type=submit name=submit value=Delete>
</form></td>";
echo "</tr>";
echo "</tr>";
}
echo "</table>";
just try this
if(isset($_POST['submit']))
{
$id = $_POST['id']; // here you should use form post value
$sql = mysql_query("DELETE FROM Fixture WHERE FixtureID =$id");
if(!$sql)
{
echo ("Could not delete rows" .mysql_error());
}
}
}
$id value is not properly set
$id = $_POST['FixtureID'];
Related
I want to delete full row from a form into a database in php when i click on delete button. But there are "Undefined index on line 39" issues come on my page and when i click on delete button it redirect me on different page and didn't delete a row.
how can i delete a row on one click ??
Please help me.
Thanks,
Nabeel
<body>
<a href="" >delete</a>
<a href="" >create</a>
<label>Read</label>
<?php
$con=mysqli_connect("localhost","root","","firstphp");
if($con)
{
}
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM users`");
echo "<table border='1'>
<tr>
<th>id</th>
<th>username</th>
<th>passward</th>
<th>name</th>
<th>delete</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['passward'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['<th><a href="<?php $_PHP_SELF ?>" >delete</a></th>'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
<label>End Read</label>
<br /><br /><hr />
<label>Delete</label>
<?php
if(isset($_POST['delete']))
{
$con=mysql_connect("localhost","root","");
if(!$con)
{
die("Could not connect: " . mysql_error());
}
$id = $_POST["id"];
$sql = "DELETE FROM users WHERE id = $id";
mysql_select_db('firstphp');
$result = mysql_query($sql,$con);
if(!$result)
{
die("Could not delete data: " . mysql_error());
}
echo "Deleted data successfully\n";
mysql_close($con);
}
else
{}
?></body>
change
echo "<td>" . $row['<th><a href="<?php $_PHP_SELF ?>" >delete</a></th>'] . "</td>";
in
echo '<th><a href="'.$_SERVER['PHP_SELF'].'?id='. $row['id'] .'&delete=true" >delete</a></td>';
and change
if(isset($_POST['delete']))
//...
$id = $_POST["id"];
in
if(isset($_GET['delete']))
//...
$id = $_GET["id"];
I am stuck in one of my application module. I have to set multiple delete options in my application.
I have used the below code. The designing is perfectly fine. All what I want to add multiple delete options in it.
Below is the code which I have tried out:
<html>
<head>
<title>Strad Hosting Limited -Web Hosting</title>
<script language="javascript">
function validate()
{
var chks = document.getElementsByName('checkbox[]');
var hasChecked = false;
for (var i = 0; i < chks.length; i++)
{
if (chks[i].checked)
{
hasChecked = true;
break;
}
}
if (hasChecked == false)
{
alert("Please select at least one.");
return false;
}
return true;
}
</script>
</head>
<body >
<?php
$con=mysqli_connect("localhost","stradsol","D#v,b5TnQ!D!","stradsol_sales");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<form name='form1' method='post' action='' onSubmit='return validate();'>";
echo "<table border='1' style='
background-color: white;'>
<tr>
<th></th>
<th>id</th>
<th style='padding-left: 11px;'>Lead Generated Date </th>
<th>name</th>
<th>email</th>
<th>contacts</th>
<th>requirement</th>
<th style='display:none;'>Company name</th>
<th style='display:none;'>Address</th>
<th>Next Follow-Up date</th>
<th>Final_Details</th>
<th style='display:none;'>Status</th>
<th style='padding-left: 12px; display:none;'>Lead Closed Date</th>
<th>EDIT</th>
<th>Follow-up History</th>
<th>Add Follow-up</th>
<th>Delete Record</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><input name='checkbox[]' type='checkbox' id='checkbox[]' value='".$rows['id']."'></td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td><a href='config_info.php?id=" . $row['id'] . "'>".$row['name']."</a></td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['contacts'] . "</td>";
echo "<td>" . $row['requirement'] . "</td>";
echo "<td style='display:none;'>" . $row['company_name'] . "</td>";
echo "<td style='display:none;'>" . $row['address'] . "</td>";
echo "<td>" . $row['startdate'] . "</td>";
echo "<td>" . $row['final_details'] . "</td>";
echo "<td style='display:none;'>" . $row['status'] . "</td>";
echo "<td style='display:none;'>" . $row['lead_close'] . "</td>";
echo "<td><a href='edit_eg1.php?id=" . $row['id'] . "'>Edit</a></td>";
echo "<td><a href='Follow_history.php?id=" . $row['id'] . "'>Follow_history</a></td>";
echo "<td><a href='add_follow.php?id=" . $row['id'] . "'>Followup</a></td>";
echo "<td><a href='delete.php?id=" . $row['id'] . "'>Delete</a></td>";
echo "</tr>";
}
echo "<tr><td><input name='delete' type='submit' id='delete' value='Delete'></td></tr>";
$count=mysqli_num_rows($result);
if(isset($_POST['delete'])){
for($i=0;$i<count($_POST['checkbox']);$i++){
$del_id=$_POST['checkbox'][$i];
$sql = "DELETE FROM Persons WHERE id='$del_id'";
echo $sql;
$result2 = mysqli_query($con,$sql);
}
// if successful redirect to delete_multiple.php
if($result2)
{
echo "<meta http-equiv=\"refresh\" content=\"0;URL=edit_table_del.php\">";
}
}
mysqli_close($con);
echo "</table>";
echo "</form>";
?>
<br><br>
[ Back To Home ]
</body>
</html>
I am stuck, the query is not working I think. I don't know what I am missing.
Try replacing
$sql = "DELETE FROM Persons WHERE id='$del_id'";
With
$sql = "DELETE FROM Persons WHERE id=$del_id";
I'm assuming the ID is a number so the single quotes aren't needed.
Complete newbie to PHP/MySQL and HTML so please bear with me if I dont explain myself properly. At present I have an order form which stores the order in my database and shows it on a table in my admin area. I would like to be able to delete the row from the table and my database when I have completed the order.
At present my code looks like this:
<?php
//87229feely.php
include('connection.php');
$result = mysql_query("SELECT * FROM orderform");
echo "<table border='1' >
<tr>
<th><u>ID</th>
<th><u>Date</th>
<th><u>Product</th>
<th><u>Product Comments</th>
<th><u>Name</th>
<th><u>Address</th>
<th><u>Age</th>
<th><u>Delivery</th>
<th><u>Delete</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id']. "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['productcomments'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['delivery'] . "</td>";
echo "<td>delete</td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
//deleterow.php
include('connection.php');
$id = $_GET['id']; //this needs to be sanitized
if(!empty($id)){
$result = mysql_query("DELETE FROM orderform WHERE id=".$id.";");
}
header("Location: 87229feely.php");
?>
Any help? All greatly appreciated. Thanks
This answer does not meet security standards but should do the job:
<?php
//index.php
include('connection.php');
$result = mysql_query("SELECT * FROM orderform");
echo "<table border='1' >
<tr>
<th><u>ID</th>
<th><u>Date</th>
<th><u>Product</th>
<th><u>Product Comments</th>
<th><u>Name</th>
<th><u>Address</th>
<th><u>Age</th>
<th><u>Delivery</th>
<th><u>Delete</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id']. "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['productcomments'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['delivery'] . "</td>";
echo "<td>delete</td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
//delete.php
include('connection.php');
$id = $_GET['id']; //this needs to be sanitized
if(!empty($id)){
$result = mysql_query("DELETE FROM orderform WHERE id=".$id.";");
}
header("Location: index.php");
?>
Many ways to do this. Since you're a beginner, this would probably be the most straight-forward (albeit not how I would do it)
Create a form around the table
Add a button on the delete column for each row and assign an id to it (the ID should be the ID from your database) <input type="submit" name="submit" value"/*YOUR ID*/">
Add some processing script before the table is being parsed
if (isset($_POST['submit'])) {
$sql = "DELETE FROM table WHERE id='/*YOUR ID*/'";
mysql_query($sql);
}
First of all ,you need to send ID of completed order to next form. You cam do this by adding:
echo("<input type='hidden' name='orderID' value='".$row['id']."'/>");
That will create a hidden field with value of ID.
If your form uses POST:
then:
if(isset($_POST['submit'])){
$orderID = $_POST['orderID'];
mysql_query("DELETE FROM table WHERE id=$oderID");
}
If you are using GET method:
<form method="GET">
You could either use hidden field as mentioned above or you could parse ID of order in GET url:
<form action='action.php?id=".$row['id']."'>
and after submitting:
if(isset($_GET['submit']) && isset($_GET['id')){
$orderID = $_GET['id'];
mysql_query("DELETE FROM table WHERE id=$orderID");
}
Maybe something like this with PDO
<?php
include('connection.php');
?>
<form name="" action="delete.php" method="post">
<table border='1' >
<tr>
<th> </th>
<th><u>ID</th>
<th><u>Date</th>
<th><u>Product</th>
<th><u>Product Comments</th>
<th><u>Name</th>
<th><u>Address</th>
<th><u>Age</th>
<th><u>Delivery</th>
<th><u>Delete</th>
</tr>
<?php
try {
$conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$row = $conn->query('SELECT * FROM orderform');
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td><input type=\"checkbox\" name=\"id[]\" id=\"checkAll\" value=\"".$row['id']."\" /></td>"
echo "<td>" . $row['id']. "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['productcomments'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['delivery'] . "</td>";
echo "</tr>";
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>
</table>
<input type="submit" name="submit" value="submit" />
</form>
delete.php
<?php
$id
if(isset($_POST['submit']) {
include('connection.php');
try {
$conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql="DELETE FROM orderform WHERE id IN (".implode(',',$conn->quote($id)).")";
$stmt->exec($sql);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
}
I have trouble with creating an approval form as am still php beginner,
the idea is
user submit a form am setting a default value"0" in the approved row at the table..
so behind the scenes the admin shows all members from this table where approved="0"
and this is the code
<code>
<?php
$con = mysql_connect("localhost","ebarea_epic","...");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ebarea_epic", $con);
$query = "select * from medicalrep where approved='0'";
$result=mysql_query($query);
echo "<table border='1'>
<tr>
<th>User Name</th>
<th>Password</th>
<th>Mobile </th>
<th>Address</th>
<th>Faculty</th>
<th>Graduation Year</th>
<th>Region</th>
<th>Area</th>
<th>Line</th>
<th>Appointment Date</th>
<th>Resign Data</th>
<th>Job Title</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['Mobile'] . "</td>";
echo "<td>" . $row['Address'] . "</td>";
echo "<td>" . $row['Faculty'] . "</td>";
echo "<td>" . $row['Graduation Year'] . "</td>";
echo "<td>" . $row['Region'] . "</td>";
echo "<td>" . $row['Line'] . "</td>";
echo "<td>" . $row['Area'] . "</td>";
echo "<td>" . $row['Appointment'] . "</td>";
echo "<td>" . $row['Resign'] . "</td>";
echo "<td>" . $row['job_title'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</code>
I just want to add checkbox for every table user and when checked thier status changed to 1 in approved column
thanks all
$con = mysql_connect("localhost","ebarea_epic","...");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ebarea_epic", $con);
$query = "select * from medicalrep where approved='0'";
$result=mysql_query($query);
$i = 1; //counter for the checkboxes so that each has a unique name
echo "<form action='process.php' method='post'>"; //form started here
echo "<table border='1'>
<tr>
<th>User Name</th>
<th>Password</th>
<th>Mobile </th>
<th>Address</th>
<th>Faculty</th>
<th>Graduation Year</th>
<th>Region</th>
<th>Area</th>
<th>Line</th>
<th>Appointment Date</th>
<th>Resign Data</th>
<th>Job Title</th>
<th>Update</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['Mobile'] . "</td>";
echo "<td>" . $row['Address'] . "</td>";
echo "<td>" . $row['Faculty'] . "</td>";
echo "<td>" . $row['Graduation Year'] . "</td>";
echo "<td>" . $row['Region'] . "</td>";
echo "<td>" . $row['Line'] . "</td>";
echo "<td>" . $row['Area'] . "</td>";
echo "<td>" . $row['Appointment'] . "</td>";
echo "<td>" . $row['Resign'] . "</td>";
echo "<td>" . $row['job_title'] . "</td>";
echo "<td><input type='checkbox' name='check[$i]' value='".$row['ID']."'/>";
echo "</tr>";
$i++;
}
echo "</table>";
echo "<input type='submit' name='approve' value='approve'/>";
echo "</form>";
mysql_close($con);
Now comes process.php
if(isset($_POST['approve'])){
if(isset($_POST['check'])){
foreach ($_POST['check'] as $value){
$sql = "UPDATE post SET post_approved = 1 WHERE ID = $value"; //write this query according to your table schema
mysql_query($sql) or die (mysql_error());
}
}
}
though you are using mysql_* functions here, i recommend you to use PDO
EDIT:
As per your request, this is the update.
Change this code in your admin panel script:
echo "<input type='submit' name='approve' value='approve'/>";
Delete the above line and add this instead:
echo "<input class='action' type='button' name='approve' value='approve' />";
echo "<input class='action' type='button' name='edit' value='edit' />";
echo "<input class='action' type='button' name='delete' value='delete' />";
echo "<input type='hidden' name='action' value='' id='action' />"; //Action (edit, approve or delete) will be set here which will be passed as POST variable on form submission
Now you will need some javascript to do some tricks.
Add the following code preferably head section in your admin panel script
<script type="text/javascript" src="jquery-1.7.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.action').click(function(){
var action = $(this).attr('name');
$('#action').val(action);
$(this).closest('form').submit();
})
})
</script>
Now comes the modification in process.php file
if (isset($_POST['approve'])) {
if (isset($_POST['check'])) {
foreach ($_POST['check'] as $value) {
$sql = "UPDATE post SET post_approved = 1 WHERE ID = $value"; //write this query according to your table schema
mysql_query($sql) or die(mysql_error());
}
}
} elseif(isset($_POST['edit'])){
//do the edit things here
} elseif(isset($_POST['delete'])){
foreach ($_POST['check'] as $value){
$sql = "DELETE FROM post WHERE ID=$value";//modify it
mysql_query($sql) or die(mysql_error());
}
}
NOTE
You may not want to mutiple checkbox for edit. You just need to tweak the javascript code above a little and it'l send the ID as a post variable on form submission from which you can retreive the details for one entry and then edit functions will come. I'l leave it to you. try it, post your trial code here and i'l give you a solution if it doesn't work.
I want to create a compare sort of page. I currently have two tables with the information, but want it to be on table or join both together. The first row would show Name of brand, second would show cost, etc... I am grabbing the values of selected checkboxs and based on what they selected it get compared.
$testName = $_POST['testCompare'];
$rpl = str_replace("_", " ", $testName);
$firstOne = "$rpl[0]";
$secondOne = "$rpl[1]";
echo "$firstOne";
echo "<br/>";
echo "$secondOne";
$query = "SELECT * FROM test_table WHERE test_name = '$firstOne'";
$query2 = "SELECT * FROM test_table WHERE test_name = '$secondOne'";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
$result2 = mysql_query($query2) or die ("Error in query: $query2. " . mysql_error());
if (mysql_num_rows($result) > 0 && mysql_num_rows($result2) > 0) {
//if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_row($result)) {
if ($firstOne == $row[1]) {
{
echo "<table border=1>";
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<tr>";
echo "<td>" . $row[1] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row[2] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row[3] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row[4] . "</td>";
echo "</tr>";
}
}
}echo "</table>";
while($row2 = mysql_fetch_row($result2)) {
if ($secondOne == $row2[1]) {
{ echo "<table border=1>";
echo "<tr>";
echo "<td>" . $row2[0] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row2[1] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row2[2] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row2[3] . "</td>";
echo "</tr>";
echo "<td>" . $row2[4] . "</td>";
echo "</tr>";
}
}
}
echo "</table>";
}
else {
echo "No Results";
}[/CODE]
Thanks
Remove the </table> after the first loop.
Remove <table border=1> from both loops.
Add <table border=1 before the first loop.
Currently, you're defining a new <table border=1> each time you enter the loop. This will result in this HTML code:
<table ..>
<tr>...
<table ..>
..
<table>
..
Et cetera
</table>
<table ..>
Et cetera
</table>