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
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.

 

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • 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.