dongziya9863 2013-06-21 08:52
浏览 17
已采纳

尝试使用php文件从数据库中删除表[关闭]

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?

  • 写回答

3条回答 默认 最新

  • duanjiebian6712 2013-06-21 09:05
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 制裁名单20240508芯片厂商
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致