Issue deleting rows from displayed table

user3236628

So I have an issue getting rows to be deleted using the following code. The table displays the information correctly, plus sends the correct id in the url to the delete.php page, but I cannot get it to complete the command. Changing the code slightly on the delete.php I can get it to show either:

Couldn't delete the index.

or:

Binding parameters failed: 0

    <?php
$con = mysqli_connect("localhost","user","pass","database");
// Check connection
if (mysqli_connect_errno())
{
    die("Failed to connect to MySQL: " . mysqli_connect_error());
}

if (!$result = mysqli_query($con,"SELECT * FROM mytable ORDER BY `server_name`;"))
{
    die("Error: " . mysqli_error($con));
}
?>
<table border='1'>
<tr>
<th><b>Server Name</b></th>
<th><center><b>Port</b></center></th>
<th><center><b>Mod</b></center></th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row['server_name']; ?></td>
<td><center><?php echo $row['server_port']; ?></center></td>
<td><center><?php echo $row['mod']; ?></center></td>
<td><center><a href="delete.php?id=<?php echo $row['index']; ?>"><img src="images/remove.png" width="16" height="16"></center></img></a></td>
</tr>
<?php
}
mysqli_close($con);
?>
</table>

My delete file is

    <?php
// Your database info
$db_host = 'localhost';
$db_user = 'user';
$db_pass = 'pass';
$db_name = 'database';

if (!isset($_GET['id']))
{
    echo 'No ID was given...';
    exit;
}

$con = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($con->connect_error)
{
    die('Connect Error (' . $con->connect_errno . ') ' . $con->connect_error);
}

$sql = "DELETE FROM mytable WHERE 'index' = " . $_GET['id'];
if (!$result = $con->prepare($sql))
{
    die('Query failed: (' . $con->errno . ') ' . $con->error);
}

if (!$result->bind_param('i', $_GET['ID']))
{
    die('Binding parameters failed: (' . $result->errno . ') ' . $result->error);
}

if (!$result->execute())
{
    die('Execute failed: (' . $result->errno . ') ' . $result->error);
}

if ($result->affected_rows > 0)
{
    echo "The ID was deleted with success.";
}
else
{
    echo "Couldn't delete the index.";
}
$result->close();
$con->close();

It has to be something simple but I cannot figure it out.

Barmar

You're using the wrong type of quotes in your SQL. To escape a table or column name that contains a reserved word, you use backticks. And if you're using bind_param, you have to put ? in the query where the parameter will be substituted.

$sql = "DELETE FROM mytable WHERE `index` = ?";

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Deleting all rows from Cassandra cql table

From Dev

Issue with Deleting rows from my second Table in my page

From Dev

Deleting multiple rows from a table

From Dev

How does deleting rows from a table affect its indexes?

From Dev

deleting massive rows from excel

From Dev

Trigger to delete rows from related tables before deleting rows from actual table

From Dev

Tetris Deleting Rows Issue

From Dev

Deleting rows from 3 tables in MySQL table

From Dev

Limit Chars displayed in table issue

From Dev

PostgreSQL: deleting rows referenced from another table

From Dev

How to limit the number of rows displayed in a table?

From Dev

Dynamically added table rows not deleting

From Dev

Issue with deleting object from parse

From Dev

Deleting Checked rows from gridview

From Dev

c# deleting duplicate rows from data table

From Dev

Deleting all rows from table that share the same ID

From Dev

Issue by deleting an item from a list

From Dev

Efficiently deleting rows from one table where not matching another [MySQL]

From Dev

Deleting many rows from a big table MySql 5.5.46

From Dev

Shiny: Deleting rows from data

From Dev

Deleting multiple rows from a table

From Dev

Deleting all rows from table, and resetting auto incrementation

From Dev

deleting the displayed key from database in php

From Dev

MySQL: deleting rows based on a condition with data from another table and NO JOIN

From Dev

Issue by deleting an item from a list

From Dev

deleting a row from table

From Dev

Deleting multiple rows from TableView

From Dev

Deleting rows in HTML table

From Dev

iText7 - Displayed text in table issue

Related Related

HotTag

Archive