PDA

View Full Version : delete from database issues


m1a2x3x7
10-24-2007, 03:52 AM
I'm working on a site where the client can go in and delete artwork out of his database.

Say you have 3 pieces of art in there they have ids of 130 131 132

I want to delete the art that holds the id of 132 so I click on the delete button but it deletes the art that holds 130. I'm not sure why.

Here's a screen grab.

Here is my code (sorry it might be messy)

<?php

ob_start();
require_once("../connection.php");
session_start();

$display = 4;
$artist = $_GET['artist'];
$delete = $_POST['delete'];
$deleteId = $_POST['deleteId'];
$id = $_SESSION['id'];
$title =$_POST['title'];
$description =$_POST['description'];
$artSize =$_POST['artSize'];
$price =$_POST['price'];


if(!isset($_SESSION['id'])){
header("location: login.php");
exit;
}

if (isset($_GET['np'])){
$numPages = $_GET['np'];
}else{
$query = "SELECT * FROM art where artist = '$artist'";
$queryResult = mysql_query($query);
$numRecords = @mysql_num_rows ($queryResult);

if ($numRecords > $display) {
$numPages = ceil($numRecords/$display);
}else{
$numPages = 1;
}
}

if (isset($_GET['s'])) {
$start = $_GET['s'];
}else{
$start = 0;
}

$query = sprintf("SELECT * FROM art where artist = '$artist' ORDER BY art_id DESC LIMIT $start, $display");
$result = @mysql_query($query);
$row = mysql_fetch_array($result);
$artId = $row['art_id'];
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>McFee Gallery and Studio</title>
<link href="css/layout.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.style3 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; }
.style4 {font-size: 10px}
-->
</style>
</head>

<body>

<div id="wrapper">
<div id="header">
<div id="navbar"><a href="addArt.php" class="nav">add art </a> | <a href="deleteArt.php" class="nav">delete art</a> | <a href="editArt.php" class="nav">edit art</a> |<a href="monthlyArtist.php" class="nav"> monthly artist</a> </a>| <a href="quotes.php">quotes</a></div>
</div>
<div id="body">
<div id="adminBg">
<table width="776" border="0" align="center" class="pages">
<tr>
<td><div align="center" class="pages"><?php

if ($row > 0) {
if ($numPages > 1){
echo '<p>';
$currentPage = ($start/$display)+1;
if ($currentPage != 1){
echo '<a href="deleteArtistArt.php?artist=' . $artist . '&s=' . ($start - $display) . '&np=' . $numPages .'">Previous </a>';
}

for ($i = 1; $i <= $numPages; $i++){
if ($i != $currentPage){
echo '<a href="deleteArtistArt.php?artist=' . $artist . '&s=' .(($display * ($i-1))) . '&np=' . $numPages . '">'; echo ' ' . $i . ' '; echo '</a>';
}else{
echo ' ' . $i . ' ';
}
}

if ($currentPage != $numPages){
echo '<a href="deleteArtistArt.php?artist=' . $artist . '&s=' . ($start + $display) . '&np=' . $numPages . '"> Next</a>';
}
}
}
?></div></td>
</tr>
</table>
<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF'];?>">
<table width="781" border="0" align="left">
<tr>
<td height="21" valign="top">
<?php do { ?>
<div align="center"><span class="style3">Image | <img src ="<?php echo $artist."/".$row['sm_picture'];?>" width="40" height="40"/> Title |
<input name="title" type="text" class="style3" id="title" value="<?php echo $row['title'];?>"/>
Description |
<textarea name="description" rows="2" class="style3" id="description" /><?php echo $row['description'];?></textarea>
Art Size |
<input name="artSize" type="text" class="style3" id="artSize" value="<?php echo $row['art_size'];?>" size="6" />
Price |
<input name="price" type="text" class="style3" id="price" value="<?php echo $row['price'];?>" size="6" />
</span>
<input name="deleteId" type="hidden" id="deleteId" value="<?php echo $row['art_id'];?>" />
<input name="delete" type="submit" class="style3" id="delete" value="Delete" />
<?php $artId = $row['art_id']; echo $artId;?><br />
</div>
<?php } while ($row = mysql_fetch_array($result));

if($delete && $deleteId) {
$query = sprintf("DELETE from art where art_id = '$artId'");
mysql_query($query);
header("location: deletedArt.php?artist=$artist");
exit;
}
?>
<br /></td>
</tr>
</table>
</form>
</div>
</div>
<div id="footer">Please make sure you follow the instructions correctly. If done improperly it could cause major issues with the web site. <br />
If you are not sure what to do or think you might have done something wrong contact the web developer <br />
at <a href="mailto:cschultz@schultzstudio.com" class="nav style4"><strong>cschultz@schultzstudio.com</strong></a> immediately. </div>
</div>
</body>
</html>

