0

I am trying to delete some rows when users check them with check button. I have created a button called delete, but I have a problem. It said to me that variable delete that I use here

// Check if delete button active, start this  if($delete){ for($i=0;$i<$count;$i++){ $del_id = $checkbox[$i]; $sql = "DELETE FROM Aktivitet WHERE Id_Akt='$del_id'"; $result = mysql_query($sql); } is not defined... But why because I have declared it as the name of Delete button...here I have created the Delete button;

  print("<tr>");
    print("<td><input name='delete' type='submit' id='delete' value='Delete'></td>");
    print("</tr>");

Please can you help me? What can I do? My php file is below:

<?php
$con = mysql_connect('127.0.0.1','root','');
if (!$con)
  {
  die('Lidhja me databazen nuk mund te kryhet' .mysql_error(). ' </body></html>');
  }
if(!mysql_select_db("Axhenda",$con))
die('Nuk mund te hapet databaza Axhenda'.mysql_error(). '</body></html>');


$result = mysql_query("SELECT * FROM Aktiviteti where Data= '$_POST[dataoutput]'");

mysql_close($con);

    ?>

<div class="title"> Aktivitetet per daten <?php print ("$_POST[dataoutput]"); ?></div>

<table> 

<?php

  print("<th >");

   print("<form name='form1' method='post' action=''>");
  print("<td>Emertimi takimit</td>");
   print("<td>Pershkrimi takimit</td>");
    print("<td>Oraa takimit</td>");
    print("</th >");

while($rows=mysql_fetch_row($result))
{

if (!empty($rows))

{

     print("<tr >");


print("<td align='center' bgcolor='#FFFFFF'><input
 name='checkbox[]' type='checkbox' id='checkbox[]' value='$rows[0]'></td>");
print("<td bgcolor='#FFFFFF'>$rows[2]</td>");
print("<td bgcolor='#FFFFFF'>$rows[3]</td>");

print("<td bgcolor='#FFFFFF'>$rows[5]</td>");

print("</tr>");



    }
 else 
 echo "ska aktivitet";

}
print("<tr>");
print("<td><input name='delete' type='submit' id='delete' value='Delete'></td>");
print("</tr>");


?>
<?php

// Check if delete button active, start this 
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM Aktivitet WHERE Id_Akt='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php 
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
}
}  

print("</form>");

?>

</table>

I modified the function like below, but it still dont delete anything:

if(isset($_POST['delete'])){

    $checkbox = $_POST['checkbox'];
$count = count($checkbox);

    for($i = 0; $i < $count; $i++) {
        $id = (int) $checkbox[$i]; 

        if ($id > 0) { // and check if it's bigger then 0
            mysql_query("DELETE FROM Aktiviteti WHERE Id_Akt= $id");
        }
    }
}

please help me..

