DELETE FROM LOOP issue - php

When I run my code I get this error:
I've tested the SQL on my server and it correct which leads me to believe that it is the loop is incorrect.
I know about the security issues which I will fix next.
if(isset($_GET['rowID']))
{
$sql = 'DELETE FROM table WHERE id="' . $_GET['rowID'] . '";';
echo "<b>ECHO SQL<b><br>";
echo $sql;
if (mysqli_query($connection, $sql))
{
echo "Record deleted successfully";
}
else
{
echo "<br> Error deleting record: " . mysqli_error($connection);
}
}

Use the correct GET variable rowID instead of row on line 3:
if(isset($_GET['rowID']))
{
$sql = 'DELETE FROM table WHERE id="' . $_GET['rowID'] . '";';
echo "<b>ECHO SQL<b><br>";
echo $sql;
if (mysqli_query($connection, $sql))
{
echo "Record deleted successfully";
}
else
{
echo "<br> Error deleting record: " . mysqli_error($connection);
}
}

Related

PHP - How to verify record deleted successfully

This is the code I'm using for deleting row from my DB:
<?php
$eid = $_GET['eid'];
$con = mysqli_connect("localhost", "root", "","project") or die("Connection failed");
echo "connection is done";
$query = "delete from exam where eid='$eid'";
if ($con->query($query)==TRUE)
{
echo " record deleted";
}
else
{
echo "Error: " . $query . "<br>" . $con->error;
}
$con->close();
?>
The else statement is not getting executed. It displays "record deleted" for every value even if the value is not found in the database.
Why is this happening? how can I verify that my record has been deleted from my DB?
You can use mysqli.affected-rows.
Consider the following:
$query="delete from exam where eid='$eid'";
if ($con->query($query)==TRUE && $con->affected_rows > 0) {
echo " record deleted";
} else {
echo "Error: " . $query . "<br>" . $con->error;
}

value of variable is not accurately get by if condition in php

