Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deleting items from database PHP SQL

Tags:

php

mysql

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

}
?> 
like image 444
sammy Avatar asked Sep 06 '26 10:09

sammy


1 Answers

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.

like image 105
Andrius Avatar answered Sep 08 '26 00:09

Andrius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!