Function prepare() on a non-object error - php

I have looked up the error for this and I think I am calling the statement before for it to be initialized. I have made a simple connection class that I can include into all of my files that will be talking to the mysql server. Knowing how I am with things, I am most likely over thinking things. I cant seem to find what I am doing wrong.....
Top part of the code is cut off as it only contains the HTML head and php starting code that is non-important for this.
//include database connection
include('connection.php');
$action = isset($_GET['action']) ? $_GET['action']: "";
if($action=='delete'){ //if the user clicked ok, run our delete query
try {
$query = "DELETE FROM sc_steamgames WHERE appid = ?";
$stmt = $con->prepare($query);
$stmt->bindParam(1, $_GET['appid']);
$result = $stmt->execute();
echo "<div>Record was deleted.</div>";
}catch(PDOException $exception){ //to handle error
echo "Error: " . $exception->getMessage();
}
}
//select all data
$query = "SELECT * FROM sc_steamgames";
$stmt = $con->prepare( $query );
$stmt->execute();
//this is how to get number of rows returned
$num = $stmt->rowCount();
echo "<a href='add.php'>Create New Record</a>";
if($num>0){ //check if more than 0 record found
echo "<table border='1'>";//start table
//creating our table heading
echo "<tr>";
echo "<th>AppID</th>";
echo "<th>Title</th>";
echo "<th>Release Date</th>";
echo "<th>Last Updated</th>";
echo "</tr>";
//retrieve our table contents
//fetch() is faster than fetchAll()
//http://stackoverflow.com/questions/2770630/pdofetchall-vs-pdofetch-in-a-loop
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
//extract row
//this will make $row['firstname'] to
//just $firstname only
extract($row);
//creating new table row per record
echo "<tr>";
echo "<td>{$appid}</td>";
echo "<td>{$title}</td>";
echo "<td>{$releasedate}</td>";
echo "<td>{$lastupdate}</td>";
echo "<td>";
//we will use this links on next part of this post
echo "<a href='edit.php?id={$appid}'>Edit</a>";
echo " / ";
//we will use this links on next part of this post
echo "<a href='#' onclick='delete_user( {$appid} );'>Delete</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";//end table
}else{ //if no records found
echo "No records found.";
}
?>
<script type='text/javascript'>
function delete_user( appid ){
//this script helps us to
var answer = confirm('Are you sure?');
if ( answer ){ //if user clicked ok
//redirect to url with action as delete and id to the record to be deleted
window.location = 'index.php?action=delete&id=' + appid;
}
}
</script>
</body>
</html>
connection.php
/* Database Info */
// Host/IP
$host = "localhost";
// Database Name
$db_name = "**";
// Username
$username = "**";
//Password
$password = "**";
/* End Database Info */
try {
$con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
}catch(PDOException $exception){ //to handle connection error
echo "Connection error: " . $exception->getMessage();
}

Related

How to get variable from header in multi query?

i already had forms where I got the variable from the header but the forms have always been pdo and always one single query. This form is connected via mysqli and I just can't figure out how to get a variable.
<?php
$mysqli = new mysqli("localhost:3307", "root", "root", "test");
if($mysqli->connect_errno)
die ("Connection failed".$mysqli->connect_error);
$query = "SELECT * FROM contacts WHERE id = ?;";
$query .= "SELECT * FROM companies WHERE id = ?;";
if($mysqli->multi_query($query)) {
do{
$result = $mysqli->store_result();
$finfo = $result->fetch_fields();
echo"<table border ='1'>";
echo "<tr>";
foreach($finfo as $f) {
echo "<th>".$f->name."</th>";
}
echo "<br>";
echo "<br>";
echo "</tr>";
while($row = $result->fetch_assoc()) {
echo "<tr>";
foreach($row as $v) {
echo "<td>".$v."</td>";
}
echo "</tr>";
}
} while ($mysqli->more_results() && $mysqli->next_result());
}
?>
So the column "id" in both tables is the PK/FK and I want to retrieve information where id = ?.
How do I get the ? variable from the header and pass it on?
I feel like in my past tries I got the variable successfully with this code
$id=isset($_GET['id']) ? $_GET['id'] : die('ERROR: Record ID not found.');
[...]
$statement = $mysqli->prepare($query);
$statement->bindParam(1, $id);
$statement->execute();
but didn't echo it correctly.
Thank you in advance!

