Jump to content

Correct actions performed, but wrong output


sergiu1103

Recommended Posts

Hi all,

 

I'm extremely new to php and mysql, so I am kindly asking all you gurus to have patience with me :) I'm sure the following code is not very friendly, but I am stuck on the following: I need to delete entries from a mysql DB, and display the updated results.

 

I can successfully delete the entries I want, but once I press the delete button, I only get an empty table in the webpage. I need to perform the query again in order to get an updated overview.

 

Can somebody bring a light where I am going wrong? Thanks

 

P.S. I attached the code in a txt file, so that you don't have to scroll down to view it. Also, there may be lines wider than one screen, and this would make it hard to follow

 

 

 

 

[attachment deleted by admin]

This whole section could be replaced....

 

if($_POST['delete']){
        $checkbox=$_POST['checkbox'];
        for($i=0;$i<count($_POST['checkbox']);$i++){

                $del_id = $checkbox[$i];
                $sql2 = "DELETE FROM $tbl_name WHERE id='$del_id'";
                $result2 = mysql_query($sql2);
        }
}

 

with....

 

if ($_POST['delete']) {
  $ids = implode(',',$_POST['checkbox']);
  $sql = "DELETE FROM $tbl_name WHERE id IN($ids)";
  if (mysql_query($sql)) {
    header("Location: http://yourdomain.com/nameofpage.php");
    exit();
  }
}

 

You'll need to replace the location with the location of this page. This code will aslo need to go at the very top, prior to any output.

It still displays an empty table after I press delete. However, if I place this code in the beginning (right after I declare the variables), it does not even delete the entry. Should it work if I place it before I declare what 'delete' means?

this is the code. I am considering changing it a little. I would like to do the delete in another page, and just display a message if it's deleted. This way I do not have to worry about page refreshing. The user will do the extra effort of doing one more click.

 

However, I am having problems also. I am not able to pass the array that's containing the checkboxes which hold the IDs that must be deleted. It is always passing an empty serialized value.

 

<?php

include ('config.php');

// include ('query.php');

 

$host=$mysql_host;

$username=$mysql_user;

$password=$mysql_pwd;

$db_name=$mysql_db;

$tbl_name="data";

$show_study=$_POST['study'];

$select=$_POST['ans'];

$interval=$_POST['timeinterval'];

$from=$_POST['from'];

$to=$_POST['to'];

$j=0;

 

//  if (isset($interval) && ($interval == 'Yes')) {

    if ($interval == "Yes") {

        if (! isset($from, $to)) {

                echo "You need to select a date interval";

        } else {

                if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $from, $regs1)) {

                        $fromyear = substr($from, 0, 4);

//                      echo "$year\n";

                        $frommonth = substr($from, 5, 2);

//                      echo "$month\n";

                        $fromday = substr($from, -2, 2);

//                      echo "$day\n";

                        if(($frommonth<=0)OR($frommonth>12)) { die ("Enter a Valid Month - From field"); }

                        elseif(($fromday<=0)OR($fromday>31)) { die ("Enter a Valid Day - From field"); }

                        elseif(($fromyear<=2007)OR($fromyear>2009)) { die ("Enter a Valid Year - From field"); }

                }

 

                if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $to, $regs2)) {

                        $toyear = substr($to, 0, 4);

//                      echo "$year\n";

                        $tomonth = substr($to, 5, 2);

//                      echo "$month\n";

                        $today = substr($to, -2, 2);

//                      echo "$day\n";

                        if(($tomonth<=0)OR($tomonth>12)) { die ("Enter a Valid Month - To field"); }

                        elseif(($today<=0)OR($today>31)) { die ("Enter a Valid Day - To field"); }

                        elseif(($toyear<=2007)OR($toyear>2009)) { die ("Enter a Valid Year - To field"); }

                }

        }

 

          if ($select == "all") {

                $sql="SELECT * FROM $tbl_name where date >= $fromyear$frommonth$fromday and date <= $toyear$tomonth$today order by date";

          }

          else {

                $sql="SELECT * FROM $tbl_name where study like \"$show_study\" and date >= $fromyear$frommonth$fromday and date <= $toyear$tomonth$today order by date";

          }

 

  } else {

          if ($select == "all") {

              $sql="SELECT * FROM $tbl_name order by date";

          }

          else {

              $sql="SELECT * FROM $tbl_name where study like \"$show_study\" order by date";

          }

  }

 

 

