Jump to content

Deleting single rows


gazza52

Recommended Posts

Hi,

 

I have developed a search which connects to a mysql database.

 

From these results I have added them to a table which has an option to add the items to a shopping basket.

 

When you go to the shopping basket the added items are shown. I have added an option to empty the basket.

 

What I am having problems with is deleting single items from the table.

 

My code for the shopping basket is:

 

<? session_start();
if (!isset($_SESSION["loggedIn"]))
{
include ("login.php");
exit;
}
if (!isset($_SESSION["basketcount"]))
{
$items=0;
}
else
{
$items= $_SESSION["basketcount"];
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
  <HEAD>
    <META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <TITLE> Shrewsbury Town FC
    </TITLE>
<?
   IF (ISSET($_COOKIE["selectedStyle"])) // if style has been set
   {
     $style=$_COOKIE["selectedStyle"];
   }
   ELSE // if style not yet set, default to 0
   {
       $style="Master";
   }
    ?>
    <LINK rel="stylesheet" href="style<?= $style; ?>.css">
  </HEAD>
  <BODY>
      <DIV id="header">
        <IMG src="mlogo.jpg"alt="Shrewsbury Town Unofficial Logo">
      </DIV>
      <DIV id="date">
<?
    PRINT date("l M dS, Y",time());
    PRINT "<br>";
    PRINT date ("H:i",time());
      ?>
    </DIV>
    
    <DIV id="basket">
<?
    ECHO "You have $items<br>";
    ECHO "items in your basket<br>";
    ECHO"<a href=\"basket.php\">View Basket</a>";
      ?>
    </DIV>
    <H1>Shopping Basket</H1>
      <DIV id="navigation">
        <UL>
          <LI>
          <A href="index.php">Home</A></LI>
          <LI>
          <A href="history.php">Club History</A></LI>
          <LI>
          <A href="clubshop.php">Club Shop</A></LI>
          <LI>
          <A href="F&R.php">Fixtures & Results</A></LI>
          <LI>
          <A href="prostar.php">Prostar Stadium</A></LI>
          <LI>
          <A href="account.php">My Account</A></LI>
          <LI>
          <A href="feedback.php">Feedback</A></LI>
          <LI>
          <A href="logout.php">Logout</A></LI>
        </UL>
      </DIV>
    <DIV id="resultstable">
<table align="center" border="1" cellpadding="5" cellspacing="0">
<tr>
<td width="500"><b>Item Selected</b></td>
<td width="100"><b>Remove Item</b></td>
</tr>
<?

if (isset($_SESSION["basket"]))
{
foreach ($_SESSION["basket"] as $item)
{
print ("<tr>");
print ("<td>$item</td>");
print ("<td>Remove</a></td>"); 
print ("<tr>");

}
}
?>
</table>
<A href="add.php?empty=true">Empty the basket</A>
    </DIV>
    </BODY>
</HTML>

The add.php is a script which allows me to add and remove items. The code for this is shown below: 

<? session_start();
IF (!ISSET($_SESSION["loggedIn"]))
{
INCLUDE ("login.php");
EXIT;
}
IF (!ISSET($_SESSION["basketcount"]))
{
$items=0;
}
ELSE
{
$items= $_SESSION["basketcount"];
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
  <HEAD>
    <META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <TITLE> Shrewsbury Town FC
    </TITLE>
<?
   IF (ISSET($_COOKIE["selectedStyle"])) // if style has been set
   {
     $style=$_COOKIE["selectedStyle"];
   }
   ELSE // if style not yet set, default to 0
   {
       $style="Master";
   }
    ?>
    <LINK rel="stylesheet" href="style<?= $style; ?>.css">
  </HEAD>
  <BODY>
    <DIV id="header">
      <IMG src="mlogo.jpg"alt="Shrewsbury Town Unofficial Logo">
    </DIV>
    <DIV id="date">
<?
    PRINT date("l M dS, Y",time());
    PRINT "<br>";
    PRINT date ("H:i",time());
      ?>
    </DIV> 
    <DIV id="basket">
<?
    ECHO "You have $items<br>";
    ECHO "items in your basket<br>";
    ECHO"<a href=\"basket.php\">View Basket</a>";
      ?>
    </DIV>
    <H1>Online Club Shop</H1>
    <DIV id="navigation">
      <UL>
        <LI>
        <A href="index.php">Home</A></LI>
        <LI>
        <A href="history.php">Club History</A></LI>
        <LI>
        <A href="clubshop.php">Club Shop</A></LI>
        <LI>
        <A href="F&R.php">Fixtures & Results</A></LI>
        <LI>
        <A href="prostar.php">Prostar Stadium</A></LI>
        <LI>
        <A href="account.php">My Account</A></LI>
        <LI>
        <A href="feedback.php">Feedback</A></LI>
        <LI>
        <A href="logout.php">Logout</A></LI>
      </UL>
    </DIV>
    <DIV id="resultstable">
<?
      if (isset($_GET["add"]))
{
$_SESSION["basket"][]=$_GET["add"];
if (isset($_GET["add"]))
$_SESSION["basketcount"] = $items;
$_SESSION["basketcount"] = $_SESSION["basketcount"] + 1;
header ("location:http://mi-linux.wlv.ac.uk/~0525154/clubshop.php");
}
if (isset($_GET["empty"]))
{
unset($_SESSION["basket"]);
$_SESSION["basketcount"] = 0;
header ("location:http://mi-linux.wlv.ac.uk/~0525154/basket.php");
}
?>

      <a href="basket.php">View Basket</a>
<p>
    <a href="http://validator.w3.org/check?uri=referer"><img
        src="http://www.w3.org/Icons/valid-html401-blue"
        alt="Valid HTML 4.01 Transitional" height="31" width="88"></a>
  </p>
    </div>
  </body>
</html>

 

Please advise on code which will allow me to delete single items from the basket.

 

Thanks

Link to comment
Share on other sites

ok I hope you understand what I'm about to say..

 

in `basket` you're going to need an auto_incrementing id field

 

THEN in the basket php when you're showing the user what he has in his basket.. have the delete button send the item's ID to another php file like..

 

deleteItem.php?id=10

 

then in deleteItem.php you get the user's id from the cookie or session

 

and do something like this

 

<?php
  $id = (int) $_GET['id'];
  $sql = "DELETE FROM `basket` WHERE `id` = '{$id}' AND `user_id` = '{$_SESSION['user_id']}'";
  mysql_query($sql);
?>

Link to comment
Share on other sites

Can anyone help me with this.

 

I have now managed to add a radio button under a field called delte and a record number for each item that is added to the basket. The record number will start from 0 and go up.

 

To test the record number I have created a button which deletes a certain record number row and this works.

 

What I am struggling with is how to delete a certain row if its radio button is checked with one single delete button.  ???

Link to comment
Share on other sites

I'd use a checkbox instead of a radio button.

<input type="checkbox" name="delme" /> Delete

 

That way you can simply check for an "on/off" state of "delme" like this:

if ($_POST['delme']) {
mysql_query("DELETE FROM basket WHERE id='".$id."' LIMIT 1");
}

 

Something like that although you'd have to adapt it for your script.

Link to comment
Share on other sites

I have now managed to get a checkbox for each item.

 

I have put a single field in which is called Delete item.

 

What I need to do now is say when I click delete item the checkbox which is ticked is deleted along with its row.

 

The delete item should call the add.php script contains something like:

 

if ($_POST['delme']) {

unset (session [basket][$item]; --$item is the id field

}

 

What I do not understand is what I would send to add.php and if the code above is correct?

 

Thanks for your help.

Link to comment
Share on other sites

Have you got a submit button which is pointing to your script to handle it?

 

The idea (I'm presuming) is to have a list of checkboxes the user can tick a few then submit the form to a script that'll see which ones are ticked and act accordingly.

 

Now, let's presume your checkboxes are all marked something like this:

<input type="checkbox" name="chk1" />
<input type="checkbox" name="chk2" />
<input type="checkbox" name="chk3" />

 

Now we can set up a loop to check them and see ifthey're ticked:

for ($i=1;$i<$maxcheckboxes;++$i) {
  if ($_POST['chk'.$i]) {
    echo 'Checkbox '.$i.' is ticked<br />';
  }
}

 

Something like that.

Link to comment
Share on other sites

in 1st file use

foreach ($_SESSION["basket"] as $k => $item)
{
print ("<tr>");
print ("<td>$item</td>");
print ("<td><a href='add.php?delete=$k'>Remove</a></td>");
print ("<tr>");
}

and in add.php add

if (isset($_GET["delete"]))
{
if(isset($_SESSION["basket"][$_GET["delete"]])) {unset($_SESSION["basket"][$_GET["delete"]]);
$_SESSION["basketcount"]--;}
header ("location:http://mi-linux.wlv.ac.uk/~0525154/basket.php");
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.