Jump to content

Deleting rows from database


fourthe

Recommended Posts

I'm new to PHP and I'm having trouble figuring this out. I think i have my if statement messed up, if someone could help me that would be great. Thank you

 

After filling out the form, it should submit to the code below

 

1. Inserts the "ordered" item into the database

2. Shows a list of all items that have been ordered

3. Provides a link to delete/remove any item from the cart

If the delete link is clicked, it should delete the corresponding item and return to the shopping cart page

 

<?php

 

$cmd = "delete"; //This will set the $cmd variable to allow the delete function to run

 

$con = mysql_connect('localhost', 'root', 'kellogg');

$db=mysql_select_db('welshch3', $con);

 

$check1 = $_REQUEST['check1'];

$check2 = $_REQUEST['check2'];

 

        $check1 = 0;

        $check2 = 0;

        foreach($var as $c)

        {

            if($c == "c1")

            {

                $check1 = 1;

            }

            elseif($c =="c2")   

            {

                $check2 = 1;

            }

        }

 

$insert = "INSERT INTO cart (text1, text2, button, check1, check2, comment) VALUES ('" . $_REQUEST['text1'] . "', '" . $_REQUEST['text2'] . "', '" . $_REQUEST['button'] ."'," . $check1 . "," . $check2 .", '" . $_REQUEST['special'] . "')";

 

if(!isset($cmd)){

 

mysql_query($insert);

$select = "SELECT * FROM cart";

$result = mysql_query($select);

 

 

$id=$row["id"];

while ($row = mysql_fetch_array($result)) {

    echo "First Line: " . $row['text1'] . "\n";

    echo "Second Line: " . $row['text2'] . "\n";

    echo "Color: " . $row['button'] . "\n";

    echo "Extra: " . $row['check1'] . "\n";

    echo "Special: " . $row['comment'] . "\n";

    echo "<a href='cart.php?del_id=id'>delete</a><br>";

    }

}

 

if($cmd=="delete")

{

    $sql = "DELETE FROM cart WHERE id=$id";

    $result = mysql_query($sql);

    echo "Row deleted!";

}

 

 

?>

Link to comment
Share on other sites

1st change

 echo "<a href='cart.php?del_id=id'>delete</a><br>";

to echo "<a href='cart.php?del_id=$id'>delete</a><br>";[/code]and

if($cmd=="delete")
{
    $sql = "DELETE FROM cart WHERE id=$id";
    $result = mysql_query($sql);
    echo "Row deleted!";
}

to

if(isset($_GET['del_id']))
{
    $id=$_GET['del_id'];
    $sql = "DELETE FROM cart WHERE id=$id";
    $result = mysql_query($sql);
    echo "Row deleted!";
}

 

Link to comment
Share on other sites

Go into your php ini file and find this:

error_reporting = E_ALL | E_NOTICE | E_STRICT

 

or rather your error reporting part, and add that to the line.

Then you will see what the problems are, I know its not pretty but it deffinatly helps finding errors out faster. It basically tells you where to look.

 

Remember when you change this to retart apache.

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.