Advertisement
gitlez

YA: Checked/Unchecked Database 20130616122614AAYSDP0

Jun 16th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.65 KB | None | 0 0
  1. Answer for Yahoo Answers Question
  2. http://answers.yahoo.com/question/index?qid=20130616122614AAYSDP0
  3.  
  4. Form Snippet
  5.  
  6. <?php
  7.  
  8. // You really should at the least be using
  9. // MySQLi functions. Better still; PDO.
  10.  
  11. // I am going to include clauses in the future
  12. // SQL, due to the category_id, to make it easier
  13. // to edit, I am also going to assign it a
  14. // variable. Ensure that it stays the same on
  15. // the processing page.
  16.  
  17. $category_id = '1';
  18.  
  19. $r1 = mysql_query("SELECT id,product_name,highlighted FROM products WHERE category_id='{$category_id}'");
  20.  
  21. if( !$r1 ){
  22.     echo 'Internal Server Error. Query Failed.';
  23.     echo '<br' . ' />'; // Yahoo Doesn't like br elements
  24.     echo mysql_error();
  25. }else if( mysql_num_rows( $r1 ) ){
  26.     echo 'No Products in the Database matching Conditions ("category_id=' . $category_id . '")';
  27. }else{
  28.     echo '<input type="hidden" name="category_id" value="' . $category_id . '" />'; // One way to pass it, if need be
  29.     while($q1 = mysql_fetch_assoc($r1)){
  30.         $checked = ((int)$q1['highlighted'] === 1)? ' checked="checked"' : '';
  31.         echo $q1['product_name'] . ' - <input type="checkbox" name="ids[]" value="' . $q1['id'] . '"' . $checked . '><b' . 'r>';
  32.         // Again, Yahoo Doesn't like br tags
  33.     }
  34. }    
  35.    
  36.    
  37. ?>
  38.  
  39.  
  40. Processing Script
  41.  
  42.  
  43. <?php
  44.  
  45. if( isset( $_POST['submit'] ) ){
  46.     // Ensure that $category_id is the same as the previous form
  47.     $category_id = mysql_real_escape_string( trim( $_POST['category_id'] ) );
  48.    
  49.     if( isset($_POST['ids']) && isset($_POST['ids'][0])){
  50.         $ids = $_POST['ids'];
  51.         $ids = mysql_real_escape_string( implode( ',', $ids ) );
  52.         // In MySQLi, you could combine the two statements and make it slightly more efficient
  53.         $stmt1 = "UPDATE products SET highlighted='1' WHERE category_id='{$category_id}' AND id IN({$ids});";
  54.         $stmt2 = "UPDATE products SET highlighted='0' WHERE category_id='{$category_id}' AND id NOT IN({$ids});";
  55.        
  56.         if( !mysql_query( $stmt1 )){
  57.             echo 'Statement 1 Failed because: ' . mysql_error();
  58.         }else if( !mysql_query( $stmt2 ) ){
  59.             echo 'Statement 2 Failed because: ' . mysql_error();
  60.         }else{
  61.             echo 'Success! <a href="index.php?function=highlighted">Go Back</a>';
  62.         }
  63.     }else{
  64.         // No Ids Checked (~ all ids unchecked)
  65.         $stmt = "UPDATE products SET highlighted='0' WHERE category_id='{$category_id}'";
  66.         if( !mysql_query( $stmt ) ){
  67.             echo 'Statement Failed because: ' . mysql_error();
  68.         }else{
  69.             echo 'Success! <a href="index.php?function=highlighted">Go Back</a>';
  70.         }
  71.     }
  72. }
  73. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement