-2

I've got a table in the database mshop2, named userdb. It has columns:

  • ID
  • name
  • username
  • password
  • email

My js file with all the functions stored in them is named mshop2.script, where me deleteUser function is also stored, which will jump to a file named deleteUser.php

The php file where I want the code to run on is admin.php and It's got these written in it:

<font color="white">Deletion - ID</font>
<input class="test1" type="text" id="txUserid" value="">
<input class="test2" name="Submit" type="submit" value="" id="submit" 
       onClick='deleteUser();txUserid.value="";'/>

the 'input class' words are just going to match an image folder according to my css file to show an image that I want the code to run on when I click it so don't mind it. I have included these lines in the head of my .php file:

<script src='jquery.js'></script>
<script src='mshop2.script.js'></script>

I've written this on my mshop2.script.js file:

function deleteUser(){
    var userid = $('#txUserid').val();
    var finalData = {
        uid: userid
    };

    $.post('deleteUser.php', finalData, function(resp){
        if(resp == 'success'){
            alert('User successfully deleted.');
            getUserList();
        }
    }); 
}

This is the contents of my deleteUser.php file:

<?php
    include 'config.php';

    $id = mysql_real_escape_string($_POST["userid"]);
    $q = "DELETE FROM userdb WHERE ID = '$uid'";

    if(!mysql_query($q, $con)){
        die('Error: ' . mysql_error());
        echo mysql_error();
    }else{
        echo 'success';
    }

    mysql_close($con);
?>

Nothing is happening and if I enter '1' into the text box or any number from 1 to 5 the matching line in my table in database with the same ID that I entered won't get deleted.

What is wrong with my code?

4

3 回答 3

1

Change this :-

$q = "DELETE FROM userdb WHERE ID = '$uid'";

to

$q = "DELETE FROM userdb WHERE ID = '$id'";

You are catching the value in variable $id and using $uid in the sql statement that's the reason its not working.

于 2013-06-21T08:53:00.470 回答
1

in jq in are posting uid.

change post to

$uid = mysql_real_escape_string($_POST["uid"]);
于 2013-06-21T09:06:29.020 回答
0

You had a jumbled variable and that confuse u.

In your script code, you had passed uid with a value of userid And you access a wrong variable in your deleteUser.php

Instead of mysql_real_escape_string($_POST['userid']);

change to mysql_real_escape_string($_POST['uid']);

And in your query, just change the variable $uid to $id

Just use a a similar variable to what had you passed in your script so that it will not be hard on your part to see common mistakes

EDIT::: Try this Remove the onclick event on your html code

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

 <script>
     $(document).ready(function() { 
          $(".test2").click(function() { 
                 var id = $("#tsUserid").val();
                 $.post("deletUser.php", { id: id }, function(data) { 
                     alert(data);
                 });
          });
     });
 </script>

Your php will be your deleteUser.php and just

change mysql_real_escape_string($_POST['userid']);

to mysql_real_escape_string($_POST['id']);

and your query to $q = "DELETE FROM userdb WHERE ID = '$id'";

Hope it helps.

于 2013-06-21T09:05:10.017 回答