I am trying to check if the value of variable is equal to 0 but if condition is not working correctly. Please see the following code
if(isset($_GET['empid']))
{
$sql_query="SELECT accept leave_mgt WHERE empid=".$_GET['empid'];
$result_set=mysqli_query($conn, $sql_query);
if(isset($sql_query))
{
$sql="UPDATE leave_mgt SET accept='1'where empid=".$_GET['empid'];
$result=mysqli_query($conn,$sql);
if($result)
{
echo "test";
}
else
{
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
else
{
$sql="UPDATE leave_mgt SET accept='0'where empid=".$_GET['empid'];
$result=mysqli_query($conn,$sql);
if($result)
echo "testing";
else
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
Line 16
if(sizeof($result_set->fetch_assoc()) < 1) {

Fetching Multiple Rows, but Updating only One php

How do you update only one row when you fetch for multiple rows? I am lost on how to do this. I can fetch for one row, update and it works. However, I'm stuck for multiple rows.
Down below is my code
FETCH PHP:
//loop through for results
while($row = mysqli_fetch_array($data)){
echo "<tr><td>"
. $row['title']."<input type='hidden' value='".$row['title']."' name='title'>"
. "</td><td>"
. $row['author']
. "</td><td>"
. $row['summary']
. "</td><td>"
. $row['isbn']
. "</td><td>"
. "<img src='books/".$row['image']."' width='100%'/>"
. "</td><td>"
. "$<input type='text' value='".$row['price']."' size='8' class='pricetoright' name='price'>"
. "</td><td>"
. "<button type='submit' class='btn btn-default btngreen' name='update'>Update</button>"
. "</td><td>"
. "<button type='submit' class='btn btn-default btnred' name='delete'>Delete</button>"
. "</td></tr>";
}
UPDATE PHP:
// Check Connection
if(!($db = mysqli_connect($server, $user, $password, $database))) {
die('SQL ERROR: Connection failed: '.mysqli_error($db));
}
$price = floatval($_POST["price"]);
$title = $_REQUEST["title"];
// SQL to update a record
$query = "UPDATE books SET price = '$price' WHERE title = '$title'";
if (mysqli_query($db, $query)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($db);
}
mysqli_close($db);
Use form tag for each row. Also its better to use unique id to update row instead of title. So take hidden input of id.
You'd want to add a foreach() statement into your code. While it's a little unclear on your code here's a possible example:
foreach($_POST as $k=>$v){
if($k === "title"){
$title = $_POST["title"];
}
if($k === "price"){
$price = floatval($_POST["price"]);
}
// SQL to update a record
$query = "UPDATE books SET price = '$price' WHERE title = '$title'";
if (mysqli_query($db, $query)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($db);
}
}

Deleting a row with Php & MySQL

I am new to php and SQL and just toying around with a project for my own understanding of accessing, updating and deleting data from my Database.
I have managed to show the selected data, create a button to delete a specific Id but really needing some assistance with deleting the selected row or record instead of hard coding in the ID in my delete php script.
Here is an example of my script:
<?php
$sql = "SELECT id, firstname, lastname, joinDate FROM customers";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo
"<div class='trow'>" .
$row["id"]. ": " .
$row["firstname"] . " " .
$row["lastname"]. " " .
$row["joinDate"]. " " .
"<span class='deleteMember'>
<form action='deleteMember.php' method='POST'>
<button type='submit'>Delete</button>
</form>
</span>" . " " .
"<span class='editMember'><a href='#'>Edit</a></span>" .
"<br></div>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Here is the delete.php
<?php
// sql to delete a record
$sql = "DELETE FROM customers WHERE id='6' ";
if ($conn->query($sql) === TRUE) {
header("Location: index.php");
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>
what I would like it to do is, delete the row from which you hit the delete button from and not just delete the row I have specified in the delete.php script. I understand HOW it should work by posting the id but not sure how to do it.
Do like this
<?php
// sql to delete a record
$sql = "DELETE FROM customers WHERE id='".$_GET['id']."' ";
if ($conn->query($sql) === TRUE) {
header("Location: index.php");
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>
<?php
$sql = "SELECT id, firstname, lastname, joinDate FROM customers";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo
"<div class='trow'>" .
$row["id"]. ": " .
$row["firstname"] . " " .
$row["lastname"]. " " .
$row["joinDate"]. " " .
"<span><a href='deleteMember.php?id=".$row['id']."'>Delete</a></span>" .
"<span class='editMember'><a href='#'>Edit</a></span>" .
"<br></div>";
}
} else {
echo "0 results";
}
$conn->close();
?>
in place of your form use this
DELETE
and in your delete query must be like below
$sql = "DELETE FROM customers WHERE id='".$_GET['id']."' ";
or stay in your post form with:
while($row = $result->fetch_assoc()) {
echo
"<div class='trow'>" .
$row["id"]. ": " .
$row["firstname"] . " " .
$row["lastname"]. " " .
$row["joinDate"]. " " .
"<span class='deleteMember'>
<form action='deleteMember.php' method='POST'>
<input type='hidden' name='myid' value='".$row['id']."' />
<button type='submit'>Delete</button>
</form>
</span>" . " " .
"<span class='editMember'><a href='#'>Edit</a></span>" .
"<br></div>";
}
And in your delete.php :
<?php
$id=(int) $_POST['myid'];
// sql to delete a record
$sql = "DELETE FROM customers WHERE id=".$id;
if ($conn->query($sql) === TRUE) {
header("Location: index.php");
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>
No need to add extra form element for Delete or Edit purpose. Try this way to pass the id of row for Eelete or Edit operation
while($row = $result->fetch_assoc())
{
$id=$row['id'];// capture your row id & pass to your delete & edit
echo
"<div class='trow'>" .
$row["id"]. ": " .
$row["firstname"] . " " .
$row["lastname"]. " " .
$row["joinDate"]. " " .
"<span class='deleteMember'>
<a href='deleteMember.php?id=<?=$id;?>'>Delete</a>
</span>" . " " .
"<span class='editMember'>
<a href='editMember.php?id=<?=$id;?>'>Edit</a>
</span>" .
"<br>
</div>";
}
EDIT:
Then catch the id on your relevant page for your operation.
//deleteMember.php
<?php
$id=$_GET['id'];
// sql to delete a record
$sql = "DELETE FROM customers WHERE id='".$id."'";
if ($conn->query($sql) === TRUE) {
header("Location: index.php");
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>
Note: Please Use Prepared Statements of PDO or MYSQLi instead to avoid SQL Injection and manual escaping.

SQL Query succeeds but no information

Okay so my code works pretty well so far, it all goes through, my only problem is that when I try and print the unordered list and it's contents I get nothing. When I view my source code I have <ul> </ul>. There's a space, so surely something is happening.
This is my code, I have commented it slightly but what's happening is obvious:
$uname = mysqli_real_escape_string($link, $_SESSION['Username']); //Get username ready
$sql = mysqli_query($link, "SELECT * FROM users WHERE Username = '" . $uname . "'"); //SQL Query result
if(!$sql)
{
echo "Error retrieving User ID. Please try again. MySQL Error: " . mysqli_error($link);
}
elseif($row = mysqli_fetch_assoc($sql))
{
$uid = $row['UserID']; //Obtain UserID
}
else
{
echo "Error: " . mysqli_error($link) . "<br />" . $uname . " / " . $sql . " / " . $uid;
}
mysqli_free_result($sql);
$sql = mysqli_query($link, "SELECT * FROM auditions"); //Get everything from the auditions table
if(!$sql)
{
echo "Error retrieving auditions. Please try again later. Error: " . mysqli_error($link);
}
elseif($row = mysqli_fetch_assoc($sql))
{
if(mysqli_num_rows($sql)==0)
{
echo "Sorry, there are currently no open auditions. Please try back at a later date.";
}
else
{
echo "<ul>";
while($row = mysqli_fetch_assoc($sql))
{
echo "<li><a href='auditions.php?id=" . $row['AudID'] . "'>" . $row['AudName'] . "</a></li>";
}
echo "</ul>";
}
}
else
{
echo "Error: " . mysqli_error($link);
}
Where am I going wrong? The only thing it doesn't do is actually pick up any results and I've put some data into the table so there are entries! Otherwise it would say there aren't any. I've reversed this so it shows the message if there aren't 0 entries and that works. What am I doing wrong guys?
Thanks in advance.
You are fetching the result twice. Instead, only fetch the result in the while loop:
<?php
$sql = mysqli_query($link, "SELECT * FROM auditions"); //Get everything from the auditions table
if(!$sql)
{
echo "Error retrieving auditions. Please try again later. Error: " . mysqli_error($link);
}
else{
if(mysqli_num_rows($sql)==0)
{
echo "Sorry, there are currently no open auditions. Please try back at a later date.";
}
else
{
echo "<ul>";
while($row = mysqli_fetch_assoc($sql))
{
echo "<li><a href='auditions.php?id=" . $row['AudID'] . "'>" . $row['AudName'] . "</a></li>";
}
echo "</ul>";
}
}
?>
See this link for more information regarding mysql_fetch_assoc

Categories