Thanks for the help!

davidj
10-24-2007, 08:36 AM
why is your delete script in the middle of your HTML

m1a2x3x7
10-24-2007, 04:40 PM
ok I moved my delete script

<?php

ob_start();
require_once("../connection.php");
session_start();

$display = 4;
$artist = $_GET['artist'];
$delete = $_POST['delete'];
$deleteId = $_POST['deleteId'];
$id = $_SESSION['id'];
$title =$_POST['title'];
$description =$_POST['description'];
$artSize =$_POST['artSize'];
$price =$_POST['price'];


if(!isset($_SESSION['id'])){
header("location: login.php");
exit;
}

if (isset($_GET['np'])){
$numPages = $_GET['np'];
}else{
$query = "SELECT * FROM art where artist = '$artist'";
$queryResult = mysql_query($query);
$numRecords = @mysql_num_rows ($queryResult);

if ($numRecords > $display) {
$numPages = ceil($numRecords/$display);
}else{
$numPages = 1;
}
}

if (isset($_GET['s'])) {
$start = $_GET['s'];
}else{
$start = 0;
}

$query = sprintf("SELECT * FROM art where artist = '$artist' ORDER BY art_id DESC LIMIT $start, $display");
$result = @mysql_query($query);
$row = mysql_fetch_array($result);
$artId = $row['art_id'];

if($delete && $deleteId) {
$query = sprintf("DELETE from art where art_id = '$artId'");
mysql_query($query);
header("location: deletedArt.php?artist=$artist");
exit;
}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>McFee Gallery and Studio</title>
<link href="css/layout.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.style3 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; }
.style4 {font-size: 10px}
-->
</style>
</head>

<body>

<div id="wrapper">
<div id="header">
<div id="navbar"><a href="addArt.php" class="nav">add art </a> | <a href="deleteArt.php" class="nav">delete art</a> | <a href="editArt.php" class="nav">edit art</a> |<a href="monthlyArtist.php" class="nav"> monthly artist</a> </a>| <a href="quotes.php">quotes</a></div>
</div>
<div id="body">
<div id="adminBg">
<table width="776" border="0" align="center" class="pages">
<tr>
<td><div align="center" class="pages"><?php

if ($row > 0) {
if ($numPages > 1){
echo '<p>';
$currentPage = ($start/$display)+1;
if ($currentPage != 1){
echo '<a href="deleteArtistArt.php?artist=' . $artist . '&s=' . ($start - $display) . '&np=' . $numPages .'">Previous </a>';
}

for ($i = 1; $i <= $numPages; $i++){
if ($i != $currentPage){
echo '<a href="deleteArtistArt.php?artist=' . $artist . '&s=' .(($display * ($i-1))) . '&np=' . $numPages . '">'; echo ' ' . $i . ' '; echo '</a>';
}else{
echo ' ' . $i . ' ';
}
}

if ($currentPage != $numPages){
echo '<a href="deleteArtistArt.php?artist=' . $artist . '&s=' . ($start + $display) . '&np=' . $numPages . '"> Next</a>';
}
}
}
?></div></td>
</tr>
</table>
<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF'];?>">
<table width="781" border="0" align="left">
<tr>
<td height="21" valign="top">
<?php do { ?>
<div align="center"><span class="style3">Image | <img src ="<?php echo $artist."/".$row['sm_picture'];?>" width="40" height="40"/> Title |
<input name="title" type="text" class="style3" id="title" value="<?php echo $row['title'];?>"/>
Description |
<textarea name="description" rows="2" class="style3" id="description" /><?php echo $row['description'];?></textarea>
Art Size |
<input name="artSize" type="text" class="style3" id="artSize" value="<?php echo $row['art_size'];?>" size="6" />
Price |
<input name="price" type="text" class="style3" id="price" value="<?php echo $row['price'];?>" size="6" />
</span>
<input name="deleteId" type="hidden" id="deleteId" value="<?php echo $row['art_id'];?>" />
<input name="delete" type="submit" class="style3" id="delete" value="Delete" />
<?php $artId = $row['art_id']; echo $artId;?><br />
</div>
<?php } while ($row = mysql_fetch_array($result)); ?>
<br /></td>
</tr>
</table>
</form>
</div>
</div>
<div id="footer">Please make sure you follow the instructions correctly. If done improperly it could cause major issues with the web site. <br />
If you are not sure what to do or think you might have done something wrong contact the web developer <br />
at <a href="mailto:cschultz@schultzstudio.com" class="nav style4"><strong>cschultz@schultzstudio.com</strong></a> immediately. </div>
</div>
</body>
</html>

It still wants to delete whatever the highest id is.