PHP - SQL - DELETE Not Working

Aim

Delete all data where the ID is selected

Problem

The data is not deleted

Error

No Errors was prompted within the developer console

Work Description

Based on what I understand the codes function by retrieving the User ID (delete_userID) and then passing it into $delete_userID by using this line mysqli_real_escape_string($conn,$_POST['delete_userID']);. But I can’t understand why doesn’t the delete query work. I have viewed several online question regarding this and some of it was because of the missing database code which is mysql_select_db("data2017", $conn); but that does not seem to be the problem for me

Delete Codes

        <div class="modal-body">
          <p>Do you want to delete?</p>
        </div>
		<div class="form-group">
		  <label for="delete_userID" class="control-label">User ID:</label>
		  <input type="text" class="form-control" id="delete_userID" name="delete_userID" readonly/>
		</div>
        <div class="modal-footer">
          <input type="submit" id="delete" name="delete" class="btn btn-block bt-login" value="Delete" />
		  	<?php
				if(isset($_POST['delete'])){
					$delete_userID=mysqli_real_escape_string($conn,$_POST['delete_userID']);
                    mysql_select_db("data2017", $conn);
					$sql = "DELETE FROM pha_user WHERE id ='".$delete_userID."'";
					$Deletequery=mysqli_query($conn,$sql);
					if($Deletequery){
						echo "<script>alert('Delete Successful.');</script>";
					}
					else{
						echo "<script>alert('Delete Failed.');</script>";
					}
				}
			?>
        </div>
1 Like

run the query with a hardcoded id value directly in MySQL, i.e. not via php

you will see it works just fine and you have a php problem, not a database problem

1 Like

I just tried adding in a <form> and it worked!!! =D Thank you for your reply and Merry Christmas and Happy New Year

Are Prepared Statements not an option?

I’m a bit surprised seeing there are both obsolete “mysql_” and “mysqli_” used in the same code. I’ve never tried doing that. I guess PHP is OK with it?

In any case, you will want to lose the obsolete “mysql_” sooner rather than later.

2 Likes

I did not try doing that yet but I will try to test it out. Thanks for replying

Noted and Thanks for your advice.

You can open as many database connections as you want. That is pretty much the same with php or any other server-side language. Mixing different types of connections is to be avoided. I would also highly doubt that using two separate connections is appropriate in this case.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.