-2

I have created an ajax based php-mysql application which shows data from mysql table using while loop.

    $ibb = mysql_query("select * from bill where tableno='{$tno}' ORDER BY ID ASC");

while($ibff = mysql_fetch_assoc($ibb))
{
$iidd=$ibff['ID'];
$itemid=$ibff['itemid'];
$amt=$ibff['amount'];
$qty=$ibff['qty'];


echo'<tr>
<td>'.$itemid.'</td>
<td>'.$amt.'</td>
<td>'.$qty.'</td>
<td>'.$total.'</td>
<td><button name= "'.$iidd.'" id="remove" class="btn btn-danger btn-sm" onclick="this.disabled=true;">X</button></td>


</tr>';
}

AJAX Code is as below :-

<script type="text/javascript">
jQuery('#remove').click(function(data) {
    var pid = jQuery(this).attr('name');
        var y = jQuery(this).attr('name');

    $.ajax({
                url: "delitem.php?item="+pid+"&data="+y,
                type: 'GET',
                success: function(s){
                    var $container = $("#content2");
        $container.load("bd.php");
                },
                error: function(e){
                    alert('Error Processing your Request!!');
                }
            });

});
</script>

delitem.php code :-

$idd=$_GET['item'];

$ab = mysql_query("select * from bill where ID='{$idd}'");
$abf=mysql_fetch_assoc($ab);

$qty = $abf['qty'];
$name = $abf['itemid'];
$d = date('d/m/Y');

$remove= mysql_query("delete from reports where name='{$name}' and date='{$d}' limit $qty");
$delete = mysql_query("delete from bill where id='{$idd}'");

Code is working fine only if i start deleting from the very first record. It always deletes the first entry displayed on the page. Where am I doing wrong?

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Akshay
  • 3
  • 2
  • 4
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). Where is the PHP with your deletion query? – Jay Blanchard Apr 29 '15 at 18:36
  • Thanks sir..Ya i will... could you please suggest the solution for now? – Akshay Apr 29 '15 at 18:37
  • 3
    delete what, something from your DB? I don't see `delete from...` in your code. The problem could be in there; the code you haven't posted. Or, is that irrelevant? – Funk Forty Niner Apr 29 '15 at 18:39
  • I have updated my question and added delitem.php code as well. kindly respond. – Akshay Apr 29 '15 at 18:48
  • 1
    `name= "'.$iidd.'"` and you're using `id='{$idd}'` no match here. if that works, let me know. – Funk Forty Niner Apr 29 '15 at 18:48
  • so this ^ is not an issue here? – Funk Forty Niner Apr 29 '15 at 18:52
  • It is just badly organized code @Fred-ii- – Jay Blanchard Apr 29 '15 at 18:53
  • glitch was only i was using ID instead of CLASS... By the way thanks to each and everyone... – Akshay Apr 29 '15 at 18:53
  • ah ok. well, am glad you found your solution, *cheers* – Funk Forty Niner Apr 29 '15 at 18:54

1 Answers1

0

# or ID is to be used for a single item where . or class is for multiple items, so you markup is technically incorrect there, which could have a negative impact.

Also, as mentioned, you should be using Prepared Statements in PDO for all DB interactions.

tjl
  • 68
  • 1
  • 6
  • Thanks a lot.. You got the exact glitch in the code... Many thanks... Solved my problem.... :) – Akshay Apr 29 '15 at 18:51