Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 单击“从数据库中删除选定记录”_Php_Javascript_Mysql_Sql - Fatal编程技术网

Php 单击“从数据库中删除选定记录”

Php 单击“从数据库中删除选定记录”,php,javascript,mysql,sql,Php,Javascript,Mysql,Sql,我想在单击时从数据库中删除一条记录,这是我尝试过的: delete.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

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

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

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

对不起,我的英语不好
<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扩展名和函数创建文件,您创建的是它的php函数,而不是javascript函数,所以请编写此代码

<a href="delete.php?id=58">Delete</a>
对不起,我英语不好 首先,使用php扩展名和函数创建文件,您创建的是它的php函数,而不是javascript函数,所以请编写此代码

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

和javascript(使用jQuery)

您的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>
id为10的行:
id为20的行:
id为30的行:
id为40的行:
德尔
PHP

和javascript(使用jQuery)

您的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>
id为10的行:
id为20的行:
id为30的行:
id为40的行:
德尔

如果您只想从链接中删除,则不必关心页面重新加载等。。。那么这里已经给出了答案:

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

您没有提供足够的代码,因此无法在执行操作后帮助您更新显示,但基本情况可能如下所示(未经测试)

delete.php

$select="delete from tbl_category where id='".$_GET['del_id']."'";
$query=mysql_query($select) or die($select);
<?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>
<?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);
?>

$(文档).ready(函数(){
$('.delete row')。单击(函数(){
$.post('delete.php?mode=delete',{row\u id:$(this.data('row\u id'))}).done(函数(数据){
//重新加载表格/数据显示
});
});
});
删除
,请改用PDO或MySQLi。回到基础知识,学习PHP和javascript如何相互作用,因为在您的知识中有一些不正确的地方

编辑(附加OCD):


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

如果您只想从链接中删除,则不必关心页面重新加载等。。。那么这里已经给出了答案:

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

您没有提供足够的代码,因此无法在执行操作后帮助您更新显示,但基本情况可能如下所示(未经测试)

delete.php

$select="delete from tbl_category where id='".$_GET['del_id']."'";
$query=mysql_query($select) or die($select);
<?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>
<?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);
?>

$(文档).ready(函数(){
$('.delete row')。单击(函数(){
$.post('delete.php?mode=delete',{row\u id:$(this.data('row\u id'))}).done(函数(数据){
//重新加载表格/数据显示
});
});
});
删除
,请改用PDO或MySQLi。回到基础知识,学习PHP和javascript如何相互作用,因为在您的知识中有一些不正确的地方

编辑(附加OCD):


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

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

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

函数getXMLHTTP(){//fuction返回xml http对象
var xmlhttp=false;
试一试{
xmlhttp=新的XMLHttpRequest();
}
第(e)款{
试试{
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
捕获(e){
试一试{
xmlhttp=新的ActiveXObject(“Msxml2.xmlhttp”);
}
渔获物(e1){
xmlhttp=false;
}
}
}       
返回xmlhttp;
}
函数getData(strURL){
var req=getXMLHTTP();
如果(请求){
req.onreadystatechange=函数(){
如果(req.readyState==4){
//只有在“OK”的情况下
如果(请求状态==200){
document.getElementById('divResult').innerHTML=req.responseText;
}否则{
警报(“使用XMLHTTP:\n“+req.statusText时出现问题”);
}
}               
}           
请求打开(“获取”,strURL,true);
请求发送(空);
}
}

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

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

函数getXMLHTTP(){//fuction返回xml http对象
var xmlhttp=false;
试一试{
xmlhttp=新的XMLHttpRequest();
}
第(e)款{
试试{
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
捕获(e){
试一试{
xmlhttp=新的ActiveXObject(“Msxml2.xmlhttp”);
}
渔获物(e1){
xmlhttp=false;
}
}
}       
返回xmlhttp;
}
函数getData(strURL){
var req=getXMLHTTP();
如果(请求){
req.onreadystatechange=函数(){
如果(req.readyState==4){
//只有在“OK”的情况下
如果(请求状态==200){
document.getElementById('divResult').innerHTML=req.responseText;
}否则{
警报(“使用XMLHTTP:\n“+req.statusText时出现问题”);
}
}               
}           
请求打开(“获取”,strURL,true);
请求发送(空);
}
}
以下是我的解决方案:

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

类别名称
品名
产品说明
产品形象
产品实际价值