PHP Function delete()

Hey there

This is my first thread on sitepoint, so don’t be too rough :stuck_out_tongue:

The thing is, i’m trying to figure out how i can delete a post from a shoutbox. I’ve manangede to put the submitters IP into my db, with the post and id.

This is how i’ve done (dosen’t work thou):

$ipadress = users ip
$ip = Ipadress storred in db
$id = auto_increment number on posts

if($ipadress == $ip){
?>
<input type=“button” onclick=“delete(<?php $id ?>)” value=“Delete” />
<?php

Login to DB and select DB

function delete($num){
mysql_query(“DELETE FROM shoutbox WHERE id=‘$num’”);
}

i’m no exerpt at this point, but according to my knowledge this should work.
Any suggestions on how to fix this, or any alternatives?

Thanks for taking your time reading this, a reply would help a lot

the delete call is going to call a JavaScript function, not PHP. In order to do delete the message upon clicking the input you would either need to submit a form and call the PHP function than, pass a variable through a query string or have the delete JavaScript function do a AJAX request to call the server side script to delete the item.

You made that sound really easy oddz. So you are suggesting that instead of using a onclick, i give it a name, create a if(isset($_POST[‘name’])){ delete the post } ?

Doesn’t seem to work either .

if($ipaddress == $ip){
	
		$connect = mysql_connect($host,$username,$password) or die('
		Unable to connect to the database server at this time.');

		mysql_select_db($database,$connect) or die('
		Unable to connect to the database at this time.');	
		
				if(isset($_POST['delete'])){
		mysql_query("DELETE FROM shoutbox WHERE id='.$id.'");
	}

	?&gt;
	&lt;input type="button" name="delete" value="Delete" /&gt;
	&lt;?php

Again, I’m no pro so please correct me if I’m mistaken

What errors are you getting?

The problem you run into with inputs is that the value needs to be the label. So the solution is to use the id as the key.


<?php
if($ipaddress == $ip){

$connect = mysql_connect($host,$username,$password) or die('
Unable to connect to the database server at this time.');

mysql_select_db($database,$connect) or die('
Unable to connect to the database at this time.');

if(isset($_POST['delete']) && !empty($_POST['delete'])){
mysql_query('DELETE FROM shoutbox WHERE id='.mysql_real_escape_string(array_pop($_POST['delete'])));
}

?>
<input type="button" name="delete[<?php echo $id;  ?>]" value="Delete" />