Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #11 (permalink)  
Old April 22nd, 2004, 02:41 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Quote:
quote:
Uhhh... am I the only person that noticed that you're overwriting $result within the while() loop, and that $result is the iteration control variable?
lol, I guess so :) Yeah, that's bad.

Regards,
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
Reply With Quote
  #12 (permalink)  
Old April 28th, 2004, 09:59 AM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for reponding, I appreciate the assistance.
To richard, I have two questions in regards to the code that you were provided....


echo "<input type='checkbox' name='id[]' value='$the_id'>$the_id</input><br />";

     if (isset($_POST['remove']))
     {
          foreach ($_POST['id'] as $the_id)
          {
               if (!mysql_query("DELETE FROM table WHERE id = '$the_id'"))
               {
                    echo mysql_error();
               }
          }
     }


First would be your variable $the_id( are you just referring to the $id fieled in the table that I am using... And the code that i used works but there are two problems... 1- after querying the db and getting the result i receive this error
Warning: Invalid argument supplied for foreach() in \Inetpub\wwwroot\php_mysql\mysql_query2.php on line 43
also when I refresh my browser the error doesn't come up until i choose the record that I want to delete and click the button, which results in the brower refreshing and after the second click or hitting the refesh button the record that i chose to delete will be deleted.

2- When I choose the record I want deleted I have to click the delete button twice before the record is removed....


Here is the code, and I assumed that $the_id is the id fieldname of in my table which $id...

global $PHP_SELF;
$link_id = db_connect($default_dbname);
$result = mysql_query( "SELECT id, name, email_address FROM faculty_mailing_list GROUP BY (id) ASC");

?>

<form action="<? echo $PHP_SELF ?>" Method='POST'>
  <table width="500" border="1" cellspacing="0" cellpadding="0">
    <tr valign="top">
      <td>ID</td>
      <td>Name</td>
      <td>E-mail</td>
      <td>Remove</td>
    </tr>
    <? while ($row = mysql_fetch_array($result)) { ?>
    <tr>
      <td>
        <? $id = $row['id']; echo $id; ?>
      </td>
      <td>
        <? $name = $row['name']; echo $name; ?>
      </td>
      <td>
        <? $email = $row['email_address']; echo $email; ?>
      </td>
      <td> <? echo "<input type='checkbox' name='id[]' value='$id'>$id</input>" ?>

        <?

        if (isset($_POST['remove']))
             {
              foreach ($_POST['id'] as $id)
                  {
                   if (!mysql_query("DELETE FROM faculty_mailing_list WHERE id = '$id'"))
                       {
                    echo error_message(sql_error());
                       }

                  }
             }

        ?>

        <?// echo "<A HREF=\"$PHP_SELF?action=delete_record&id=$id\" onclick=\"return confirm('Are you sure you want to delete record $id');\"> DELETE </A>" ?>
      </td>
      <? }?>
    </tr>
  </table>
  <input type='submit' name='remove' value='Delete' />



thanks for the assistance...
Reply With Quote
  #13 (permalink)  
Old April 28th, 2004, 10:14 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Code:
<?php
    $link_id = db_connect($default_dbname);

    if (isset($_POST['remove']) && isset($_POST['id']) && is_array($_POST['id']))
    {
        foreach ($_POST['id'] as $id)
        {
            if (!mysql_query("DELETE FROM faculty_mailing_list WHERE id = '$id'"))
            {
                echo error_message(sql_error());
            }
        }
    }
    else
    {
        echo "Error: the id is not an array!<br />\n";
    }

    $result  = mysql_query("SELECT id, name, email_address FROM faculty_mailing_list GROUP BY (id) ASC");

    echo "<form action='{$_SERVER['PHP_SELF']}' method='POST'>\n".
         "  <table width='500' border='1' cellspacing='0' cellpadding='0'>\n".
         "      <tr valign='top'>\n".
         "          <td>ID</td>\n".
         "          <td>Name</td>\n".
         "          <td>E-mail</td>\n".
         "          <td>Remove</td>\n".
         "          <td></td>\n".
         "      </tr>\n";

    while ($row = mysql_fetch_array($result))
    {
        echo "      <tr>\n".
             "          <td>".$row['id'].             "</td>\n".
             "          <td>".$row['name'].           "</td>\n".
             "          <td>".$row['email_address'].  "</td>\n".
             "          <td><input type='checkbox' name='id[]' value='{$row['id']}'>{$row['id']}</input></td>\n".
             "          <td><a href='{$_SERVER['PHP_SELF']}?action=delete_record&id={$row['id']}' onclick=\"return confirm('Are you sure you want to delete record $id');\"> DELETE </a></td>\n".
             "      </tr>\n";
    }

    echo "      <tr>\n".
         "          <td colspan='5' align='right'><input type='submit' name='remove' value='Delete' onclick=\"return confirm('Are you sure you want to delete the selected records?');\" /></td>\n".
         "      </tr>\n".
         "  </table>\n".
         "</form>";
?>
I haven't got alot of time to comment, so study the above rewrite of your code and I'll offer some comments later. Please try and make your code more readable: http://p2p.wrox.com/topic.asp?TOPIC_ID=11967


Regards,
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
Reply With Quote
  #14 (permalink)  
Old April 29th, 2004, 07:59 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Hi Dungey,
OK, after rewriting this mess, it came down to you were calling on the delete query inside of the while loop, this should be a loop all its own, and would be better placed before querying your database so you don't view records that you've just deleted. Having nice, clean code like this helps to see logic errors like that. Second in this rewrite it is clear that the <DELETE> link doesn't do anything, the functionality for that should be placed somewhere closeby to the delete loop that runs for the checkboxes.

I also had one typo in my post, I left out a <td> in the table headers, I posted an edit of my previous reply.

hth!

Regards,
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
Reply With Quote
  #15 (permalink)  
Old April 30th, 2004, 08:51 AM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Richard,

The code definitely makes more sense now that the code is easly read... I appreciate the assistance and the lesson learned from this
posting. Thanks to all...

--Dungey
Reply With Quote
  #16 (permalink)  
Old May 16th, 2007, 03:55 PM
Registered User
 
Join Date: May 2007
Location: , , Colombia.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you all for solving (and asking in the first place, of course) about this issue.

I'm just learning PHP, and I still couldn't get how could I inform PHP interpreter that something in (X)HTML was selected, given that I thought about the checkbox for deleting or editing data.

Anyways, thank you for that easy-to-read solution.

I'm actually new to programming in general, and what a coincidence, I follow most of PEAR coding standards. I feel like I rock.:)

Thank you again.

PS: I use the one-true brace for functions, classes, etc. ;)
Reply With Quote
  #17 (permalink)  