How can I pull in field data by ID from MySQL database?

I have two PHP files: itemTransaction.php and recordItemTransaction.php. I can select which row I would like to record a transaction for from a table in itemTransaction, and it links to the correct row in the database in recordItemTransaction.php, leading to a form allowing me to edit the itemQuantity. I have a form that has a hidden ID field and a textbox for the user to enter in an updated itemQuantity, which will be submitted to the database upon submission. I would like to display the current itemQuantity to the user, so when they edit the itemQuantity, they know what the current quantity is before they edit it and record the transaction.
My issue is that in recordItemTransaction.php, I cannot figure out how to pull in both the values for ID and itemQuantity in the same file.
This links to recordItemTransaction.php. Since I am referencing ID here, I can retrieve it in the next file. But I cannot retrieve itemQuantity along with the ID. Only one or the other. So, when I switch it to...
...I can retrieve the itemQuantity value in the textbox, but when I submit the form, it cannot tell which row to update.
itemTransaction.php
$query = "SELECT * FROM `Items` WHERE `isActive` = 'Active'";
$result = mysqli_query($con, $query);
echo "<h1>Record Transaction | Items</h1>";
echo "<a href='../inventoryIndex.php'><button class='button'>Back</button></a>";
//Display Data
echo "<table class='applyFont' cellspacing='0' cellpadding='0'>";
echo "<tr>";
echo "<th></th>";
echo "<th>ITEM</th>";
echo "<th>COST</th>";
echo "<th>RECORD TRANSACTION</th>";
echo "</tr>";
while($row=mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td align='center' width='9%'><img src='/InventoryManager/InventoryManagerImages/Items/{$row['itemImage']}' width='115' height='125' style='display:block'></td>";
echo "<td align='center' width='30%'>{$row['description']}</td>";
echo "<td align='center' width='30%'>$ {$row['unitCost']}</td>";
echo "<td align='center'><a href='recordItemTransaction.php?ID={$row['ID']}'><img src='/InventoryManager/InventoryManagerImages/Icons/couple-of-arrows-changing-places.png' title='Record an update to inventory'></td>";
echo "</tr>";
}
?>
</body>
</html>
recordItemTransaction.php
>
>
<?php
if(isset($_POST['updateQuantity'])) {
//Connect to DB
$hostname = "******";
$username = "******";
$password = "******";
$dbName = "******";
$con = mysqli_connect($hostname, $username, $password, $dbName);
//Get Value From User
$itemQuantity = $_POST["itemQuantity"];
$ID = $_POST["ID"];
//Query to Update Data
$query = "UPDATE `Items` SET `itemQuantity`='$itemQuantity' WHERE ID='$ID'";
$result = mysqli_query($con, $query);
//Check if Query Was Successful
if($result) {
echo "<p style=font-family:'Roboto Condensed', sans-serif>Item quantity has been updated</p>";
} else {;
echo "<p style=font-family:'Roboto Condensed', sans-serif>Error updating the quantity of the item.</p>" . mysqli_error();
}
//Disconnect From DB
mysqli_close($con);
}
?>
<body>
</body>
</html>

Mysql returning highest id value while not requested in PHP

