Jump to content

Delete Product from website and database


Jools1988

Recommended Posts

Hi there, I've been having trouble the past few days figuring out what was wrong with my code.

 

What I'm trying to do is delete a product from an online store and from the mysql database connected to it. I have not implemented a cart function yet so there is no code for that function.

 


<?php

session_start();

require_once('./connections/connect.php');
require_once("template.html");

if (isset($_POST['submit']) && isset($_POST['number']))
{
$Description = $_POST['description'];
$Type = $_POST['type'];
$Units = $_POST['units'];
$Storage = $_POST['storage'];
$Price = $_POST['price'];

$query = "DELETE FROM product WHERE PID = '$_POST[number]'";
$result = mysql_query($query);

if(mysql_affected_rows() == 1)
{
echo "The product details have been deleted from the system";
}

}

echo "<p></p>";
$query = "SELECT PID, P_Desc, P_Type, P_Units, P_Storage, P_Unit_Price FROM product WHERE PID = '$_POST[id]'";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result))
{
$id = $row['PID'];
$Description = $row['P_Desc'];
$Type = $row['P_Type'];
$Units = $row['P_Units'];
$Storage = $row['P_Storage'];
$Price = $row['P_Unit_Price'];
}


?>

<form action="delete_product.php" method="post" class ="contact">
<fieldset>
<legend>Delete Product Details</legend>

<p>
<label for="description" class ="fixed_required">Description: </label>
<input type="text" name="description" id="description" value ="<?php echo $Description; ?>"/>
</p>

<p>
<label for="type" class ="fixed_required">Type: </label>
<input type="text" name="type" id="type" value ="<?php echo $Type; ?>"/>
</p>

<p>
<label for="units" class ="fixed_required">Units: </label>
<input type="text" name="units" id="units" value ="<?php echo $Units; ?>"/>
</p>

<p>
<label for="storage" class ="fixed_required">Storage: </label>
<input type="text" name="storage" id="storage" value ="<?php echo $Storage; ?>"/>
</p>

<p>
<label for="price" class ="fixed_required">Price: </label>
<input type="text" name="price" id="price" value ="<?php echo $Price; ?>"/>
</p>

<p class="submitbuttonarea" id="submit">
<input type="submit" name ="submit" id="submit" value="Submit" />
<input type="hidden" name ="number" id="number" value ="<?php echo $id; ?>" />
</p>

</fieldset>
</form>

 

Any input is apreciated. Thanks!

 

Edit: Left out important code oops!

 


<?php

session_start();

require_once('./connections/connect.php');
require_once("template.html");

$query = "SELECT * FROM product"; 
$result = mysql_query($query);

echo "<p></p>";
echo "<table class ='events'>";

echo "<th align='center'>ID Number</th><th align='center'>Description</th><th align='center'>Type</th><th align='center'>Units</th><th align='center'>Storage</th><th align='center'>Unit Price</th><th align='center'></th><th align='center'></th>";

while ($row = mysql_fetch_array($result))
{
$id = $row['PID'];
$Description = $row['P_Desc'];
$Type = $row['P_Type'];
$Units = $row['P_Units'];
$Storage = $row['P_Storage'];
$Price = $row['P_Unit_Price'];
$_SESSION['id']=$id;

echo
"<tr><td align='center'>$id</td><td align='center'>$Description</td><td align='center'>$Type</td><td align='center'>$Units</td><td align='center'>$Storage</td><td align='center'>$Price</td>";
echo '<td>

<form method="post" action="delete_product.php" name="delete_product">
<input type ="submit" name="submit" id="submit" value="Delete Product" /> 
<input type="hidden" name="id" value="'.$id.'" /> 

</form>
</td>
<td>

<form method="post" action="edit_product.php" name="edit_product">
<input type ="submit" name="submit" id="submit" value="Edit Product Details" /> 
<input type="hidden" name="id" value="'.$id.'" />

</form>
</td>
</tr>';
}

echo "</table>";
?>

Well I noticed one problem:

 

$query = "DELETE FROM product WHERE PID = '$_POST[number]'";

 

This is not actually getting the number because you are missing the quotes. I would change this to someting like this:

 

 

$number = $_POST['number'];
$query = "DELETE FROM product WHERE PID = '$number'";

 

Well I noticed one problem:

 

$query = "DELETE FROM product WHERE PID = '$_POST[number]'";

 

This is not actually getting the number because you are missing the quotes. I would change this to someting like this:

 

 

$number = $_POST['number'];
$query = "DELETE FROM product WHERE PID = '$number'";

 

Sorry for the late reply, but thank you for posting. I will test it out as soon as i can and let you know.

 

Your query's have the same name change them.

 

look at all your arrays man come on $redarrow['look_array'] <<< correct

not correct $redarrow[look_array]

 

 

did this code as posted work earlier

$number = $_POST['number'];
$query = "DELETE FROM product WHERE PID = '$number'";

that why i asked as i no the code works (( your driving me crazy lol

 

 

Most programmers say it bad programming practice to use any functions within a sql query, but as time goes on most php programming books now show it as good programming practice (crazy man).

 

It all about preference i guess.

lol i think we confused each other. I was just saying that it was good practice to use the trim() and the mysql_real_escape_string() functions not actually how you used them. I think either way you use it is personal preference. Personally I use them outside of my queries for readablity.

Thanks for the replies, unfourtunatly green your code was giving us te same problem.

 

We decided to completly revamp the delete by using a checkbox instead. After this we got it working.

 


<?php
	$dbconn = mysql_connect("127.0.0.1","mannixtn","R00022424");
	mysql_select_db("britney");

	include("template.html");

	if($_POST['delete']) // from button name="delete"
	{
		$checkbox = $_POST['checkbox']; //from name="checkbox[]"
		$countCheck = count($_POST['checkbox']);

		for($i=0;$i<$countCheck;$i++)
		{
			$del_id  = $checkbox[$i];
			$sql = "DELETE from product where PID = $del_id";
			$result = mysql_query($sql, $dbconn);
		}
			if($result)
			{
				echo "Product has being deleted from the system";
			}
			else
			{
				echo "Error: ".mysql_error();
			}
	}
?>

 

 

<?php
		//mysql connection
		$dbconn = mysql_connect("127.0.0.1","mannixtn","R00022424");
		mysql_select_db("britney");

		$sqlquery = "SELECT * FROM product"; // query on table
		$sqlresult = mysql_query($sqlquery, $dbconn);
		$count = mysql_num_rows($sqlresult); // count query result			

include("template.html");
?>

<table >
<tr><td>	<form method="post" action="delete_multiple2.php">
<table width="700" border="1" cellpadding="5" cellspacing="5" ><tr>

<tr><td align="center">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Description</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Type</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Units</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Storage</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Price</strong></td></tr>

<?php while($row = mysql_fetch_array($sqlresult)){ ?>

<tr><td align="center"> <input type="checkbox" name="checkbox[]" id="checkbox[]"  value="<?php echo $row['PID']?>" />
</td><td><?php echo $row['PID']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['P_Desc']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['P_Type']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['P_Units']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['P_Storage']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['P_Unit_Price']; ?></td></tr>

<?php } ?>

<tr><td colspan="7" align="center">
<input name="delete" type="submit" id="delete" value="Delete"></td></tr>

</table>
</form></td></tr></table>

 

Thanks for all your replies, and sorry for wasting your time!

Archived

This topic is now archived and is closed to further replies.

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