Jump to content

PROBABLY A SQL PROBLEM


jamkelvl

Recommended Posts

Okay so I'll try to make this seem as simple as possible.  It really is pretty simple...

 

The below code simply deletes a specific file chosen by the user and also removes it from the database.

 

The problem is when a user selects a page, for example: process.php

 

and the below code will for some reason delete the last record in the database and the file associated with it rather than the selected page.

 

<?PHP                                                    
                                                       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
								$insert = "DELETE FROM pages where title = '{$title}'";
								$result = mysql_query($insert);

								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;
					}						
				?>

 

Any help?

Link to comment
Share on other sites

Where is $title defined?  Do me a favor and echo $insert (which is actually a DELETE statement, confusing variable name) before you execute the query:

 

$insert = "DELETE FROM pages where title = '{$title}'";
echo $insert;
die();
$result = mysql_query($insert);

 

You also don't need curly braces around $title, it will interpolate just fine.

Link to comment
Share on other sites

Took out the braces, thanks.

 

Added in echo to debug, returns: DELETE from pages where title = 'CONTACT US'

 

In this case I tried to delete OUR PROCESS (process.php) but instead it deletes contact.php which happens to be the last row in the table.

 

I believe $title isn't really set.. it's a column in the database....

 

<?PHP
// 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 title = '$title'";
								echo $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;
					}						
				?>

Link to comment
Share on other sites

http://easywebsnow.com/admin/delete.php

 

All other pages will ask you to login as I have only enabled delete.php.

 


<?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 = "xxxxxxxx";
					$username = "xxxxxxxx";
					$password = "xxxxxxxx";
					$dbid="xxxxxxxx";

					// 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 title = '$title'";
								echo $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>

Link to comment
Share on other sites

$delete = "DELETE FROM pages where title = '$title'";

Here you are saying that you need to delete it where title = $title. You haven't actually defined title anywhere. You should store the page name itself in the database and then call to the page name when deleting instead of the page title.

Alternatively

 

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>';
                           }
                        }

 

You could turn the inside of that while statement into a form at at the bottom of the form create some hidden inputs with the name and title of the page your trying to delete which would be sent via $_POST to the ?delete action so you could have the database entry deleted using $title

Link to comment
Share on other sites

I see what you're saying although I do not believe it is the solution.

 

I can try other things but still not understanding why this isn't working.

 

Also, I do not understand why you think defining $title will change anything, changing it to $name (page name.php) will change nothing, been there, tried that.  This is why I am here.

 

The database stores: id, name, title, paragraph1, paragraph2, paragraph3, type

 

All of which become variables once my loop as iterated through all records.

 

One may notice that the page www.easywebsnow.com/admin/delete.php lists all rows in the database (all pages for the website), included with this array of pages are page details such as $name and $title, doesn't that mean they're set?

 

Logically, what I've done makes perfect sense and I can not seem to see why this does not work..

 

The reason I have done this the way I have, is because there "delete function" is used more than once, for example:

 

on the edit.php page, one has the option to delete the page from there which simple just calls delete.php, but of course this isn't working properly.

 

Link to comment
Share on other sites

P.S - Don't mean to be snappy, just frustrated.

 

If setting these variables will change anything, where and how do I set them?  What values do I give them?  I'm not creating a form.. that wouldn't work the way I want it.

 

I'm well aware I can just capture form data and pass it via $_POST..

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.