user3272713
  • 167
  • 2
  • 5
  • 15
  • It is not dublicate. I have seen it but in that case the variable is not the name of a form button. IN my case the delete variable that is undefined, is the name of button and I dont know what to do...If it is possible, can you help me? – user3272713 Feb 09 '14 at 17:30
  • @user3272713 - and where do you define `$delete`? – andrewsi Feb 09 '14 at 17:33
  • That what I am asking you? I dont know where and how to define it...Can you help me? – user3272713 Feb 09 '14 at 17:34
  • @user3272713: Why do you need that variable? From the comment in your code, I'm guessing you're trying to `Check if delete button active` - but I don't know what that means. Could you explain? – Amal Murali Feb 09 '14 at 17:36
  • 1
    I'm going to make a guess that instead of `if ($delete)` you want something along the lines of `if (isset($_POST['delete']))`. – andrewsi Feb 09 '14 at 17:36
  • @andrewsi yes you are right. The error does not appear any more...but my button does not delete anything....even when I check some rows it just redirect me again to that page but not delete anything...Can you help me, please? – user3272713 Feb 09 '14 at 17:42
  • If ID is numeric, then try `WHERE Id_Akt=$del_id";` @user3272713 – Funk Forty Niner Feb 09 '14 at 17:45
  • @user3272713 - I'll give you a hint. `$delete` isn't set anywhere; what do you think `$checkbox` will do? – andrewsi Feb 09 '14 at 17:47
  • but what can I do? delete is the name of button and checkbox the name of checkbox input....how to set $delete? – user3272713 Feb 09 '14 at 17:50
  • @user3272713 - I get that checkbox is the name of the button. But it doesn't automatically get turned into a variable in PHP that you can access. When you changed `$delete` into `$_POST['delete']`, the error went away. What do you think you need to change `$checkbox` into? – andrewsi Feb 09 '14 at 17:53
  • I modified the function like i just posted above, but it still does not function...please can you help me – user3272713 Feb 09 '14 at 18:01
  • @user3272713 - add some debugging. Is your query being run? If it is, echo it out to see what the values are. Try `var_dump($_POST)` to see exactly what your form is passing. Check the return value from your query to see if it succeeds; if it doesn't check and see what's in `mysql_error()` – andrewsi Feb 09 '14 at 18:10
  • when i try var_dump($_POST) my query output this value :array(1) { ["dataoutput"]=> string(9) "2014-2-11" }. Also it output the table with the activities for that date. the problem is that when I check some dates, it does not delete anything when I press button delete.. When I press it it is like refreshing the page. what canI do? – user3272713 Feb 09 '14 at 18:17
  • When it comes to questions like these, I always refer people to [**This Link on SO**](http://stackoverflow.com/q/14475096/) and base yourself on the Q&A inside that page. It's helped me and others before. @user3272713 – Funk Forty Niner Feb 09 '14 at 18:19
  • I have seen it...And I am seing it now again..But still I havent solve my problem? Have you caught any error in my code? please help me...I have spend hours stuck here... – user3272713 Feb 09 '14 at 18:25
  • I suggest you use the Q&A from the link I provided to you already above, and then modify the variables from there, it will work. Don't spend anymore time in trying to fix your existing code. I don't have time to setup a DB just for this and to debug your code, that's your job. I don't mind helping with obvious errors, but this is too much. @user3272713 - Continue using `var_dump();` for all your variables, you will find what's set and what is not. – Funk Forty Niner Feb 09 '14 at 18:32
  • @ Fred can I make you one more question please.. I make it like it is solved in the page you gave to me and it still does not delete anything. But I want to ask you what does this mean: "$result = mysqli_query($sql);"...checked the error logs...found out that mysqli_query() expects at least two parameters.. Because the guy at the other page said that have solved problem with this? What athe parameter should I put in mysqli_query($sql)??? – user3272713 Feb 09 '14 at 19:15
  • This `$result = mysqli_query($sql);` probably needs to have the DB connection added to it. I.e.: `$result = mysqli_query($con,$sql);` plus your code uses `mysql_*` and `mysqli_*` you can't mix both. So you may have to use `$result = mysql_query($sql,$con);` to match the rest of `mysql_` – Funk Forty Niner Feb 09 '14 at 20:54
  • The link I gave you, the person uses `mysqli_*` functions in there, so you will need to change your DB connection method from `mysql_*` to `mysqli_*`. Notice the added `i` ? – Funk Forty Niner Feb 09 '14 at 20:57
  • Also this `$_POST[dataoutput]` you have no form element called `dataoutput` so I don't know how I could piece everything together. I'm totally confused. – Funk Forty Niner Feb 09 '14 at 21:00
  • @Fred I made it in onother way. I make a new php file, and when I press the delete button I direct it to that php file and it deletes the checked row. But can I ask you for smth? How can I redirect the new php file to go back to the old one, not to display a blank page whe the wquery is executed? – user3272713 Feb 09 '14 at 21:40
  • What do you mean by *"How can I redirect the new php file to go back to the old one"*? Oh, and I was just about to put something as an answer to help you out, something that I use myself that works. @user3272713 – Funk Forty Niner Feb 09 '14 at 21:42
  • Oh thanks... you can put it now, for me.If it is not a problem to you...What I mean is that when I press delete button it execute the query, and it sends me to a blank page. But I want that when I press the button, not to go to a blank page but to be at the same page where user can see the table of activities...So it is smth like I press delete button, it execute the query, and it direct me to the page where the calendar and activitis are...I dont know if I am explaining it well,, – user3272713 Feb 09 '14 at 21:48
  • You're welcome. The one I will put up, will redirect you back to the deletion page using `echo "";` so there will be no blank page. @user3272713 yet you will need to make some modifications. – Funk Forty Niner Feb 09 '14 at 21:53
  • I would need to see your updated code to see why it's giving you a blank page. @user3272713 - and I posted my answer below. – Funk Forty Niner Feb 09 '14 at 21:58
  • I am seeing your answer now...Thanks – user3272713 Feb 09 '14 at 22:04

2 Answers2

0

Try this:

if(isset($_POST['delete'])){
    foreach($_POST['checkbox'] as $del_id){
        $sql = "DELETE FROM Aktivitet WHERE Id_Akt='$del_id'"; 
        $result = mysql_query($sql);
    }
}
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
hous
  • 174
  • 4
  • 11
0

Tested on my own server. You will need to modify it for your own columns as well as other elements that need to be translated in your language.

Change settings for the following:

DEFINE ('DB_HOST', 'xxx');
DEFINE ('DB_USER', 'xxx');
DEFINE ('DB_PASSWORD', 'xxx');  
DEFINE ('DB_NAME', 'xxx');

And: (Note: the # is just a reference character. You can change it to what you like.)

<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Link ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Link Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Link Email</strong></td>

And change: (The rows you wish to show up. There are two of these. One is to show the rows, while the other is used for deletion.)

<td bgcolor="#FFFFFF"><?php echo $row['id']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['name']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['email']; ?></td>

And change: (delete.php is this file to redirect to after deleting)

echo "<meta http-equiv=\"refresh\" content=\"3;URL=delete.php\">";

And change: ( your_table ) and the id depending on which column you want to ORDER BY

$query = "select * from your_table ORDER BY id ASC";

And change:

$sql = "DELETE FROM your_table WHERE link_id='$del_id'";

And change: all instances of link_id being the id (column name) associated with the deletion.

Full script: (delete.php)

<html>
<head>
<title></title>
</head>
<body>
<h2>Choose and delete selected items.</h2>

<?php
DEFINE ('DB_HOST', 'xxx');
DEFINE ('DB_USER', 'xxx');
DEFINE ('DB_PASSWORD', 'xxx');
DEFINE ('DB_NAME', 'xxx');

$dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) 
OR die("could not connect");

$query = "select * from your_table ORDER BY id ASC";

$result = mysqli_query($dbc,$query)
or die('Error querying database');

$count=mysqli_num_rows($result);
?>

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="3" bgcolor="#FFFFFF"><strong>Delete multiple items</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Link ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Link Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Link Email</strong></td>
</tr>

<?php
while ($row=mysqli_fetch_array($result)) {
?>

<tr>
<td align="center" bgcolor="#FFFFFF">

<input name="checkbox[]" type="checkbox" value="<?php echo $row['link_id']; ?>">

</td>
<td bgcolor="#FFFFFF"><?php echo $row['id']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['name']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['email']; ?></td>
</tr>

<?php
}
?>

<tr>
<td colspan="4" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" value="Delete"></td>
</tr>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
 <tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="3" bgcolor="#FFFFFF"><strong>Delete multiple items</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Link ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Link Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Link Email</strong></td>
 </tr>

<?php
while ($row=mysqli_fetch_array($result)) {
?>

<tr>
<td align="center" bgcolor="#FFFFFF">

<input name="checkbox[]" type="checkbox" value="<?php echo $row['link_id']; ?>">

</td>
<td bgcolor="#FFFFFF"><?php echo $row['id']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['name']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['email']; ?></td>
</tr>

<?php
}
?>

<tr>
<td colspan="4" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" value="Delete"></td>
</tr>

<?php
if(isset($_POST['delete']))
{
    $checkbox = $_POST['checkbox'];

for($i=0;$i<count($checkbox);$i++){

$del_id = $checkbox[$i];
$sql = "DELETE FROM your_table WHERE link_id='$del_id'";
$result = mysqli_query($sql);
}
// if successful redirect to delete.php 
if($result){
 echo "<meta http-equiv=\"refresh\" content=\"3;URL=delete.php\">";
// echo "Got results.";
}
 }

mysqli_close($dbc);

?>

</table>
</form>
</td>
</tr>
</table>
</body>
</html>
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141