Jump to content

[SOLVED] Editing my database from the web...


derekbelcher

Recommended Posts

I am trying to build a simple content management system and am not real sure where to go.  I have the code (below) that displays my table contents.  Works great!  What I want to do is make that "delete" word clickable and execute a delete function of that row's data.

 

<?php

 

$result = mysql_query("SELECT * FROM admin");

 

echo "<table border='1'>

<tr>

<th>First Name</th>

<th>Last Name</th>

<th>Email Address</th>

<th>DELETE USER</th>

</tr>";

 

while($row = mysql_fetch_array($result))

{

echo "<tr>";

echo "<td>" . $row['firstname'] . "</td>";

echo "<td>" . $row['lastname'] . "</td>";

echo "<td>" . $row['email'] . "</td>";

echo "<td align='center'>" . delete;

echo "</tr>";

}

echo "</table>";

 

mysql_close($con);

 

?>

Link to comment
https://forums.phpfreaks.com/topic/153531-solved-editing-my-database-from-the-web/
Share on other sites

echo '<td align="center"><a href="delete.php?id='.$row['id'].'">delete</a>';

 

That's presuming your primary key is called "id"

 

Then inside delete.php use $_GET['id'] to get the parameter you're passing it.

 

Exactly what I was going to say but he beat me to it

alright.  That's awesome and I think right where I need to be.  Now, here comes my "newbieness"  what should my delete.php have in it.  I am just starting to learn some of the different functions, but haven't used the GET one yet.

 

Thanks for your help.

$_GET will get values from the URL

$_POST gets data from a form with method of post

$_REQUEST will get from loads of sources including the above two and cookies

 

Once you have your ID retrieved from $_GET the next step depends if you want to verify with the user that it's the right record to delete.

 

Either way later on you'll need to pass it to a DELETE query like this:

$rowid=intval($_GET['id']);
if (mysql_query("DELETE FROM admin WHERE id=".$rowid." LIMIT 1")) {
  echo 'Deleted';
} else {
  echo 'Not deleted';
}

 

We add "LIMIT 1" as a precaution to make sure we only delete 1 record.

We also use intval() on $_GET because we're expecting a number. Anything other than a number will return 0.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.