duanluan3651 2015-11-09 10:20
浏览 68
已采纳

从数据库PHP SQL中删除项目

i'm trying to delete a item from my database. deleting is successful but there is two problems:

  1. showing this error when i've run this code for first time:

Notice: Undefined index: delete in C:\wamp\www\source\admin_delete_user.php on line 46

line 46: if($_POST['delete'])

  1. when i've delete a item from database, nothing appears at first and i need to refresh to see the results.

code:

<form name="form2" method="post" action="" > 
  <?php

   $db_host = 'localhost';
   $db_name= 'site';
   $db_table= 'tablesite';
   $db_user = 'root';
   $db_pass = '';




$con = mysql_connect($db_host,$db_user,$db_pass) or die("خطا در اتصال به پايگاه داده");

mysql_query("SET NAMES 'utf8'", $con);
mysql_query("SET CHARACTER SET 'utf8'", $con);
mysql_query("SET character_set_connection = 'utf8'", $con);

$selected=mysql_select_db($db_name, $con) or die("خطا در انتخاب پايگاه داده");
mysql_query("SET CHARACTER SET  utf8");
$dbresult=mysql_query("SELECT * FROM  tablesite",$con);
echo "کاربری که قصد حذفش را دارید انتخاب نمایید: ";
echo '<br/>';

echo '<select name="delete">';

while($amch=mysql_fetch_assoc($dbresult))
{
   echo '<option value="'.$amch['id_user'].'">'.$amch['username'].'</option>';
}
echo '</select>'; ?> <br/>
 <input name="submit2" type="submit" value="حذف" />

</form>

<?php
if($_POST['delete'])
{
$db_host = 'localhost';
$db_name= 'site';
$db_table= 'tablesite';
$db_user = 'root';
$db_pass = '';


$con = mysql_connect($db_host,$db_user,$db_pass) or die("خطا در اتصال به پايگاه داده");

mysql_query("SET NAMES 'utf8'", $con);
mysql_query("SET CHARACTER SET 'utf8'", $con);
mysql_query("SET character_set_connection = 'utf8'", $con);

$selected=mysql_select_db($db_name, $con) or die("خطا در انتخاب پايگاه داده");
 $ins = "DELETE FROM tablesite 
         where id_user='" . mysql_escape_string($_POST['delete']) . "'";
         $dbresult=mysql_query($ins,$con);
echo "('" . mysql_escape_string($_POST['delete']) . "')";

}
?> 
  • 写回答

1条回答 默认 最新

  • du1913 2015-11-09 10:24
    关注

    To fix the first problem, you'll need to add (using &&) isset($_POST['delete']) to the if statement. That'll do the check if the variable even exists.

    In order to fix the second one... You most likely need to move the whole deletion part above the data parsing. Then it will be deleted first and only then parsed, not the other way around.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器