i'm trying to delete a item from my database. deleting is successful but there is two problems:
Notice: Undefined index: delete in C:\wamp\www\source\admin_delete_user.php on line 46
line 46: if($_POST['delete'])
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']) . "')";
}
?>
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.