Jump to content

PHP CRUD How delete a record


Herbert at GC

Recommended Posts

Hi guys this topic is from video tutorial (Complete web programing)"PHP CRUD".

From video tutorial "php_crud" -- videos: mysqli-2 "view.php" and mysqli- "delete.php"

 

Problem: Can't delete a record (please help codes below).

 

 

delete.php code

===========================================================

<?php

 

include('db-connect.php');

 

if (isset($_GET['id']) && is_numeric($_GET['id']))

{

$id = $_GET['id'];

if($stmt = $mysqli->prepare("DELETE FROM records WHERE id = ? LIMIT 1"))

{

$stmt->bind_param("i",$id);

$stmt->execute();

$stmt->close();

}

else

{

echo "Error: Could not prepare sql etatement";

}

$mysqli->close();

 

header("Location: view.php");

}

else

{

header("Location: view.php");

}

?>

===================================================================

 

view.php code

===================================================================

<?php

 

include('db-connect.php');

 

if ($result = $mysqli->query("SELECT * FROM records ORDER BY id"))

{

if($result->num_rows > 0);

{

echo "<table border='1' cellpadding='10'>";

echo "<tr><th>ID</th><th>First Name</th><th>Last Name</th><th></th><th></th></tr>";

 

while ($row = $result->fetch_object())

{

echo "<tr>";

echo "<td>" . $row->id . "</td>";

echo "<td>" . $row->firstname . "</td>";

echo "<td>" . $row->lastname . "</td>";

echo "<td><a href='records.php?id" . $row->id . "'>Edit</a></td>";

echo "<td><a href='delete.php?id" . $row->id . "'>Delete</a></td>";

 

echo "</tr>";

}

echo "</table>";

}

}

else

{

echo "Error: " . $mysqli->error;

}

 

$mysqli->close();

 

 

?>

 

 

Thanks, Herbert

Link to comment
Share on other sites

One thing to double check that I found when looking over your code... Take a look at these lines in your view.php:

 

echo "<td><a href='records.php?id" . $row->id . "'>Edit</a></td>";
echo "<td><a href='delete.php?id" . $row->id . "'>Delete</a></td>";

I'm pretty sure it is supposed to be:

 

echo "<td><a href='records.php?id=" . $row->id . "'>Edit</a></td>";
echo "<td><a href='delete.php?id=" . $row->id . "'>Delete</a></td>";

(note the "=").

Link to comment
Share on other sites

Noting happens no errors or anything. I'll change the code on view.php.

 

Thanks Ben

 

One thing to double check that I found when looking over your code... Take a look at these lines in your view.php:

 

echo "<td><a href='records.php?id" . $row->id . "'>Edit</a></td>";
echo "<td><a href='delete.php?id" . $row->id . "'>Delete</a></td>";

I'm pretty sure it is supposed to be:

 

echo "<td><a href='records.php?id=" . $row->id . "'>Edit</a></td>";
echo "<td><a href='delete.php?id=" . $row->id . "'>Delete</a></td>";

(note the "=").

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...