473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Deleting record from MySQL

23
I'm having a bit of trouble creating a page to delete a story from the content table in my database.

I have checked my DELETE query directly in phpmyadmin and it is working fine.

I am echoing the query to check that it comes out as expected and it looks fine.

The problem seems to be that when my code is run the record never gets deleted.

Meaning that mysql_affected_rows() is always -1 and therefore an error message - 'Could not delete the story' is always output.

I just can't figure out why, Any help would be greatly appreciated.

Thanks.

Here is my code
[php]
<?php
# Filename - delete_story.php
# Date - 10th August 2007
# Author - Stephen Hoult
# Author Email - stephen@hoult.org.uk

// This file allows logged in users to delete a story.

// Include config file for error management and such
include('./includes/config.inc.php');

// Set page title and include HTML header
$page_title = 'Delete a Story';
include('./includes/header.html');


// If no first_name variable exists, redirect the user.
if (!isset($_SESSION['first_name'])) {

// Start defining the URL.
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
// Check for a trailing slash.
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1); // Chop off the slash.
}

// Add the page.
$url .= '/login.php';

ob_end_clean(); // Delete the buffer.
header("Location: $url");
exit(); // Quit the script.

} else { // First name variable exists - user is logged in

// Check to see if ID exists
if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) {
$id = $_GET['id'];

} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) {
$id = $_POST['id'];

} else { // Kill The script
echo '<p><font color="red" size="+1">That ID does not exist. Please select a Valid ID</font></p>';
include('./includes/footer.html'); // Include HTML footer
exit();

} // End of If ID exists


// Connect to the database
require_once('../mysql_connect.php');

// Check if the form has been submitted
if (isset($_POST['submitted'])) {


if ($_POST['sure'] == 'Yes') { // Delete the story - User selected Yes

// Make the query
$query = "DELETE FROM content WHERE id = $id LIMIT 1";
$result = mysql_query($query); // Run the query


echo $query; //For debugging purposes
echo mysql_affected_rows(); //For debugging purposes


if (mysql_affected_rows() == 1) { // Query ran ok

// Print a message

echo '<p>The story has been deleted.</p>';

} else { // If it didn't run ok

echo '<p><font color="red" size="+1">The story could not be deleted. Please try again.</font></p>';

} // End if query ran ok


} else { // Wasn't sure about deleting - User selects No
echo '<p><font color="red" size="+1">The story was not deleted.</font></p>';
}

} else { // else the form was not submitted - Show the form


?>
<fieldset><legend>Delete a story</legend>
<p> Are you sure you want to delete this story?</p>

<form action="delete_story.php" method="post" >
<p><input type="radio" name="sure" value="Yes" >Yes</p>
<p><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="text" name="id" value="<?php $_POST['id'] = $id; echo $_POST['id'] ;?>">

</form>

<?php

} // End of is submitted
} // End if logged in

mysql_close(); // Close the database connection
include('./includes/footer.html'); // Inlcude the HTML footer.
?>[/php]
Aug 11 '07 #1
6 1784
Atli
5,058 Expert 4TB
Hi.

Try adding this line if your query fails. It will print out any error that MySQL returns on a failed query.
Expand|Select|Wrap|Line Numbers
  1. echo mysql_error();
  2.  
Aug 11 '07 #2
jx2
228 100+
[PHP]
$query = "DELETE FROM content WHERE id = $id LIMIT 1";
$result = mysql_query($query); // Run the query

[/PHP]
well i think this is the line which might cos your problem

should be:

id = '$id'

and is the LIMIT 1 necessary in here?

regards
jx2
Aug 11 '07 #3
keeps21
23
SOLVED

The single quotes around the $id variable in the string are unneccesary.

I added the mysql_error() and recieved the following message:

Table 'content.content' doesn't exist

Problem was I had included the wrong mysql_connect.php file

This was flagged by the mysql_error() message showing that the query was looking for a database named 'content' instead of the content table on the my_cms database

Thanks for your help.
Aug 11 '07 #4
Atli
5,058 Expert 4TB
[PHP]
$query = "DELETE FROM content WHERE id = $id LIMIT 1";
$result = mysql_query($query); // Run the query

[/PHP]
well i think this is the line which might cos your problem

should be:

id = '$id'

and is the LIMIT 1 necessary in here?

regards
jx2
Quotes should only be used arround string values (including dates and binary strings), numeric values should not be quoted.

You should always use LIMIT clause if it is at all possible when deleting or updating data. There is always a possibility that something might go wrong, or that somebody makes it go wrong, and this prevents such an error from whiping out your entire table.
Aug 11 '07 #5
Atli
5,058 Expert 4TB
SOLVED
..
Glad you found your answer.
Aug 11 '07 #6
jx2
228 100+
great men :-)

i didnt notice you did checked it if that was a number :-)

jx2
Aug 11 '07 #7

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

Similar topics

7
by: Rob | last post by:
Thanks for the Help on the other problems...all resolved. One last (hopefullly) problem I can't seem to get around for this program...why isn't this deleting the row i want? It does nothing. Now...
2
by: R. Tarazi | last post by:
Hello, DB-structure: ========= firmendaten ----------------- firmendatenid <- firmennummer name strasse
1
by: Mark | last post by:
This question refers to a main form with a continuous form subform. After an error occurs after entering several records in the subform, how can I delete all the data in the main form and all the...
3
by: Nathan Bloom | last post by:
Hi, I have a data entry form (access 2000) that also allows the user to add, update, and delete records from the form. The Delete action is carried out in an event procedure and has the...
2
by: Lee-Anne Waters via AccessMonster.com | last post by:
Hi, would someone please help me with this problem. i have a list box that displays a persons various sub records. what i want to do is to be able to double click a list box record and have it...
46
by: DP | last post by:
hi, i've got a form, with a subform in it. i've got a delete button in the subform. the code i;ve got is; Private Sub cmdDeleteRecord_Click() msg = "Are you sure you want to delete this...
5
by: Bob Bedford | last post by:
Hello, I'm doing a select in wich I retrieve some values. If the value is in an array, I must delete the record. As I'm doing a "mysql_fetch_object", may I delete this record inside the...
5
by: Manish | last post by:
The topic is related to MySQL database. Suppose a table "address" contains the following records ------------------------------------------------------- | name | address | phone |...
1
by: Kyosuke18 | last post by:
Hi everyone, I have a problem in deleting a data that is connected on the database.. I tried this code but it shows me an error: Run-time error '-2147217900(80040e14)': Syntax error in string in...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.