3

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']) . "')";

}
?> 
5
  • A Notice is not an Error. Commented Nov 9, 2015 at 10:23
  • @arkascha: a notice do not has good view when user is working in site Commented Nov 9, 2015 at 10:24
  • You mean you visualize such stuff in your web view? Switch that off in your php settings! If at all, that should only be configured for debugging and development, but never for production usage! Take a look at the display_errors settings inside your php.ini configuration file. Commented Nov 9, 2015 at 10:26
  • @arkascha A well-written website should not produce any notices. Commented Nov 9, 2015 at 10:28
  • @syck That undoubtedly is true. But it does not change the fact that error display has to be switched off on a production site, or does it? :-) Commented Nov 9, 2015 at 10:29

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

1.what is using 2. are you sure there is not any way to deleting first?
Just add the isset() part to the if statement and use the AND operator (add the isset() before you do !empty()). Well, you can only delete first if the deletion code is above the parsing code, right? That does make sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.