Jump to content

how to delete image from database?


lukelee

Recommended Posts

here is the code:

<?php
require_once('db.php');
$query = mysql_query("SELECT * FROM image WHERE thumb = '1'");
while($row = mysql_fetch_array($query)) {
?>
<ul>
<li><img src="upload/<?PHP echo $row['imagedata']; ?>" width="150" height="150" border="1" /></li>
<li><?PHP echo $row['address']; ?></li>
<li>delete</li>
</ul>
<?php } ?>

the images are house images, so house must have an address,price, description <?PHP echo $row['address']; ?> shows the address, 1 address can contain more than 1 image. I use thumb = '1' image as a thumbnail.

when i click delete, all images and data on this address will be deleted. does anyone know how to do this?

Link to comment
https://forums.phpfreaks.com/topic/132810-how-to-delete-image-from-database/
Share on other sites

The syntax of the query is:

 

DELETE FROM yourtable WHERE somefield = 'somevalue'

 

You'll obviously need to use something to uniquely identify the row in the table that you want to delete and pass that in the URL to a script that performs the delete query.

The syntax of the query is:

 

DELETE FROM yourtable WHERE somefield = 'somevalue'

 

You'll obviously need to use something to uniquely identify the row in the table that you want to delete and pass that in the URL to a script that performs the delete query.

 

I will make another php file to do the process, but I dont know how to pass the address value.

You can pass values in the URL like this:

 

http://www.yourwebsite.com/somepage.php?foo=bar

 

Or multiple items like this:

 

http://www.yourwebsite.com/somepage.php?id=4&foo=bar

 

To get the values in somepage.php, you'll use the $_GET superglobal:

 

$foo = $_GET['foo'];
$id = $_GET['id'];

 

If you're using this data to query a database, you'll also need to sanitize your data to prevent SQL injections. You can do this by using the mysql_real_escape_string function.

here is my code:

<?php
require_once('db.php');
$query = mysql_query("SELECT * FROM image WHERE thumb = '1'");
while($row = mysql_fetch_array($query)) {
?>
<ul>
<li><img src="upload/<?PHP echo $row['imagedata']; ?>" width="150" height="150" border="1" /></li>
<li><?PHP echo $row['address']; ?></li>
<li><a href="delete_house.php?address=<?php echo $row['address']; ?>">delete</a></li>
</ul>
<?php } ?>

<?php
$address=$_GET['address'];
require_once('db.php');
$query = mysql_query("DELETE FROM image WHERE address = $address");
?>

 

I dont know whats wrong with my codes, the data is not deleted while i clicked the delete.

Did you check to make sure the address is passed in the URL? You should see it in the address bar.

 

And how about some debugging on that query:

 

$sql = "DELETE FROM image WHERE address = $address";
$query = mysql_query($sql) or trigger_error(mysql_error().'<br />Query was: '.$sql,E_USER_ERROR);

 

 

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.