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.

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

报告相同问题?