By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,035 Members | 1,555 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,035 IT Pros & Developers. It's quick & easy.

Problem with deleting records from DB

Odisey
P: 14
I am not sure if this is a MySQL syntax error or a DB issue yet. I am getting the error from the code:

Page Error!
This page has been accessed in error.

The GET is populated in the browser address window with a valid user ID, yet I get the error. I've studied this script - it should work.

Any ideas?

===================================
[PHP]
<?php
//This page delets a user
//This page is accessed through view_users.php

$page_title = 'Delete a user';
include ('./includes/header.html');

//Check for a valid user ID through GET or POST

if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { //Accessed through view_users.php
$id = $_GET['id'];

} elseif ( (isset($_POST['id'])) &&
(is_numeric($_POST['id'])) ) { //Form has been submitted

$id = $_POST['id'];

} else { //No valid ID - kill the script

echo '<h1 id="mainhead">Page error.</h1>
<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';


include ('./includes/footer.html');
exit();

}

require_once ('mysql_connect2.php'); //Connect to DB

// Check if the form has been submitted

if (isset($_POST['submitted'])) {

if ($_POST['sure'] == 'Yes') { //Delete them

//Make the enquiry
$query = "DELETE FROM users WHERE user_id=$id";

$result = @mysql_query ($query); //Run the query

if (mysql_affected_rows() == 1) { //If it ran OK


//Print a message
echo '<h1 id="mainhead">Delete a User</h1>
<p>The user has been deleted.</p><p><br /><br /></p>';

} else { //If the query did not run

echo '<h1 id="mainhead">System error</h1>
<p class="error"> The user could not be deleted due to a system error.</p>'; //Message
echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query . '</p>'; //Debugging message

}

} else { // Was not sure about deleting the user

echo '<h1 id="mainhead"> Delete a User </h1>

<p> The user has NOT been deleted. </p><p><br /><br /></p>';

}

} else { //Show the form


//Retrieve the user's information

$query = "SELECT CONCAT(last_name, ', ', first_name) FROM users WHERE user_id=$id";

$result = @mysql_query ($query); //Run the query

if (mysql_num_rows($result) == 1) { // Valid user ID - show the form

// Get the users information

$row = mysql_fetch_array ($result, MYSQL_NUM);

//Create the form

echo '<h2> Delete a User </h2>

<form action="delete_user.php" method="post">
<h3>Name: ' . $row[0] . '</h3>
<p>Are you sure you want to delete this user?<br />
<input type="radio" name="sure" value="Yes" /> Yes
<input type="radio" name="sure" value="No" checked="checked" />No</p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name"id" value="' . $id . '" />
</form>';

} else { //Not a valid ID

echo '<h1 id="mainhead">Page Error</h1>
<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';

}


} //End of the main submit conditional

mysql_close(); //Close the database connection

include ('./includes/footer.html');

?>
[/PHP]
===============================================
May 14 '07 #1
Share this Question
Share on Google+
3 Replies


Odisey
P: 14
I found it!

The = sign in the id input tag......

Sorry to bother...

Odisey
May 14 '07 #2

Atli
Expert 5K+
P: 5,058
I'm glad you found your answer.

Just a thought, I usually put a limit clause on all DELETE and UPDATE statements to minimize the damage if something were to go wrong.
May 14 '07 #3

pbmods
Expert 5K+
P: 5,821
If where the variable came from isn't important (as appears to be the case in your script), you can also just use $_REQUEST (starting with PHP v4.1).

Expand|Select|Wrap|Line Numbers
  1. $id = $_REQUEST['id'];
  2. // Or better yet...
  3. $id = addslashes($_REQUEST['id']);
  4.  
http://www.php.net/manual/en/reserved.variables.php (most of the way down the page)
May 15 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.