1

I wrote the code to delete MySQL table rows. But when I click on delete icon, nothing happens. Could someone please tell me what's left in my code?

<?php
include_once 'include/DatabaseConnector.php';
$query1="SELECT * FROM MyTable;";
$result1=DatabaseConnector::ExecuteQueryArray($query1);
?>

<script type="text/javascript">
function deleteRow(tableName,colName,id){
    $.ajax({
           type: "POST",
           url: "delete.php",
           data: "tableName=tableName&colName=colName&id=id",
           success: function(msg){
             alert( "Row has been updated: " + msg );
           }
    });
}
</script>

<table id="newspaper-b" border="0" cellspacing="2" cellpadding="2" width = "100%">
<thead>
<tr>
    <th scope="col">Opr</th>
    <th scope="col">Flt Num</th>
    <th scope="col">From</th>
    <th scope="col"></th>
</tr>
</thead>
<tbody>
<?php foreach ($result1 as $row):?>
<tr>
<td><?php echo $row['airlineName'];?></td>
<td><?php echo $row['flightNum'];?></td>                        <td><?php echo $row['from'];?></td>
<td>
  <div title='Delete' onclick='deleteRow(<?php echo 'flightschedule','flightNum',$row['flightNum']; ?>)'>
<img src='images/delete.png' alt='Delete' />
</div>              
</td>
</tr>
<?php endforeach;?>
</tbody>

delete.php

<?php
    /* Database connection */
    include_once 'include/DatabaseConnector.php';
    if(isset($_POST['tableName']) && isset($_POST['colName']) && isset($_POST['id'])){
        $tableName = $_POST['tableName'];
        $colName = $_POST['colName'];
        $id = $_POST['id'];
        $sql = 'DELETE FROM '.$tableName.' WHERE '.$colName.' ="'.$id.'"';
        mysql_query($sql);
    } else { 
        echo '0'; 
    }
?>
4
  • 1
    Where does Ajax.Request come from? Did you make it, or is it part of a library? Also, if you're not careful, code like this could allow a user to delete any data from any table in the database. Commented May 24, 2012 at 12:12
  • 1
    in your ajax request u did not mention cal type'post' or get... and in ur script try $_GET to get url params Commented May 24, 2012 at 12:13
  • 2
    just an observation, use some kind of restriction to execute the "delete.php" because someone can make deletes into your database, calling that file from a external form, give it the parameter and that's it.. Regards Commented May 24, 2012 at 12:32
  • Please look at my updated post. The only trouble I have now is the following line of code: data: "tableName=tableName&colName=colName&id=id". How to pass parameters of the function "deleteRow.php"? Commented May 24, 2012 at 13:03

3 Answers 3

1
  • have you checked your PHP log to see if there is an error?
  • what is Ajax.Request ? if you are using the prototype library, where is it included in your HTML code?
  • finally, are you sure your PHP code is called? (check using for instance the Web Developper Tools in Chrome browser, "Requests" tab)
Sign up to request clarification or add additional context in comments.

Comments

0

You have an error on this line

 <div title='Delete' onclick='deleteRow(<?php echo 'flightschedule','flightNum',$row['flightNum']; ?>)'>

this will print

 <div title='Delete' onclick='deleteRow(flightscheduleflightNumid)'>

You have to change it to

  <div title='Delete' onclick='deleteRow("flightschedule","flightNum",<?php $row['flightNum']; ?>)'>

to work.

Best wishes

Update: For the above code to work also add

<script src="js/jquery.js" type="text/javascript" ></script>

or load it from the internet

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.js"></script>  

to include Jquery at the header of html. I have tested its ok now.

3 Comments

Thank you. I changed this line, but still nothing works. Maybe there is something more which is wrong in my code?
It worked for you after the update? I can read the values normally.
Have you tested the code? P.s. You can make a good use of a javascript debugger when you are writing javascript. I use bugzilla it's a nice one plugin to firefox.
0

you send not your data to the delete.php file, because in the attribute dated you seize them badly. Here is this code I have just tested him(it) and that seems to work.

data: "tableName="+tableName+"&colName="+colName+"&id="+id+"",

function deleteRow(tableName,colName,id){
    $.ajax({
           type: "POST",
           url: "delete.php",
           data: "tableName="+tableName+"&colName="+colName+"&id="+id+"",
           success: function(msg){
             alert( "Row has been updated: " + msg );
           }
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.