Jump to content

[SOLVED] Need Help


phatuis

Recommended Posts

What I am trying to do, is say I have 1 piece of data with id 1, one with id 2, and one with 3. Using this code, I can successfully delete the selected data by id, but also what I want to do is for every id above the one deleted, I want to make them go down by 1, like the one with id of 2 would go to 1, and the one with id of 3 would go to 2, the code below is what I have tried, and been unsuccessful

 

This is my code so far:

$sqldelete = "DELETE from `banners` WHERE id='".$deleteid."'";
	$resdelete = mysql_query($sqldelete) or die(mysql_error());

	$upsql = "SELECT * FROM `banners` WHERE id > '".$deleteid."'";
	$upres = mysql_query($upsql) or die(mysql_error());

	$array[] = $upres;

	foreach($array AS $idnum)
	{

	$idstart = $deleteid + $idnum;
	$idupdate = $idstart - 1;

	$sqlupdate = "UPDATE `banners` SET id='".$idupdate."' WHERE id='".$idstart."'";
	$sqlres = mysql_query($sqlupdate);


	}

	echo "You have successfully deleted the item with an ID of ".$deleteid."!";

Link to comment
Share on other sites

When I click delete next to the data on the main page, the deleteid is found using GET, so like if the extension is admin.php?delete=1 , then the id being deleted is 1

 

So your not posting all ur code? lol bro we need to look at it all, sorry to be rude but just it helps alot to see what's going on everywhere u know man?

 

 

If it getting $_GET correctly then it should work, post all. let's me see.

 

echo that $deleteid variable to see what's it's calling too to make sure.

Link to comment
Share on other sites

All Code:

 

<?php

include("global.php");

connect();

$sql = "SELECT * FROM `banners` WHERE admin='1' AND username='".$session_username."'";
$res = mysql_query($sql) or die(mysql_error());

if(mysql_num_rows($res) == 1)
{

$sql2 = "SELECT * FROM `banners`";
$res2 = mysql_query($sql2) or die(mysql_error());

if(isset($_GET['delete']))
{


$deleteid = $_GET['delete'];
	if(!isset($_GET['sure']))
	{
		echo "<center>Are you sure you want to delete this data?<br><br>";
		echo "<a href='admin.php?delete=".$deleteid."&sure=1'>Yes, I am sure</a<br>";
		echo "<a href='admin.php'>No, I am not.</a></center>";
	}else
	{


	$sqldelete = "DELETE from `banners` WHERE id='".$deleteid."'";
	$resdelete = mysql_query($sqldelete) or die(mysql_error());

	$upsql = "SELECT * FROM `banners` WHERE id > '".$deleteid."'";
	$upres = mysql_query($upsql) or die(mysql_error());

	$array[] = $upres;

	foreach($array AS $idnum)
	{

	$idstart = $deleteid + $idnum;
	$idupdate = $idstart - 1;

	$sqlupdate = "UPDATE `banners` SET id='".$idupdate."' WHERE id='".$idstart."'";
	$sqlres = mysql_query($sqlupdate);


	}

	echo "You have successfully deleted the item with an ID of ".$deleteid."!";

	}
}
else
{
while($assoc = mysql_fetch_assoc($res2))
{

$id = $assoc['id'];
$username = $assoc['username'];
$sitename = $assoc['sitename'];
$bannerurl = $assoc['bannerurl'];
$url = $assoc['url'];

echo "<center><table border='1'>";
echo "<tr><td>".$username."</td><td>".$sitename."</td><td>".$bannerurl."</td><td>".$url."</td><td><a href='admin.php?delete=".$id."'>Delete</a></td></tr>";
echo "</table></center>";
}
}


}else
{
die("You do not have permission to access this area");
}


?>

Link to comment
Share on other sites

I'll break it down

 

$idstart is what the id starts as, like for the first foreach loop the id will be $deleteid + 1, because it is in the first loop, then on the second it is $deleteid + 2.

 

$idupdate is simply $idstart - 1, which then lowers each id by one after deletion.

Link to comment
Share on other sites

I have only briefly browsed the replies since the opening of the thread as I don't like the direction they were going in. Firstly why do you wish the id's to remain perfectly sequential? id's are generally of no real relevance other than for identifying the field. Secondly you shouldn't need to do any looping to achieve your objective, something like the following should suffice...

 

$sql = "DELETE FROM table WHERE id=$deleteid";
mysql_query($sql) or trigger_error("SQL: $sql, ERROR: " . mysql_error(), E_USER_ERROR);
$sql = "UPDATE table SET id=id-1 WHERE id > $deleteid";
mysql_query($sql) or trigger_error("SQL: $sql, ERROR: " . mysql_error(), E_USER_ERROR);

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.