// Connect to server and select databse.

mysql_connect($host, $username, $password)or die("cannot connect");

mysql_select_db($db_name)or die("cannot select DB");

 

$result=mysql_query($sql);

 

$count=mysql_num_rows($result);

 

?>

<table width="700" align="center" border="0" cellspacing="1" cellpadding="0" bgcolor="#CCCCCC">

 

<tr>

        <td><form name="form1" method="post" action="delete_data.php" bgcolor="#FFFFFF">

        <td colspan="8" align="center" bgcolor="#FFFFFF"><h1>Query results</h1> </td>

</tr>

 

 

<tr>

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

        <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>

        <td align="center" bgcolor="#FFFFFF"><strong>Study</strong></td>

        <td align="center" bgcolor="#FFFFFF"><strong>Category</strong></td>

        <td align="center" bgcolor="#FFFFFF"><strong>Monitor</strong></td>

        <td align="center" bgcolor="#FFFFFF"><strong>Document</strong></td>

        <td align="center" bgcolor="#FFFFFF"><strong>Date</strong></td>

        <td align="center" bgcolor="#FFFFFF"><strong>Amount</strong></td>

</tr>

 

<?php

 

$sum="0";

while($rows=mysql_fetch_array($result, MYSQL_ASSOC)){

$sum = ($sum + $rows['amount']);

$j++;

?>

<tr>

        <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>

        <td bgcolor="#FFFFFF"><?php echo $j; ?></td>

        <td bgcolor="#FFFFFF"><?php echo $rows['study']; ?></td>

        <td bgcolor="#FFFFFF"><?php echo $rows['category']; ?></td>

        <td bgcolor="#FFFFFF"><?php echo $rows['monitor']; ?></td>

        <td bgcolor="#FFFFFF"><?php echo $rows['document']; ?></td>

        <td bgcolor="#FFFFFF"><?php echo $rows['date']; ?></td>

        <td bgcolor="#FFFFFF"><?php echo $rows['amount']; ?></td>

</tr>

 

<?php

}

?>

<tr>

        <td colspan="8" align="right" bgcolor="#FFFFFF"><strong>Total: <?php echo $sum; ?></strong></td>

</tr>

 

</table>

<br>

<table width="180" align="center" border="0" cellspacing="1" cellpadding="0" bgcolor="#CCCCCC">

<tr>

        <td align="right" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>

        <td align="right" bgcolor="#FFFFFF"><input name="refresh" onClick="history.go(0)" type="button" value="Refresh"></td>

        <td align="right" bgcolor="#FFFFFF"><input type="button" value="Print" onclick="javascript:window.print();">

</tr>

 

<?php

 

if($_POST['delete']){

$checkbox=$_POST['checkbox'];

$array = serialize($checkbox);

 

echo $array;

}

 

// Check if delete button active, start this

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

//      $checkbox=$_POST['checkbox'];

//      for($i=0;$i<count($_POST['checkbox']);$i++){

 

//              $del_id = $checkbox[$i];

//              $sql2 = "DELETE FROM $tbl_name WHERE id='$del_id'";

//              $result2 = mysql_query($sql2);

//      }

// }

 

?>

</form>

</table>

 

<?php

mysql_close();

?>

 

 

delete_data.php

<?php

include ('config.php');

$check = $_GET['array'];

$tbl_name = "data";

 

$checks = unserialize($check);

echo $checks;

 

$connect = mysql_connect($mysql_host, $mysql_user, $mysql_pwd);

 

mysql_select_db($mysql_db);

 

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

 

                $del_id = $checks[$i];

                $sql2 = "DELETE FROM $tbl_name WHERE id='$del_id'";

                echo "$sql2";

//                if ($result2 = mysql_query($sql2)) {

//                      print "<font size=12 color=green>Data succesfully deleted!</font>";

//              } else {

//                      print "<font size=12 color=red>Error deleting data!</font>";

//              }

        }

 

 

mysql_close();

 

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.