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"];
Related
Welcome.php
<?php
session_start();
if (!isset($_SESSION['id'])) {
header('location:login.php');
}
?>
<!DOCTYPE html>
<html>
<body>
<?php
include_once 'connect.php';
$query = mysqli_query($mysqli, "select * from `users` where userid='" . $_SESSION['id'] . "'");
$row = mysqli_fetch_array($query);
echo 'Welcome - ' . $row['username'];
?>
<br>
Logout
<br><br>
<?php
include_once 'connect.php';
$result = mysqli_query($mysqli, "SELECT * FROM users");
echo "<table border='1'>
<tr>
<th>User_ID</th>
<th>Name</th>
<th>Username</th>
<th>E-mail</th>
<th>Department</th>
<th>Date_of_birth</th>
<th>Age</th>
<th>Image</th>
<th>Action</th>
<th>Action</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['userid'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['department'] . "</td>";
echo "<td>" . $row['Date_of_birth'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td><img alt='image' style='width:100px;height:50px;' src='upload/" . $row['filename'] . "'></td>";
echo "<td> <a href='editform.php?id=" . $row['userid'] . "'>Edit</a></td>";
echo "<td> <a href='deleteform.php?id=" . $row['userid'] . "'>Delete</a></td>";
echo "</tr>";
}
echo "</table>";
include_once 'connect.php';
if (isset($_POST["happy"])) {
if (empty($_POST["happy"])) { //not empty name
echo '<br>';
echo "No letter entered";
} else {
$id = $_GET['id'];
$sql = "SELECT * FROM `users` WHERE userid='$id'";
$result = mysqli_query($mysqli, $sql);
$row = mysqli_fetch_array($result);
$sql = "select name, username, email, Date_of_birth from `users` where (name LIKE '%$name%' OR username LIKE '%$name%' OR email LIKE '%$name%' OR Date_of_birth LIKE '%$name%')";
if (mysqli_query($mysqli, $sql) === TRUE) {
print_r($id);
} else {
echo 'No record found';
}
}
}
?>
<form method="post">
<br>
Search: <input type="text" name="happy"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Problem:
I have a welcome page with all the users data who have registered. In my welcome page their is an input field, so when I enter anyone's name,username,email,D.O.B say pavan or pavan1994#gamil.com , then all the row matches with this entered data, only those row should be displayed.
I have updated your code please try it.
<?php
session_start();
if (!isset($_SESSION['id'])) {
header('location:login.php');
}
?>
<!DOCTYPE html>
<html>
<body>
<?php
include_once 'connect.php';
$query=mysqli_query($mysqli,"select * from `users` where userid='".$_SESSION['id']."'");
$row=mysqli_fetch_array($query);
echo 'Welcome - '.$row['username'];
?>
Logout
<?php
//include_once 'connect.php';
$result = mysqli_query($mysqli,"SELECT * FROM users");
echo "<table border='1'>
<tr>
<th>User_ID</th>
<th>Name</th>
<th>Username</th>
<th>E-mail</th>
<th>Department</th>
<th>Date_of_birth</th>
<th>Age</th>
<th>Image</th>
<th>Action</th>
<th>Action</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['userid'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['department'] . "</td>";
echo "<td>" . $row['Date_of_birth'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td><img alt='image' style='width:100px;height:50px;' src='upload/".$row['filename']."'></td>";
echo "<td> <a href='editform.php?id=" . $row['userid'] . "'>Edit</a></td>";
echo "<td> <a href='deleteform.php?id=" . $row['userid'] . "'>Delete</a></td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
//include_once 'connect.php';
if(isset($_POST["happy"])){
if(empty($_POST["happy"])){ //not empty name
echo '<br>';
echo "No letter entered";
}else {
$name = $_POST["happy"];
$id = $_GET['id'];
$sql = "SELECT * FROM `users` WHERE userid='$id'";
$result = mysqli_query($mysqli, $sql);
$row = mysqli_fetch_array($result);
$sql="select name, username, email, Date_of_birth from `users` where (name LIKE '%$name%' OR username LIKE '%$name%' OR email LIKE '%$name%' OR Date_of_birth LIKE '%$name%')";
if (mysqli_query($mysqli, $sql) === TRUE) {
print_r($id);
}else {
echo 'No record found';
}
}
}
?>
<form method="post">
<br>
Search: <input type="text" name="happy"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
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'];
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'm trying to assign anID to the table I'm generating with PHP but it keeps returning an error. Here is the full code that works fine so far. All I want is to add an 'id' to the table so I can apply css styles to it in the relevant sheet
<?php
$con=mysqli_connect("localhost","<un>","<pw>","monitor");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM presnationalresults ORDER BY Percentage DESC");
echo "<table border='1'>
<tr>
<th>President</th>
<th>Party</th>
<th>Votes</th>
<th>Percentage</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['PresidentName'] . "</td>";
echo "<td>" . $row['PartyCode'] . "</td>";
echo "<td>" . $row['Votes'] . "</td>";
echo "<td>" . $row['Percentage'] . "</td>";
}
echo "</table>";
mysqli_close($con);
Any help? maybe I'm going about it the wrong way?
Please try below block of code . I have added table id and also close </tr> inside while loop
<?php
$con = mysqli_connect("localhost", "<un>", "<pw>", "monitor");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM presnationalresults ORDER BY Percentage DESC");
echo "<table border='1' id='table-id'>
<tr>
<th>President</th>
<th>Party</th>
<th>Votes</th>
<th>Percentage</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['PresidentName'] . "</td>";
echo "<td>" . $row['PartyCode'] . "</td>";
echo "<td>" . $row['Votes'] . "</td>";
echo "<td>" . $row['Percentage'] . "</td>";
echo "</tr>"; // you forget close tr
}
echo "</table>";
mysqli_close($con);
I am creating a database and when creating the HTML script the table is coming back with no boarders = Firstname Lastname ";
while($row = mysqli_fetch_array($result)) {
echo "";
echo "" . $row['Firstname'] . "";
echo "" . $row['Lastname'] . "";
echo "";
}
echo "";
mysqli_close($con); ?>
Here is the code I created, could someone look at it and tell me what I have done wrong?
<?php
$con=mysqli_connect("localhost","root","","Franchise_Call_Log");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM caller_info");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
th>Franchise</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Firstname'] . "</td>";
echo "<td>" . $row['Lastname'] . "</td>";
echo "<td>" . $row['Franchise'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Look at the tag
<th>Franchise</th>
Please change your file extension to .php it is not rendering your script. Html file not render your php, and also correct your markup as suggested by #suhail.