Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 6 of 6
  1. #1
    Junior Member
    Join Date
    Nov 2005
    Posts
    10
    Member #
    11976
    I am trying to take a selection from a select box and delete the selection from the database. The values in the select box are retrieved from the database and everytime I select one to be deleted, the first one in the list is deleted instead. I am confused because I do the exact same thing for another table in my database and that one works.

    Here is the code:

    <?php
    //connect to server and select database
    $conn = mysql_connect("hawk.csc.villanova.edu", "group04f05", "philly") or die(mysql_error());
    mysql_select_db("shopping",$conn) or die(mysql_error());

    echo "<p> <strong>Store: </strong>";
    $sql = 'SELECT `id` , `name` , `address` FROM `store` WHERE 1 ORDER BY `name` ASC LIMIT 0, 500';
    $result = mysql_query($sql,$conn) or die(mysql_error());

    echo "<select name='store'>";
    if (mysql_num_rows($result) > 0)
    {
    while ($row = mysql_fetch_array($result))
    {
    $idDel = $row['id'];
    $nameDel = $row['name'];
    $addressDel = $row['address'];
    echo "<option value=\"$idDel\">$nameDel - $addressDel</option>";
    }
    }
    echo "</select>";

    echo"<p><INPUT type='submit' name='submitDelete' value='DELETE'></p>";

    if(isset($_POST['submitDelete']))
    {
    $idDel = $_POST['store'];

    $query = "DELETE FROM `store` WHERE `id`='$idDel' ";
    $result = mysql_query($query);

    if($result)
    {
    echo ("Successfully deleted from the database.");
    }
    else
    {
    die("Error: Could not delete entry: " . mysql_error());
    }
    }
    ?>


    Any help would be greatly appreciated!!!

  2.  

  3. #2
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    Where is your form tag? Self-referencing?

  4. #3
    Junior Member
    Join Date
    Nov 2005
    Posts
    10
    Member #
    11976
    Quote Originally Posted by bfsog
    Where is your form tag? Self-referencing?
    Yes, I'm sorry, this is just a piece of the code from a form that also inserts into the database. That part works. It is self-referencing.

    <FORM METHOD="POST" ACTION="<?php echo $_SERVER['PHP_SELF'];?>">

  5. #4
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Are you sure you don't have more than one select with the same name in the same form? You might be pulling the wrong select's value in that case.

  6. #5
    Junior Member
    Join Date
    Nov 2005
    Posts
    10
    Member #
    11976
    Quote Originally Posted by Shadowfiend
    Are you sure you don't have more than one select with the same name in the same form? You might be pulling the wrong select's value in that case.
    No, all the selects have different names.

  7. #6
    Junior Member
    Join Date
    Nov 2005
    Posts
    10
    Member #
    11976
    It works now

    For some reason it just started working, I don't know what I did. But now, I'd like to do the same thing and when a store is selected I would like to put the information in text areas in order to update it and send back the updated info to the database. Here is what I have so far, any suggestions?


    <FORM METHOD="POST" ACTION="<?php echo $_SERVER['PHP_SELF'];?>">

    <h3> Edit a Store! </h3>
    <?php
    //connect to server and select database
    $conn = mysql_connect("hawk.csc.villanova.edu", "group04f05", "philly") or die(mysql_error());
    mysql_select_db("shopping",$conn) or die(mysql_error());

    echo "<p> <strong>Store: </strong>";
    $sql = 'SELECT `id` , `name` , `address` FROM `store` WHERE 1 ORDER BY `name` ASC LIMIT 0, 100';
    $result = mysql_query($sql,$conn) or die(mysql_error());

    echo "<select name='storeEdit'>";
    if (mysql_num_rows($result) > 0)
    {
    while ($row = mysql_fetch_array($result))
    {
    $idEdit = $row['id'];
    $nameEdit = $row['name'];
    $addressEdit = $row['address'];
    $phoneEdit = $row['phone'];
    echo "<option value='$idEdit'>$nameEdit - $addressEdit</option>";
    }
    }
    echo "</select>";
    ?>

    <p><INPUT type="submit" name="submitEdit" value="EDIT"></p>

    <?php
    if(isset($_POST['submitEdit']))
    {
    $id = $_POST['storeEdit'];

    $sql = "SELECT `name` , `address` , `phone` FROM `store` WHERE 'id' = '$id' ";
    $result = mysql_query($sql, $conn) or die(mysql_error());
    $row = mysql_fetch_array($result);
    $id = $row['id'];
    $name = $row['name'];
    $address = $row['address'];
    $phone = $row['phone'];

    echo "<p><strong>Name:</strong><br> <INPUT type='text' size='25' name='nameEdit' value='$name'></p>";
    echo "<p><strong>Address:</strong><br> <INPUT type='text' size='35' name='addressEdit' value='$address'></p>";
    echo "<p><strong>Phone:</strong><br> <INPUT type='text' size='35' name='phoneEdit' value='$phone'></p>";
    echo "<p><strong>Hours:</strong><br> <INPUT type='text' size='35' name='hoursEdit'></p>";
    echo"<p><strong>Price Range: </strong>";
    echo "<select name = 'pricerangeEdit'>
    <option value='Cheap'>Cheap</option>';
    <option value='Average'>Average</option>';
    <option value='Expensive'>Expensive</option>';
    </select> </p>";
    echo "<p><strong>Category: </strong>";
    echo "<select name = 'categoryEdit'>
    <option value='Department'>Department Store</option>';
    <option value='Chain'>Chain Store</option>';
    <option value='Boutique'>Boutique</option>';
    <option value='Designer'>Designer Store</option>';
    </select></p>";

    echo "<p><INPUT type='submit' name='submitUpdate' value='UPDATE'></p>";

    if(isset($_POST['submitUpdate']))
    {
    //connect to server and select database
    $conn = mysql_connect("hawk.csc.villanova.edu", "group04f05", "philly") or die(mysql_error());
    mysql_select_db("shopping",$conn) or die(mysql_error());

    $nameEdit = $_POST['nameEdit'];
    $addressEdit = $_POST['addressEdit'];
    $phoneEdit = $_POST['phoneEdit'];
    $pricerangeEdit = $_POST['pricerangeEdit'];
    $categoryEdit = $_POST['categoryEdit'];

    $query = "UPDATE `store` SET 'name'='$nameEdit', 'address'='$addressEdit', 'phone'='$phoneEdit', 'pricerange'='$pricerangeEdit', 'category'='$categoryEdit' WHERE 'id'='$idEdit'";
    $result = mysql_query($query);

    if($result)
    {
    echo ("Successfully updated into the database.");
    }
    else
    {
    die("Error: Could not insert entry: " . mysql_error());
    }
    }

    }
    ?>
    </FORM>


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 11:54 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com