0

I'm trying to create a button for each row in my database that, when pressed, will delete this particular row. I should also mention that the data from the database is displayed correctly and the table I'm using is also completely fine.The buttons appear at the side of each row, when the button is clicked, the row dissapears but the data is not deleted from the database, when the page is reloaded the rows that were previously "deleted", reappear. After pressing the button i also get this "Fatal error: Uncaught Error: Call to undefined function mysql_query() in C:\xampp\htdocs\INDUSTRIALPROJECT\records.php:56 Stack trace: #0 {main} thrown in C:\xampp\htdocs\INDUSTRIALPROJECT\records.php on line 56". line 56 is : $del = mysql_query("DELETE FROM records WHERE id=" . $row['id']);. The same query works fine when placed directly into phpMyAdmin.

    <?php
// Check connection
include_once 'config.php';
if ($link->connect_error) {
die("Connection failed: " . $link->connect_error);
}
$sql = "SELECT * FROM records";
$result = $link->query($sql);
function post($key){ return(isset($_POST[$key]) ? htmlspecialchars($_POST[$key]) : ""); }
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
    if(post('rowButton'.$row['id']) =="Delete"){
        $del = mysql_query("DELETE FROM records WHERE id=" . $row['id']);
        $deleted = '<p>Entry ' . $row['id'] . ' was succesfully deleted</p>';
    }
    else {
        echo '<form action ="' . $_SERVER['PHP_SELF'] . '" method="post">';
        echo "<tr>";
        echo "<td>" . $row['id'] . "</td>";
        echo "<td>" . $row["visitingdate"]. "</td>";
        echo "<td>" . $row["department"] . "</td>";
        echo "<td>" . $row["visitingreason"]. "</td>";
        echo "<td>" . $row["importance"]. "</td>";
        echo "<td>" . $row["visitorname"]. "</td>";
        echo "<td>" . $row["company"]. "</td>";
        echo "<td>". $row["internalrecipientname"]. "</td>";
        echo "<td>". $row["visitinglocation"]. "</td>";
        echo "<td>". $row["ETA"]. "</td>";
        echo "<td>". $row["ETD"]. "</td>";
        echo "<td>". $row["HRverification"]. "</td>";
        echo "<td>". $row["visitcompleted"]. "</td>";
        echo '<td><input type="submit" name="rowButton'. $row['id'] .'" value="Delete"/> </td>';
        echo "</tr>";
        echo "</form>";
    }
}
echo "</table>";
echo $deleted;
} 
else { echo "0 results"; }
$link->close();
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
isidoros _
  • 11
  • 4

1 Answers1

0

First, this appears to be rather vulnerable to SQL injection attacks. StackOverflow is quite font of pointing this out up front, because it's really a solved problem that you should account for in the early stages of development. You're taking untrusted data (that was submitted by the user, without sanitizing it) and putting it directly in an SQL query. Bad things can happen when that occurs. Now with that aside, on to your actual question.

"Nothing happens" means the page doesn't change at all, right? So the browser doesn't know what to do when the button is clicked.

I think you haven't put any <form...> declaration here, which would be required for <input type="submit"> to do anything useful. You could use JavaScript with the stand alone submit button, but I don't see that in your code, either. You'll need something to tell the browser what to do when the submit button is pressed.

I haven't really tested the rest of your code, but based on what you've got already you might add the following:

    else {
+         echo '<form action ="' . $_SERVER['PHP_SELF'] . '" method="post">';
        echo "<tr>";

and

        echo "</tr>";
+        echo "</form>";
    }

(don't add the plus sign, that's just to show which line is added). I should add that I don't usually use submit buttons like this, so there's a chance I missed some additional details about how you're calling this, but putting the form in a <form> tag is at least a good start.

Edit

The mysql_query() function was removed in PHP 7; if you're using an older PHP you need to add support for the MySQL functions or if you're on PHP 7, you should use the MySQLi or PDO_MySQL functions instead. The warning box on the PHP manual page for mysql_query has some links for alternatives, how to select an alternative, and other supporting documentation that may help you. This StackOverflow answer may help, as well.

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
  • Benetch Thanks for taking the time and answering my question, I updated my code and the description based on what I added after your first comment. – isidoros _ May 19 '20 at 02:50
  • @isidoros_ I have edited my answer based on the new error you're getting. You'll have to modify your code to use mysqli or pdo functions (I generally prefer mysqli in these cases because I find it's easier to switch from mysql to mysqli than from mysql to pdo). – Isaac Bennetch May 19 '20 at 14:42
  • you are right, I edited the code to the code that worked for me about 10 hours ago but it was edited again 1 hour ago and a rollback to a previous edit was made, I don't really understand why but everything you have posted so far have been the correct answers, cheers. – isidoros _ May 19 '20 at 14:49