Pregunta

I have made a js function to remove data from a database using ajax. its very basic but i want to get it to work before i add anything else the function is below:

function removeAd(ad_id) {

    $.post('remove.php', {id: ad_id});

};

I use php to build a page and create links that use this function:

<ul id='categoryorder'>

<?php while ($item = mysqli_fetch_array($r)) { ?>

<li id="advert_<?php echo $item['display_id'] ?>"><div class="<?php echo $item['ad_type']?>"><a href="javascript:removeAd('<?php echo $item['display_id'] ?>')" >Remove</a></div></li>

<?php } ?> 

</ul>

it builds the page fine and creates remove links within divs that have a href of javascript:removeAd(id) with the id being the id that relates to that div in a database eg:

<li id="advert_5"><div class="banner"><a href="javascript:removeAd('5')" >Remove</a></div></li>

finally this should send to the remove.php script which actually removes from the database the entry with that ID

<?php

require ('Mysql_Connect.php');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $q = "DELETE FROM st_display WHERE display_id = {$_POST['id']}";
    $r = @mysqli_query ($dbc, $q);

}

?>

I am very new to this so im quite prepared to be told that im making a stupid mistake or that this will never work! but any help will be much appreciated.

¿Fue útil?

Solución

Your query is perfect for a good SQL injection.
But that's not the importance here, if you try to do echo $q does it return something ?
Do a console.log(ad_id); before your $.post to see if you send the data as well.

Otros consejos

you are missing ; at the end of the echo statements,

<li id="advert_<?php echo $item['display_id']; ?>"><div class="<?php echo $item['ad_type']; ?>"><a href="javascript:removeAd('<?php echo $item['display_id']; ?>')" >Remove</a></div></li>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top