从数据库onClick中删除选定的记录

时间:2022-06-11 22:13:01

I want to delete a record from database on click and this is what I have tried:

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

delete.js

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

Button:

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

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

Please help me I cant find out how to do this. Just tell me if you need any more information.

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

6 个解决方案

#1


1  

If you just want to delete from a link, don't care about page reload etc... then it's already answered here:

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

php delete mysql row from link

php从链接中删除mysql行

But it sounds like you want to click a button, and it'll delete the row without navigating you away from the page you're on, in which case you'll want to look into using some form of ajax.

但听起来你想点击一个按钮,它会删除你的行,而不会导航你离开你所在的页面,在这种情况下你会想要使用某种形式的ajax。

You've not provided enough of your code so can't help you with updating the display after you've performed your action, but the basis would probably look something like this (untested)

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

delete.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, use PDO or MySQLi instead. Go back to basics and learn how PHP and javascript can interact with each other, as there's something not right there in your knowledge.

我会强烈建议不要使用mysql_函数,而是使用PDO或MySQLi。回到基础知识,了解PHP和javascript如何相互交互,因为在你的知识中有些东西不正确。

Edit (additional OCD):

编辑(额外强迫症):

You should also be considering other things, can anyone delete any row? If only certain people should have permission, you should verify that the currently logged in user should be allowed to delete that particular row prior to deleting it.

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

#2


0  

<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>

delete.php

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

#3


0  

Sorry for my bad english First of all create file with php extensions and function you create its php's function not javascript, so code this one.

对不起我的坏英语首先创建文件与PHP扩展和功能你创建其PHP的功能而不是JavaScript,所以编码这一个。

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

Create delete.php file and code this one

创建delete.php文件并对此进行编码

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

#4


0  

PHP

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

and in javascript (using jQuery)

并在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();
    });
}

and your html would be

你的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


0  

This is what I did to delete data without leaving page or reload page:

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

In 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>

In 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":
}
?>

You can also use this for dynamic row for multiple "Delete" button.

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

#6


0  

Here is my solution:

这是我的解决方案:

 <?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>

?>

In delete.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);
?>

#1


1  

If you just want to delete from a link, don't care about page reload etc... then it's already answered here:

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

php delete mysql row from link

php从链接中删除mysql行

But it sounds like you want to click a button, and it'll delete the row without navigating you away from the page you're on, in which case you'll want to look into using some form of ajax.

但听起来你想点击一个按钮,它会删除你的行,而不会导航你离开你所在的页面,在这种情况下你会想要使用某种形式的ajax。

You've not provided enough of your code so can't help you with updating the display after you've performed your action, but the basis would probably look something like this (untested)

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

delete.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, use PDO or MySQLi instead. Go back to basics and learn how PHP and javascript can interact with each other, as there's something not right there in your knowledge.

我会强烈建议不要使用mysql_函数,而是使用PDO或MySQLi。回到基础知识,了解PHP和javascript如何相互交互,因为在你的知识中有些东西不正确。

Edit (additional OCD):

编辑(额外强迫症):

You should also be considering other things, can anyone delete any row? If only certain people should have permission, you should verify that the currently logged in user should be allowed to delete that particular row prior to deleting it.

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

#2


0  

<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>

delete.php

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

#3


0  

Sorry for my bad english First of all create file with php extensions and function you create its php's function not javascript, so code this one.

对不起我的坏英语首先创建文件与PHP扩展和功能你创建其PHP的功能而不是JavaScript,所以编码这一个。

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

Create delete.php file and code this one

创建delete.php文件并对此进行编码

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

#4


0  

PHP

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

and in javascript (using jQuery)

并在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();
    });
}

and your html would be

你的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


0  

This is what I did to delete data without leaving page or reload page:

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

In 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>

In 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":
}
?>

You can also use this for dynamic row for multiple "Delete" button.

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

#6


0  

Here is my solution:

这是我的解决方案:

 <?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>

?>

In delete.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);
?>