Jump to content

PHP + SQL DELETING WRONG ROW


jamkelvl

Recommended Posts

<?php
//	session_start();
//	
//	if (!session_is_registered(myusername)) {
//		header("location:index.php");
//	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>CMS</title>
	<link href="styles/style.css" rel="stylesheet" type="text/css">
</head>
<body>
	<table>
		<tr>
			<td colspan="2" class="header">
				<p>
					<a href="logout.php" style=" float: right; margin-top: -10px; margin-right: 10px;">Log out</a>
					<a href="home.php"><img src="images/logo.jpg" width="175" height="60" alt="Diggy" class="logo" /></a>									
				</p>
				<p>
					<form>
						<input class="search" type="text" value="Search" />
					</form>
				</p>
			</td>
		</tr>
		<tr>
			<td colspan="2" class="nav">
				<h3>
					Navigation
				</h3>
				<p>	
					<?php include('sitenav.php'); ?>
				</p>					
			</td>
		</tr>
		<tr>
			<td class="left">	
				<?php include('nav.php'); ?>
			</td>
			<td class="content">
				<?php
					// Suppress all errors
					error_reporting(0);

					// Set all variables	
					$hostname = "xxxxxxxxx";
					$username = "xxxxxxxxx";
					$password = "xxxxxxxxx";
					$dbid="xxxxxxxxx";

					// Attempt database connection
					$connect = mysql_connect($hostname, $username, $password);
					if($connect == false){
						echo('<p class="error">We are having technical difficulties and apologize for the inconvenience. Please try again later.</p>');
					}

					// Select database
					$db = mysql_select_db($dbid);

					// Query the database
					$select = "SELECT * FROM pages";
					$result = mysql_query($select);

					switch ($_GET['delete']) {

						case ($_GET['delete'] == "?") :
							echo "<h3>Select Page to Delete:</h3>";								
							while($row = mysql_fetch_array($result)){
								extract($row);
								if ($type == "basic") {
									echo '<p>
											<a href="delete.php?delete='.$page.'"><img src="images/delbasp.jpg" width="125" height="125" alt="Edit '.$title.' Page" /></a>
											<p class="details">
												<strong>Page Name:</strong> '.$page.'.php<br/>
												<strong>Title:</strong> '.$title.'<br/><br/><br/><br/><br/>
												<a href="delete.php?delete='.$page.'">Delete</a>
											</p>
										  </p>';

								} else {
									echo '<p>
											<a href="delete.php?delete='.$page.'"><img src="images/delgalp.jpg" width="125" height="125" alt="Edit '.$title.' Page" /></a>
											<p class="details">
												<strong>Page Name:</strong> '.$page.'.php<br/>
												<strong>Title:</strong> '.$title.'<br/><br/><br/><br/><br/>
												<a href="delete.php?delete='.$page.'">Delete</a>
											</p>
										  </p>';
								}
							}
						break;

						case ($_GET['delete']) :
							//Check if user is trying to delete index page
							if ($page == "index") {
								echo '<h3>Error:</h3>';
								echo '<p class="error">We are unable to delete this page.</p>';
								echo "<meta http-equiv='refresh' content='1;URL=delete.php'>";	
							} else {								
								//Delete the actual file
								$myFile = $page.".php";
								unlink("../".$myFile);

								// Build query to delete row from table
								$delete = "DELETE FROM pages where page = '{$_POST['delete']}'";
								$result = mysql_query($delete);

								if ($result == true) {
									echo ('<h3>Success!</h3><p class="success">Deleting '.$title.' page.');	
									echo "<meta http-equiv='refresh' content='1;URL=delete.php'>";	
								} else {
									echo '<p class="error">There was a problem deleting this page.</p>';
								}
							}			
						break;
					}						
				?>
			</td>
		</tr>
		<tr>
			<td colspan="2">
				<p>
					Copyright 2009 Jamie Kelly
				</p>
			</td>
		</tr>			
	</table>																											
</body>
</html>

 

Anyone know why this is deleting the wrong row and page?

 

It will always delete the last record in the database rather than the selected one?

 

View the page here: www.easywebsnow.com/admin/delete.php (only allowed access to that page, delete whatever you want.)

 

In fact, try deleting process.php and you'll see that it deletes contact.php

Link to comment
https://forums.phpfreaks.com/topic/187480-php-sql-deleting-wrong-row/
Share on other sites

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.