Old April 10th, 2009, 03:39 PM
Registered User
 
Join Date: Apr 2007
Location: Beavercreek, Ohio, USA.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Similar problem as OP

Hello to all. I am having a similar issue with some script that I am trying to run. Keep in mind I am very new to php/mysql programming. I am at the before crawl stage. I have a database that has id column which is my primary key and is auto-incremented and first name, last name, and email address. I am able to display the db contents with checkboxes, but when I comes to deleting a record, the script runs w/o errors and displays the customer(s) removed statement and no records are deleted. I have at one point inserted a command to show what is going on and get query empty messages. Please help me as I have not found any useful answers anywhere else. I have tried probably a couple of hundred iterations of this code using ideas from the web....nothing is working.

Thanks

Code:
<body>
  <img src="blankface.jpg" width="161" height="350" alt="" style="float:right" />
  <img name="elvislogo" src="elvislogo.gif" width="229" height="32" border="0" alt="Make Me Elvis"  />
  <p>Please select the email addresses to delete from the email list and click Remove.</p>
  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<?php


    //echo 'Testing database connection<br>';

  $dbc = mysql_connect('localhost', 'myusename, 'mypass')
    or die('Error connecting to MySQL server.');
    mysql_select_db('mydb', $dbc);

    //echo 'Database select successful<br>';






   // Delete the customer rows (only if the form has been submitted)

     if(isset($_POST['submit']))
  {
  foreach($_POST['todelete'] as $delete_id) {
        
        $query="DELETE FROM email_list WHERE id=$delete_id";
            mysql_query($query, $dbc) or die ('Error in query: ' .mysql_errno() . mysql_error());
        
    
    
          }
        
    
    }


    // Display the customer rows with checkboxes for deleting
    $query="SELECT * FROM email_list";
        
    $result = mysql_query($query, $dbc);
    while ($row = mysql_fetch_array($result)){
    echo '<input type="checkbox" value"' . $row['id'] .'"name="todelete[]" />';
    echo $row['first_name'];
    echo ' '.$row['last_name'];
    echo ' '.$row['email'];
    echo '<br />';


   }








mysql_close($dbc);
?>

    <input type="submit" name="submit" value="Remove" />
  </form>
</body>
Reply With Quote
  #18 (permalink)  
Old April 11th, 2009, 12:24 PM
Registered User
 
Join Date: Apr 2007
Location: Beavercreek, Ohio, USA.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem solved

Just to let everyone know, the problem is solved. I had looked at this code for weeks and didn't see the problem until this morning. In the 2 lines below you will see what has been causing all of my grief colored in red.

This is the original
Code:
echo '<input type="checkbox" value"' . $row['id'] .'"name="todelete[]" />';
This is the fixed

Code:
echo '<input type="checkbox" value="' . $row['id'] .'"name="todelete[]" />';
I feel like an idiot.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to delete file System.IO.Delete error maricar C# 13 March 14th, 2014 06:50 AM
Commit.PHP and Delete.PHP in WROX's book GiFos Beginning PHP 0 January 30th, 2007 09:51 PM
How to delete the added button in outlook haiying Access VBA 0 June 10th, 2005 07:10 AM
how to delete a row when click delete hyperlink naveenkumarg1 Pro JSP 1 August 16th, 2004 01:29 AM



All times are GMT -4. The time now is 07:23 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.