【问题标题】:Delete selected records from database onClick从数据库中删除选定的记录 onClick
【发布时间】:2013-08-31 09:29:33
【问题描述】:

我想在点击时从数据库中删除一条记录,这是我尝试过的:

删除.js

function oki(){
    mysql_query("DELETE FROM topics WHERE id='58'"); 
}

按钮:

<script type="text/javascript" src="delete.js"></script>
<?php
    include_once("connect.php");
?>

<button onClick="oki();">Del</button>

请帮帮我,我不知道该怎么做。 如果您需要更多信息,请告诉我。

【问题讨论】:

标签: php javascript mysql sql


【解决方案1】:

如果您只想从链接中删除,不关心页面重新加载等......那么这里已经回答了:

php delete mysql row from link

但听起来您想单击一个按钮,它会删除该行而不会将您从您所在的页面导航,在这种情况下,您需要考虑使用某种形式的 ajax。

您没有提供足够的代码,因此无法帮助您在执行操作后更新显示,但基础可能看起来像这样(未经测试)

删除.php

<?php

include_once("connect.php");

if ($_GET['mode'] == 'delete') {
  $row_id = (int)$_POST['row_id'];
  mysql_query("DELETE FROM topics WHERE id=" . $row_id); 
}

?>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1    /jquery.min.js"></script>
<script type="text/javascript">
  $(document).ready(function(){
    $('.delete-row').click(function() {
      $.post('delete.php?mode=delete', { row_id: $(this).data('row_id')}).done(function(data) {
        // Reload your table/data display
      });
    });
  });
</script>

<button class="delete-row" data-row_id="58">Delete</button>

I would HEAVILY advise against using mysql_ functions,请改用 PDO 或 MySQLi。回到基础,了解 PHP 和 javascript 如何相互交互,因为在您的知识范围内有些东西是不正确的。

编辑(附加强迫症):

您还应该考虑其他事情,任何人都可以删除任何行吗?如果只有某些人应该有权限,您应该验证当前登录的用户应该被允许在删除该特定行之前删除它。

【讨论】:

    【解决方案2】:
    <input type="button" onclick="deleteme(<?php echo $row['id']; ?>)"> // fetch from database
    <script language="javascript">
    function deleteme(delid) 
    {
            window.location.href='delete.php?del_id='+delid+'';
            return true;
    }
    </script>
    

    删除.php

    $select="delete from tbl_category where id='".$_GET['del_id']."'";
    $query=mysql_query($select) or die($select);
    

    【讨论】:

    • 至少,做 (int)$_GET['del_id']
    【解决方案3】:

    对不起我的英语不好 首先创建带有 php 扩展名和函数的文件,你创建它的 php 函数而不是 javascript,所以编写这个。

    <a href="delete.php?id=58">Delete</a>
    

    创建 delete.php 文件并编写代码

    if(isset($_GET['id'])) {
       @mysql_query("DELETE FROM topics WHERE id = '".$_GET['id']."'");
       header("location: index.php");
       exit();
    }
    

    【讨论】:

    • 你应该使用 mysql_real_escape_string 这样人们就不会在查询中注入 SQL。这将删除所有条目delete.php?id=58 or 1=1
    • 应避免通过 GET 字符串进行删除操作 - 它指示做某事而不得到某事。无论如何,想象一下蜘蛛在您的网站上点击每个删除链接都会发疯......
    【解决方案4】:

    PHP

    $ids = array_map('intval', json_decode($_GET['id']));
    mysql_query("DELETE FROM topics WHERE id in (" . implode(',', $ids) . ")");
    

    在javascript中(使用jQuery)

    function oki() {
        var checked = $('.row:checked');
        var ids = checked.map(function() { return $(this).val(); });
        $.get('delete.php', {id: JSON.stringify(ids)}, function(response) {
            checked.remove();
        });
    }
    

    你的 html 会是

    Row with id 10: <input type="checkbox" class="row" value="10"/>
    Row with id 20: <input type="checkbox" class="row" value="20"/>
    Row with id 30: <input type="checkbox" class="row" value="30"/>
    Row with id 40: <input type="checkbox" class="row" value="40"/>
    <button onClick="oki();">Del</button>
    

    【讨论】:

      【解决方案5】:

      这是我在不离开页面或重新加载页面的情况下删除数据所做的:

      在 some.php 中:

      <?php
      include"../connect.php";
      
      // for simple security need change to improve
      Session_start();
      $n=rand(1,99);
      $uid=md5($n);
      $_SESSION['uid']=$uid
      // end
      ?>
      <script>
      function getXMLHTTP() { //fuction to return the xml http object
          var xmlhttp=false;  
          try{
              xmlhttp=new XMLHttpRequest();
          }
          catch(e)    {       
              try{            
                  xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
              }
              catch(e){
                  try{
                  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                  }
                  catch(e1){
                      xmlhttp=false;
                  }
              }
          }       
          return xmlhttp;
      }
      
      function getData(strURL) {      
          var req = getXMLHTTP();
          if (req) {
              req.onreadystatechange = function() {
                  if (req.readyState == 4) {
                      // only if "OK"
                      if (req.status == 200) {                        
                  document.getElementById('divResult').innerHTML=req.responseText;                        
                      } else {
                          alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                      }
                  }               
              }           
              req.open("GET", strURL, true);
              req.send(null);
              }
          }
      </script>
      <input name="button" type="button" value="Clear Data" onClick="getData('deldata.php?id=58&uid=<?=$uid?>')">
      <div id="divResult"></div>
      

      在deldata.php中:

      <?php
      Session_start();
      include"../connect.php";
      $idd=$_REQUEST['id'];
      $uid=$_REQUEST['uid'];
      $sc=$_SESSION['uid'];
      
      //check for simple security
      if($sc==$uid){
         mysqli_query("DELETE FROM topics WHERE id='$idd'");
      echo "Data deleted":
      }
      ?>
      

      您也可以将其用于多个“删除”按钮的动态行。

      【讨论】:

        【解决方案6】:

        这是我的解决方案:

         <?php
        $query="select * from product limit ".$offset. " , " .$perpage;
        $result=mysql_query($query);
        
        ?>
        <center>
        <table border="2">
        <tr>
        <th>Category Name</th>
        <th>Product Name</th>
        <th>Product Description</th>
        <th>Product Image</th>
        <th>Product Actual image</th>
        <th>Product Discount</th>
        <th>Product Selling Price</th>
        <th>Update/Delete</th>
        </tr>
        <?php while ($row=mysql_fetch_array($result)){?>
        <tr>
        <td><?php echo $row['pro_catagory'];?></td>
        <td><?php echo $row['pro_name'];?></td>
        <td><?php echo $row['pro_des'];?></td>
        <td><img src="Img/<?php echo $row['pro_image'];?>" width="100" height="100" ></td>
        <td><?php echo $row['pro_actual'];?></td>
        <td><?php echo $row['pro_dis'];?></td>
        <td><?php echo $row['pro_price'];?></td>
        
        
        <td<a href="delete.php?pid=<?php echo $row['pro_id'];?>">Delete</a></td>
        

        ?>

        在删除.php中

        <?php
        $con=mysql_connect('localhost','root','');
        if(!$con)
        {
        die('could not connect' .mysql_error());
        }
        mysql_select_db("jaswinder", $con);
        $Query="delete from product where pro_id=".$_REQUEST['pid'];
        $result=mysql_query($Query);
        ?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-03-22
          • 1970-01-01
          • 1970-01-01
          • 2019-07-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多