My PHP script is returning the Mysql value with the highest ID, not the requested ID:
<?php
$db = mysqli_connect('localhost', 'root', '(Password)', 'web');
#THE 'userEmail' variable is from a script above not displayed on here
$email = $row['userEmail'];
$result = mysqli_query($db, "SELECT * FROM table WHERE email = '$email'");
echo "<center><table border='1'>
<tr>
<th>Username</th>
<th>Password</th>
<th>Active?</th>
<th>Add Account</th>
<th>Delete Account</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
$userName = $row['username'];
$passWord = $row['password'];
$addId = $row['id'];
$deleteId = $row['id'];
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['activeStatus'] . "</td>";
echo "<td><a href=home.php?add=$addId>Add Account</a></td>";
echo "<td><a href=home.php?delete=$deleteId>Delete Account</a></td>";
echo "</tr>";
}
echo "</table></center>";
if( isset($_GET['delete']) ) {
$deleteId = $_GET['delete'];
$delete = "DELETE FROM table WHERE id = $deleteId";
$result = mysqli_query($db, $delete);
deleteLineInFile("/var/www/html/table-users.txt","'$userName'");
if ($result == TRUE) {
echo "Record updated successfully";
header('Location: home.php');
} else {
echo "Error updating record";
}
}
if( isset($_GET['add']) ) {
$addId = $_GET['add'];
$add = "(Too long to display)";
echo("$userName, $passWord");
file_put_contents("/var/www/html/directory/$addId.xml", $add);
header("Location: directory/$addId.xml");
}
function deleteLineInFile($file,$string)
{
$i=0;$array=array();
$read = fopen($file, "r") or die("can't open the file");
while(!feof($read)) {
$array[$i] = fgets($read);
++$i;
}
fclose($read);
$write = fopen($file, "w") or die("can't open the file");
foreach($array as $a) {
if(!strstr($a,$string)) fwrite($write,$a);
}
fclose($write);
}
?>
Every time I try to get the ID, it only displays the id with the highest value. I don't know what the problem is. I think it is a problem with the row because the $userName = $row['username']; and $passWord = $row['password']; lines are stored in the loop. I noticed that it will pick up the id for the $_GET variable correctly, but it won't for the $row variable. Could someone help me figure out this situation?
When the form is submitted, $userName and $password don't contain the info for the submitted ID, they contain the last value from the while loop. You need to do a database query to look up the corresponding username.
if( isset($_GET['add']) ) {
$addId = $_GET['add'];
$add = "(Too long to display)";
$stmt = mysqli_prepare($db, "SELECT username, password FROM table WHERE id = ?");
mysqli_stmt_bind_param($stmt, "i", $addId);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $userName, $passWord);
if (mysqli_stmt_fetch($stmt)) {
echo("$userName, $passWord");
file_put_contents("/var/www/html/directory/$addId.xml", $add);
header("Location: directory/$addId.xml");
} else {
die("User ID $addId not found");
}
}
I've also shown how to use a prepared statement to protect against SQL injection. You should use this method everywhere.
As Barmar and I noted, your $userName and $passWord variables are being overwritten by the while loop. As such the ID specified in $_GET['add'] and $_GET['delete'] requests will not be associated with the correct $userName.
In order to resolve the issue, you must lookup the user details by the ID associated with the desired add or delete $_GET request.
Note: the use of the object oriented mysqli syntax is used below, as it is less verbose and personally easier for me to read.
<?php
#/var/www/html/home.php
$db = mysqli_connect('localhost', 'root', '(Password)', 'web');
/* ensure an expected action is requested and determine associated ID */
$actionId = null;
if (!empty($_GET['add'])) {
$actionId = $_GET['add'];
} elseif (!empty($_GET['delete'])) {
$actionId = $_GET['delete'];
}
/* execute action before outputting user table (use strict comparison operator !==) */
if (null !== $actionId) {
/* an action was specified - retrieve the associated user details from the database */
if (!$stmt = $db->prepare('SELECT username, password FROM table WHERE id = ?')) {
die('Unable to retrieve user details.');
}
$stmt->bind_param('s', $actionId);
$stmt->execute();
$stmt->bind_result($userName, $passWord);
if (!$stmt->fetch()) {
die('Unknown user specified');
}
/* execute desired add action */
if (isset($_GET['add'])) {
# debug code prevents redirect
die("$userName, $passWord");
$add = '(Too long to display)';
/* use __DIR__ as opposed to full path in case you ever move hosts */
file_put_contents(__DIR__ . "/directory/$actionId.xml", $add);
header("Location: directory/$actionId.xml");
exit;
}
/* execute desired delete action */
if (isset($_GET['delete'])) {
if (!$stmtDelete = $db->prepare('DELETE FROM table WHERE id = ?')) {
die('Error updating record');
}
$stmtDelete->bind_param('s', $actionId);
if (!$result = $stmtDelete->execute()) {
die('Error updating record');
}
/*
* only check file if it was deleted from the database successfully
* use __DIR__ as opposed to full path in case you ever move hosts
*/
deleteLineInFile(__DIR__ . '/table-users.txt', "'$userName'");
# debug code prevents redirect
die('Record updated successfully');
header('Location: home.php');
exit;
}
}
/*
* this is where <html> should start unless you utilize output buffering
* output the HTML and your table data after your request action, to ensure the redirects occur
*/
$email = $row['userEmail']; //retrieved from somewhere else
if (!$stmt = $db->prepare('SELECT id, username, password, activeStatus FROM table WHERE email = ?')) {
die('Unable to retrieve account list');
}
$stmt->bind_param('s', $email);
$stmt->bind_result($userId, $userName, $passWord, $activeStatus);
echo "<center><table border='1'>
<tr>
<th>Username</th>
<th>Password</th>
<th>Active?</th>
<th>Add Account</th>
<th>Delete Account</th>
</tr>";
while ($stmt->fetch()) {
//always escape user supplied values being output to HTML
$userName = htmlentities($userName, ENT_QUOTES, 'UTF-8', false);
$passWord = htmlentities($passWord, ENT_QUOTES, 'UTF-8', false);
$activeStatus = htmlentities($activeStatus, ENT_QUOTES, 'UTF-8', false);
echo "<tr>";
echo "<td>" . $userName . "</td>";
echo "<td>" . $passWord . "</td>";
echo "<td>" . $activeStatus . "</td>";
echo "<td><a href='home.php?add=" . $userId . "'>Add Account</a></td>";
echo "<td><a href='home.php?delete=" . $userId . "'>Delete Account</a></td>";
echo "</tr>";
}
echo "</table></center>";
I also suggest converting your HTML attributes from using single quote ' to using double quote ", to ensure compatibility and uniformity with any other library you may use.

