Deleting rows in a database using select lists.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yc022
    New Member
    • Mar 2007
    • 6

    Deleting rows in a database using select lists.

    Hi all, this is my first time using this so i'm not really sure how it works. please bear with me.

    i am trying to delete a row in a database using a select list. First of all i have a query to produce what i would like to be held in the select (dropdown) list. i then create the select list with a delete button. I then try to create the query to delete the selected option from the list from the database, but nothing happens and an error message appears. This error message however is not one of the error messages returned from one of my queries. Please Help!!!! what am i doing wrong?

    <?php

    $namequery = "SELECT DISTINCT projectnumber
    FROM projects";

    $result = mysql_query($na mequery) or die('Error, query failed');
    $count = mysql_num_rows( $result);
    if ($count > 0){

    echo "<form action=\"<$self >\" method=\"post\" >";
    echo "<select name=\"item\">" ;

    while($result_r ow = mysql_fetch_arr ay($result)){
    echo "<option name=\"item2\"> ";
    echo ($result_row[0]);
    echo "</option>";
    }
    echo "</select>";
    echo "<input type=\"submit\" name=\"Delete\" id=\"Delete\" value=\"Delete\ " />";
    if(isset($_POST['Delete'])){
    foreach($_POST[item] as $del){
    $query_delete = ("DELETE FROM projects WHERE projectnumber ='".$del."'") ;
    $result = mysql_query($qu ery_delete) or die('Error, query failed');
    }
    echo "<meta http-equiv=\"refresh \" content=\"0\"; URL=\"AdminProj ects.php\">";
    echo "Row deleted!";
    }
    echo "</form>";
    }
    else{
    echo "There are no project numbers in the system";
    }
    ?>
  • gauravgmbhr
    New Member
    • Feb 2007
    • 107

    #2
    Originally posted by yc022
    Hi all, this is my first time using this so i'm not really sure how it works. please bear with me.

    i am trying to delete a row in a database using a select list. First of all i have a query to produce what i would like to be held in the select (dropdown) list. i then create the select list with a delete button. I then try to create the query to delete the selected option from the list from the database, but nothing happens and an error message appears. This error message however is not one of the error messages returned from one of my queries. Please Help!!!! what am i doing wrong?

    <?php

    $namequery = "SELECT DISTINCT projectnumber
    FROM projects";

    $result = mysql_query($na mequery) or die('Error, query failed');
    $count = mysql_num_rows( $result);
    if ($count > 0){

    echo "<form action=\"<$self >\" method=\"post\" >";
    echo "<select name=\"item\">" ;

    while($result_r ow = mysql_fetch_arr ay($result)){
    echo "<option name=\"item2\"> ";
    echo ($result_row[0]);
    echo "</option>";
    }
    echo "</select>";
    echo "<input type=\"submit\" name=\"Delete\" id=\"Delete\" value=\"Delete\ " />";
    if(isset($_POST['Delete'])){
    foreach($_POST[item] as $del){
    $query_delete = ("DELETE FROM projects WHERE projectnumber ='".$del."'") ;
    $result = mysql_query($qu ery_delete) or die('Error, query failed');
    }
    echo "<meta http-equiv=\"refresh \" content=\"0\"; URL=\"AdminProj ects.php\">";
    echo "Row deleted!";
    }
    echo "</form>";
    }
    else{
    echo "There are no project numbers in the system";
    }
    ?>
    WHAT is the error that u r getting

    i thing u r applying foreach on a non-aaray variable
    which will not work
    either u create multiple selection list box instead of single selection drop down and send multiple values or dont use foreach

    but still lemme know the error message

    if u need any help in creatin multiple selection list box, and how to send all multiple values to PHP then do write to me

    Comment

    • ak1dnar
      Recognized Expert Top Contributor
      • Jan 2007
      • 1584

      #3
      Hi Here is Solution.Just use this coding by changing the SQL Script.
      Please Note that here i am using a Table called product.
      Columns:
      p_id, p_name

      In the Select box I am displaying p_name and as the value of the each and every List menu item I am Passing the p_id for deleting purpose.

      That means the from the PHP delete section it will fetch this value Attribute.and pass it to the DELETE query. for this you can use your any other column. :) but for better performance, this primary key in my table is a Good idea.

      And also no need to use double quotes for PHP echo. use single quotes. Then No need to escape them.

      change this line also
      [PHP]echo "<meta http-equiv=\"refresh \" content=\"0\"; URL=\"untitled3 .php\">";[/PHP]

      [PHP]<?php
      require 'dbcon.php';
      $namequery = "SELECT p_name,p_id
      FROM products";

      $result = mysql_query($na mequery) or die('Error, query failed');
      $count = mysql_num_rows( $result);
      if ($count > 0)
      {
      echo '<form action="'.$PHP_ SELF.'" method="POST">' ;
      echo '<select name="item">';
      while($result_r ow = mysql_fetch_arr ay($result))
      {
      echo '<option value="'.$resul t_row[1].'" >';
      echo ($result_row[0]);
      echo '</option>';
      }
      echo '</select>';
      echo '<input type="submit" name="Delete" id="Delete" value="Delete" />';
      echo '</form>';
      }
      else
      {
      echo "There are no project numbers in the system";
      }

      if($_POST['Delete'])
      {
      $del = $_POST['item'];
      $query_delete = ("DELETE FROM products WHERE p_id ='".$del."'") ;
      $result = mysql_query($qu ery_delete) or die('Error, query failed');
      echo "<meta http-equiv=\"refresh \" content=\"0\"; URL=\"untitled3 .php\">";
      }
      ?>[/PHP]

      Comment

      • yc022
        New Member
        • Mar 2007
        • 6

        #4
        Hi, the error i am getting is Access Forbidden. You don't have permission to access the requested object so i will defienetly take out the for each loop. It also says the document is either read-protected or not readable by the server.

        Comment

        • yc022
          New Member
          • Mar 2007
          • 6

          #5
          Hi, thank you for your help. I'm having a go at changing my code just now so will let you know how i get on.









          Originally posted by ajaxrand
          Hi Here is Solution.Just use this coding by changing the SQL Script.
          Please Note that here i am using a Table called product.
          Columns:
          p_id, p_name

          In the Select box I am displaying p_name and as the value of the each and every List menu item I am Passing the p_id for deleting purpose.

          That means the from the PHP delete section it will fetch this value Attribute.and pass it to the DELETE query. for this you can use your any other column. :) but for better performance, this primary key in my table is a Good idea.

          And also no need to use double quotes for PHP echo. use single quotes. Then No need to escape them.

          change this line also
          [PHP]echo "<meta http-equiv=\"refresh \" content=\"0\"; URL=\"untitled3 .php\">";[/PHP]

          [PHP]<?php
          require 'dbcon.php';
          $namequery = "SELECT p_name,p_id
          FROM products";

          $result = mysql_query($na mequery) or die('Error, query failed');
          $count = mysql_num_rows( $result);
          if ($count > 0)
          {
          echo '<form action="'.$PHP_ SELF.'" method="POST">' ;
          echo '<select name="item">';
          while($result_r ow = mysql_fetch_arr ay($result))
          {
          echo '<option value="'.$resul t_row[1].'" >';
          echo ($result_row[0]);
          echo '</option>';
          }
          echo '</select>';
          echo '<input type="submit" name="Delete" id="Delete" value="Delete" />';
          echo '</form>';
          }
          else
          {
          echo "There are no project numbers in the system";
          }

          if($_POST['Delete'])
          {
          $del = $_POST['item'];
          $query_delete = ("DELETE FROM products WHERE p_id ='".$del."'") ;
          $result = mysql_query($qu ery_delete) or die('Error, query failed');
          echo "<meta http-equiv=\"refresh \" content=\"0\"; URL=\"untitled3 .php\">";
          }
          ?>[/PHP]

          Comment

          • gauravgmbhr
            New Member
            • Feb 2007
            • 107

            #6
            Originally posted by yc022
            Hi, thank you for your help. I'm having a go at changing my code just now so will let you know how i get on.

            Well make sure that the username and password that u using to connect to the DataBase Server have permissions To delete. so contact UR DB-admin To change ur privelages to database
            Coz Most of the time The username given to a programmer by the admins of DB does nOt have permission to delete Rows From Table


            And Try Avoiding Using Foreach loop when u are not using an array coz PHP 5 Will produce A E-Warning if the argument supplied to Foreach Loop Is Not An array.
            And I guess U R Using Php4.XX

            Comment

            • yc022
              New Member
              • Mar 2007
              • 6

              #7
              Yeah, it's php 4. I managed to get it working so thank you to everyone for your help. The only problem is that my id's are now getting muddled up, but i have seen a thread on this site that will help me fix that. Thank you again.

              Comment

              Working...