Unknown Error when Deleting Records using PDO

I am using this code which allows me to see my DB records in a table and in this table there is an option to delete or edit the records.
Only I get this error message and I can't figure out what I am doing wrong.
The error message:
Fatal error: Call to a member function prepare() on a non-object in C:\wamp\www\Inventaris++\NinjaCodeDelete.php on line 32
The code:
<?php
include'Connect2db3.php';
$action = isset($_GET['action']) ? $_GET['action']: "";
if($action=='delete'){ //if the user clicked ok, run our delete query
try {
$query = "DELETE FROM BCD WHERE id = ?";
$stmt = $conn->prepare($query);
$stmt->bindParam(1, $_GET['id']);
$result = $stmt->execute();
echo "<div>Record was deleted.</div>";
}catch(PDOException $exception){ //to handle error
echo "Error: " . $exception->getMessage();
}
}
$query = "SELECT ID, Categorie, SerieNummer, MacAdress, ProductCode, Prijs, RekNummer, PaletNummer, Hoeveelheid, Aantekeningen FROM BCD";
$stmt = $conn->prepare( $query );
$stmt->execute();
$num = $stmt->rowCount();
echo "<a href='reports.php'>View Reports</a>";
if($num>0){ //check if more than 0 record found
echo "<table border='1'>";//start table
echo "<tr>";
echo "<th>Categorie</th>";
echo "<th>SerieNummer</th>";
echo "<th>MacAdress</th>";
echo "<th>ProductCode</th>";
echo "<th>Prijs</th>";
echo "<th>RekNummer</th>";
echo "<th>PaletNummer</th>";
echo "<th>Hoeveelheid</th>";
echo "<th>Aantekeningen</th>";
echo "</tr>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
echo "<tr>";
echo "<td>{$Categorie}</td>";
echo "<td>{$SerieNummer}</td>";
echo "<td>{$MacAdress}</td>";
echo "<td>{$ProductCode}</td>";
echo "<td>{$Prijs}</td>";
echo "<td>{$RekNummer}</td>";
echo "<td>{$PaletNummer}</td>";
echo "<td>{$Hoeveelheid}</td>";
echo "<td>{$Aantekeningen}</td>";
echo "<td>";
echo "<a href='edit.php?id={$id}'>Edit</a>";
echo " / ";
echo "<a href='#' onclick='delete_user( {$id} );'>Delete</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";//end table
}else{
echo "No records found.";
}
?>
<script type='text/javascript'>
function delete_user( id ){
var answer = confirm('Are you sure?');
if ( answer ){
window.location = 'NinjaCodeDelete.php?action=delete&id=' + id;
}
}
</script>
I also want to say that I am not an advanced programmer I found this code online, where it seemed to be working for the other people who have used it.
I have some experience with Mysql and php but not with PDO.
I hope u can help me!
thank you in advanced.
What is inside your " include'Connect2db3.php'; "?
And with this I'm refering to connect2db3.php and is this file inside the right folder?
Your connection could look like this:
<?php
$config['conn'] = array(
'host' => 'yourHostName',
'username' => 'yourUserName',
'password' => 'yourPassword',
'dbname' => 'yourDBName'
);
$conn = new PDO('mysql:host=' . $config['conn']['host'] . ';dbname=' . $config['conn']['dbname'], $config['conn']['username'], $config['conn']['password']);
?>

Issue with Deleting rows from my second Table in my page

I have a Reports page with 2 tables, one show items in stock the other shows Items sold.
I made a delete option for them which provides a button at the right end side of the table to delete rows out of my table.
The Issue that I am having is that the Code works perfectly for the 1st table but for the second table, the code will execute but the data does not get deleted from the DB.
I think what is happening is that due to me using the same Code to delete from both tables, that only 1 works. ( I think I am not sure)
After looking at it for a while trying to find potential errors I made and trying to see what else might be the issue, I decided to ask u for help!
Here the code:
<?php
$config['conn'] = array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'inventarisdb'
);
$conn = new PDO('mysql:host=' . $config['conn']['host'] . ';dbname=' . $config['conn']['dbname'], $config['conn']['username'], $config['conn']['password']);
$action = isset($_GET['action']) ? $_GET['action']: "";
if($action=='delete'){ //if the user clicked ok, run our delete query
try {
$query = "DELETE FROM BCD WHERE id = ?";
$stmt = $conn->prepare($query);
$stmt->bindParam(1, $_GET['id']);
$result = $stmt->execute();
echo "<div>Record was deleted.</div>";
}catch(PDOException $exception){ //to handle error
echo "Error: " . $exception->getMessage();
}
}
//select all data
$query = "SELECT ID, Categorie, SerieNummer, MacAdress, ProductCode, Prijs, RekNummer, PaletNummer, Hoeveelheid, Aantekeningen FROM BCD";
$stmt = $conn->prepare( $query );
$stmt->execute();
//this is how to get number of rows returned
$num = $stmt->rowCount();
if($num>0){ //check if more than 0 record found
echo "<table border='1'>";//start table
//creating our table heading
echo "<tr>";
echo "<th>Categorie</th>";
echo "<th>SerieNummer</th>";
echo "<th>MacAdress</th>";
echo "<th>ProductCode</th>";
echo "<th>Prijs</th>";
echo "<th>RekNummer</th>";
echo "<th>PaletNummer</th>";
echo "<th>Hoeveelheid</th>";
echo "<th>Aantekeningen</th>";
echo "</tr>";
//retrieve our table contents
//fetch() is faster than fetchAll()
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
//extract row
//this will make $row['firstname'] to
//just $firstname only
extract($row);
//creating new table row per record
echo "<tr>";
echo "<td>{$Categorie}</td>";
echo "<td>{$SerieNummer}</td>";
echo "<td>{$MacAdress}</td>";
echo "<td>{$ProductCode}</td>";
echo "<td>{$Prijs}</td>";
echo "<td>{$RekNummer}</td>";
echo "<td>{$PaletNummer}</td>";
echo "<td>{$Hoeveelheid}</td>";
echo "<td>{$Aantekeningen}</td>";
echo "<td>";
//we will use this links on next part of this post
echo "<a href='#' onclick='delete_user( {$ID} );'>Delete</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";//end table
}else{ //if no records found
echo "No records found.";
}
?>
<script type='text/javascript'>
function delete_user( id ){
var answer = confirm('Are you sure?');
if ( answer ){ //if user clicked ok
//redirect to url with action as delete and id to the record to be deleted
window.location = 'Remove.php?action=delete&id=' + id;
}
}
</script>
<br/><br/><br/><br/><br/><br/><br/><br/><br/>
<?php
$config['conn'] = array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'inventarisdb2'
);
$conn = new PDO('mysql:host=' . $config['conn']['host'] . ';dbname=' . $config['conn']['dbname'], $config['conn']['username'], $config['conn']['password']);
$action = isset($_GET['action']) ? $_GET['action']: "";
if($action=='delete'){ //if the user clicked ok, run our delete query
try {
$query = "DELETE FROM CDE WHERE id = ?";
$stmt = $conn->prepare($query);
$stmt->bindParam(1, $_GET['id']);
$result = $stmt->execute();
echo "<div>Record was deleted.</div>";
}catch(PDOException $exception){ //to handle error
echo "Error: " . $exception->getMessage();
}
}
//select all data
$query = "SELECT ID2, Klant, Categorie1, SerieNummer1, MacAdress1, ProductCode1, Prijs1, Hoeveelheid1, Aantekeningen1 FROM CDE";
$stmt = $conn->prepare( $query );
$stmt->execute();
//this is how to get number of rows returned
$num = $stmt->rowCount();
if($num>0){ //check if more than 0 record found
echo "<table border='1'>";//start table
//creating our table heading
echo "<tr>";
echo "<th>Klant</th>";
echo "<th>Categorie1</th>";
echo "<th>SerieNummer1</th>";
echo "<th>MacAdress1</th>";
echo "<th>ProductCode1</th>";
echo "<th>Prijs1</th>";
echo "<th>Hoeveelheid1</th>";
echo "<th>Aantekeningen1</th>";
echo "</tr>";
//retrieve our table contents
//fetch() is faster than fetchAll()
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
//extract row
//this will make $row['firstname'] to
//just $firstname only
extract($row);
//creating new table row per record
echo "<tr>";
echo "<td>{$Klant}</td>";
echo "<td>{$Categorie1}</td>";
echo "<td>{$SerieNummer1}</td>";
echo "<td>{$MacAdress1}</td>";
echo "<td>{$ProductCode1}</td>";
echo "<td>{$Prijs1}</td>";
echo "<td>{$Hoeveelheid1}</td>";
echo "<td>{$Aantekeningen1}</td>";
echo "<td>";
//we will use this links on next part of this post
echo "<a href='#' onclick='delete_user( {$ID2} );'>Delete</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";//end table
}else{ //if no records found
echo "No records found.";
}
?>
<script type='text/javascript'>
function delete_user( id ){
var answer = confirm('Are you sure?');
if ( answer ){ //if user clicked ok
//redirect to url with action as delete and id to the record to be deleted
window.location = 'Remove.php?action=delete&id=' + id;
}
}
</script>
So in short: 1st Table: everything works, all data gets deleted
2nd Table: Appears to be working but after confirming the Delete, the data is still there and didn't get removed from my DB.
The Code for Table 2 is exactly the same as the code for Table 1 exepct for the Names of DB and Table etc.
I am hoping you can go over my code see if you notice anything that might be causing this.
Maybe if u agree with what I was thinking, that the same code will not work for both tables on the same page, that you can give an example or a link to how I can tackle this issue?
Sorry for the Long code!
Thank you in advanced!
You shouldn't mix the delete's because you might have an instance when one id be the same as the other, so you'll delete the wrong thing. But I don't believe that is your primary problem:
Your select is
SELECT ID2, Klant, Categorie1, SerieNummer1, MacAdress1, ProductCode1, Prijs1, Hoeveelheid1, Aantekeningen1 FROM CDE
But your delete is:
DELETE FROM CDE WHERE id = ?";
You're delete should probably be:
DELETE FROM CDE WHERE ID2 = ?";
To Prevent Deleting the wrong thing:
The easiest thing to do here, is change you're delete user JavaScript to accept an action parameter and specify which delete you want to perform, because both delete attempts are running right now.
JavaScript
You don't need the JavaScript twice on the same page. Just have it one time in your HEAD or right before the end of the body.
<script type='text/javascript'>
function delete_user( action, id ){
var answer = confirm('Are you sure?');
if ( answer ){ //if user clicked ok
//redirect to url with action as delete and id to the record to be deleted
window.location = 'Remove.php?action=' + action + '&id=' + id;
}
}
</script>
Checking Action
if ($action=='delete_BCD') {
// or
if ($action=='delete_CDE') {
Rendering Rows
echo "<a href='#' onclick='delete_user( \"delete_BCD\", {$ID2} );'>Delete</a>";
// or
echo "<a href='#' onclick='delete_user( \"delete_CDE\", {$ID2} );'>Delete</a>";

Categories