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

Home Posts Topics Members FAQ

DELETE problem with MySQL

I've been learning using PHP and MySQL from the tutorial at
freewebmasterhelp.com. Everything works fine, except deleting things.
The script is invoked with a URL like this:

http://www.example.org/test/delete_entry.php?id=35

And here's the script:

<?
include("dbinfo.inc.php");

$id = $_POST['id'];
mysql_connect($server, $user, $password);
$query = "DELETE FROM test1 WHERE id = '$id'";
@mysql_select_db($database) or die("Unable to select database.");
mysql_query($query);
mysql_close();

header("Location: entries.php");
?>

The dbinfo.inc.php file defines the variables $server, $database, $user
and $password. The login part works.

Gustaf
Jul 17 '05 #1
12 3265
I'm no expert but if the code is invoked via the url then you will need to
use the $_GET global variable to access it not the $_POST one you presently
are using Hope this helps
Noel.

"Gustaf Liljegren" <gu*****@algonet.se> wrote in message
news:cf**********@green.tninet.se...
I've been learning using PHP and MySQL from the tutorial at
freewebmasterhelp.com. Everything works fine, except deleting things.
The script is invoked with a URL like this:

http://www.example.org/test/delete_entry.php?id=35

And here's the script:

<?
include("dbinfo.inc.php");

$id = $_POST['id'];
mysql_connect($server, $user, $password);
$query = "DELETE FROM test1 WHERE id = '$id'";
@mysql_select_db($database) or die("Unable to select database.");
mysql_query($query);
mysql_close();

header("Location: entries.php");
?>

The dbinfo.inc.php file defines the variables $server, $database, $user
and $password. The login part works.

Gustaf

Jul 17 '05 #2
On Sat, 14 Aug 2004 07:29:13 +0200, Gustaf Liljegren wrote:
freewebmasterhelp.com. Everything works fine, except deleting things.


often it helps to print out the error message from the database
server. In your case put the line:
echo mysql_error()

after the line:
mysql_query($query)

so you will get a hint what causes the problem. If that don't help,
just print out the query line with "echo $query" to check if you pass
the correct SQL syntax and if you put in all the information that you
wanted to pass to mySQL.

These two things helped me most of the time to find any error related
to mySQL problems.

Regards

Marian

--
Tipps und Tricks zu PHP, Coaching und Projektbetreuung
http://www.heddesheimer.de/coaching/
Jul 17 '05 #3
"Gustaf Liljegren" <gu*****@algonet.se> wrote in message
news:cf**********@green.tninet.se...
I've been learning using PHP and MySQL from the tutorial at
freewebmasterhelp.com. Everything works fine, except deleting things.
The script is invoked with a URL like this:

http://www.example.org/test/delete_entry.php?id=35

And here's the script:

<?
include("dbinfo.inc.php");

$id = $_POST['id'];
mysql_connect($server, $user, $password);
$query = "DELETE FROM test1 WHERE id = '$id'";
@mysql_select_db($database) or die("Unable to select database.");
mysql_query($query);
mysql_close();

header("Location: entries.php");
?>


If the examples are anything like this then I suggest that you look
elsewhere for free tutorials. Only one statement in the above code attempts
to detect and handle errors (and not very nicely at that), no wonder you are
having trouble.

<?php

include("dbinfo.inc.php");

$id = $_POST['id'];
if($conn = mysql_connect($server, $user, $password))
{
if(! mysql_select_db($database))
{
echo "Unable to select the database";
exit;
}
$query = "DELETE FROM test1 WHERE id = '$id'";
$result = mysql_query($query , $conn);
if(! $result || mysql_error($conn))
{
printf("Unable to delete record: %s [%s]" , mysql_error($conn) ,
$query);
exit;
}
mysql_close();
header("Location: entries.php");
exit;
}
echo "Unable to open database connection";
?>

This should point out what the error is.
Jul 17 '05 #4
CJ Llewellyn wrote:
"Gustaf Liljegren" <gu*****@algonet.se> wrote in message
news:cf**********@green.tninet.se...
I've been learning using PHP and MySQL from the tutorial at
freewebmasterhelp.com. Everything works fine, except deleting things.
The script is invoked with a URL like this:

http://www.example.org/test/delete_entry.php?id=35

And here's the script:

<?
include("dbinfo.inc.php");

$id = $_POST['id'];
mysql_connect($server, $user, $password);
$query = "DELETE FROM test1 WHERE id = '$id'";
@mysql_select_db($database) or die("Unable to select database.");
mysql_query($query);
mysql_close();

header("Location: entries.php");
?>


If the examples are anything like this then I suggest that you look
elsewhere for free tutorials. Only one statement in the above code
attempts to detect and handle errors (and not very nicely at that), no
wonder you are having trouble.

<?php

include("dbinfo.inc.php");

$id = $_POST['id'];
if($conn = mysql_connect($server, $user, $password))
{
if(! mysql_select_db($database))
{
echo "Unable to select the database";
exit;
}
$query = "DELETE FROM test1 WHERE id = '$id'";
$result = mysql_query($query , $conn);
if(! $result || mysql_error($conn))
{
printf("Unable to delete record: %s [%s]" , mysql_error($conn)
,
$query);
exit;
}
mysql_close();
header("Location: entries.php");
exit;
}
echo "Unable to open database connection";
?>

This should point out what the error is.


Also your code is subject to SQL injection. (Do a Google search if you don't
know what that is http://www.google.com/search?q=sql+injection)

You should cast the $id as an integer like in the following examples:
$id = (int)$_POST['id'];
$id = (int)$_GET['id'];
$id = (int)$_COOKIE['id'];

If it's a string then you need to escape quotes. This can be done by using
the addslashes() function eg:
$string = addslashes($_POST['string']);

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #5
Noel Wood wrote:
I'm no expert but if the code is invoked via the url then you will need to
use the $_GET global variable to access it not the $_POST one you presently
are using Hope this helps


This makes sense, and it solves my problem. But I'm still a bit
confused, because in the script for updating entries (included below),
which is also invoked vith a URL, I'm also using POST to fetch the ID,
and it works.

When I use POST in the delete script, I get no error at all, not even
with the line

echo mysql_error();

or all the nice error code CJ Llewellyn provided. I take it as if GET is
right here, but can anyone explain why POST works in the update script?

<?
include("dbinfo.inc.php");

$id = $_POST['id'];
$dt_created = $_POST['dt_created'];
$dt_updated = strftime('%Y-%m-%d %H:%M:%S', time());
$title = $_POST['title'];
$content = $_POST['content'];
$status = $_POST['status'];

mysql_connect($server, $user, $password);
$query = "UPDATE test1 SET dt_created = '$dt_created', dt_updated =
'$dt_updated', title = '$title', content = '$content', status =
'$status' WHERE id = '$id'";
@mysql_select_db($database) or die("Unable to select database.");
mysql_query($query);
mysql_close();

// Back to the entries page
header("Location: entries.php");
?>

I'll make it proof to SQL injection too. Thanks.

Gustaf
Jul 17 '05 #6
CJ Llewellyn wrote:
"Gustaf Liljegren" <gu*****@algonet.se> wrote in message
news:cf**********@green.tninet.se...
I've been learning using PHP and MySQL from the tutorial at
freewebmasterhelp.com. Everything works fine, except deleting things.
The script is invoked with a URL like this:

http://www.example.org/test/delete_entry.php?id=35

And here's the script:

<?
include("dbinfo.inc.php");

$id = $_POST['id'];
mysql_connect($server, $user, $password);
$query = "DELETE FROM test1 WHERE id = '$id'";
@mysql_select_db($database) or die("Unable to select database.");
mysql_query($query);
mysql_close();

header("Location: entries.php");
?>


If the examples are anything like this then I suggest that you look
elsewhere for free tutorials. Only one statement in the above code attempts
to detect and handle errors (and not very nicely at that), no wonder you are
having trouble.

<?php

include("dbinfo.inc.php");

$id = $_POST['id'];
if($conn = mysql_connect($server, $user, $password))


.....um....actually, shouldn't you have "==" instead of "=" ???
;-)

Jul 17 '05 #7
"Westcoast Sheri" <sh*********@nospamun8nospam.com> wrote in message
news:41**************@nospamun8nospam.com...
CJ Llewellyn wrote:

-snip-
<?php

include("dbinfo.inc.php");

$id = $_POST['id'];
if($conn = mysql_connect($server, $user, $password))


....um....actually, shouldn't you have "==" instead of "=" ???
;-)


if mysql_connect assigns a value to conn then the condition is true.

;-)

Since $conn doesn't exist before the connect statement, it is unlikely to
ever equal the result of mysql_connect unless the connection fails.
Jul 17 '05 #8
CJ Llewellyn wrote:
"Westcoast Sheri" <sh*********@nospamun8nospam.com> wrote in message
news:41**************@nospamun8nospam.com...
CJ Llewellyn wrote:

-snip-
<?php

include("dbinfo.inc.php");

$id = $_POST['id'];
if($conn = mysql_connect($server, $user, $password))


....um....actually, shouldn't you have "==" instead of "=" ???
;-)


if mysql_connect assigns a value to conn then the condition is true.

;-)

Since $conn doesn't exist before the connect statement, it is unlikely to
ever equal the result of mysql_connect unless the connection fails.


Well...just so I wouldn't "speak to soon" I did a little test. I did this
with a "correct" user, server, and password, and then I did it by using an
incorrect password, etc.:

if($conn = mysql_connect($server, $user, $password)) {
echo "yes";
}

It echoed "yes" both times.

;-)
Jul 17 '05 #9
"Westcoast Sheri" <sh*********@nospamun8nospam.com> wrote in message
news:41***************@nospamun8nospam.com...
CJ Llewellyn wrote:
"Westcoast Sheri" <sh*********@nospamun8nospam.com> wrote in message
news:41**************@nospamun8nospam.com...
CJ Llewellyn wrote:

-snip-
> <?php
>
> include("dbinfo.inc.php");
>
> $id = $_POST['id'];
> if($conn = mysql_connect($server, $user, $password))
>

....um....actually, shouldn't you have "==" instead of "=" ???
;-)


if mysql_connect assigns a value to conn then the condition is true.

;-)

Since $conn doesn't exist before the connect statement, it is unlikely to ever equal the result of mysql_connect unless the connection fails.


Well...just so I wouldn't "speak to soon" I did a little test. I did this
with a "correct" user, server, and password, and then I did it by using an
incorrect password, etc.:

if($conn = mysql_connect($server, $user, $password)) {
echo "yes";
}

It echoed "yes" both times.


DOH! Just goes to show you should always test before posting :-)

Jul 17 '05 #10
CJ Llewellyn wrote:
"Westcoast Sheri" <sh*********@nospamun8nospam.com> wrote in message
news:41***************@nospamun8nospam.com...
CJ Llewellyn wrote:
"Westcoast Sheri" <sh*********@nospamun8nospam.com> wrote in message
news:41**************@nospamun8nospam.com...
> CJ Llewellyn wrote:
-snip-
> > <?php
> >
> > include("dbinfo.inc.php");
> >
> > $id = $_POST['id'];
> > if($conn = mysql_connect($server, $user, $password))
> >
>
> ....um....actually, shouldn't you have "==" instead of "=" ???
> ;-)

if mysql_connect assigns a value to conn then the condition is true.

;-)

Since $conn doesn't exist before the connect statement, it is unlikely to ever equal the result of mysql_connect unless the connection fails.


Well...just so I wouldn't "speak to soon" I did a little test. I did this
with a "correct" user, server, and password, and then I did it by using an
incorrect password, etc.:

if($conn = mysql_connect($server, $user, $password)) {
echo "yes";
}

It echoed "yes" both times.


DOH! Just goes to show you should always test before posting :-)


Okay....now if you could help on one tiny little problem: I *don't* want php to
display error messages to my browser. I don't have access to server compile
files, so in the past simply placing "error_reporting(E_NONE);" at the top of
my script worked. Now it doesn't work. What am I doing wrong? Or does that
directive work on some servers but not others???
Jul 17 '05 #11
Westcoast Sheri wrote:
Okay....now if you could help on one tiny little problem: I *don't* want php to
display error messages to my browser. I don't have access to server compile
files, so in the past simply placing "error_reporting(E_NONE);" at the top of
my script worked. Now it doesn't work. What am I doing wrong? Or does that
directive work on some servers but not others???


Try:

ini_set('display_errors','0');

at the start of your php script.

--
Jasper Bryant-Greene
Cabbage Promotions
Jul 17 '05 #12
"Westcoast Sheri" <sh*********@nospamun8nospam.com> wrote in message
news:41***************@nospamun8nospam.com...
CJ Llewellyn wrote:
"Westcoast Sheri" <sh*********@nospamun8nospam.com> wrote in message
news:41***************@nospamun8nospam.com...
CJ Llewellyn wrote:

> "Westcoast Sheri" <sh*********@nospamun8nospam.com> wrote in message
> news:41**************@nospamun8nospam.com...
> > CJ Llewellyn wrote:
> -snip-
> > > <?php
> > >
> > > include("dbinfo.inc.php");
> > >
> > > $id = $_POST['id'];
> > > if($conn = mysql_connect($server, $user, $password))
> > >
> >
> > ....um....actually, shouldn't you have "==" instead of "=" ???
> > ;-)
>
> if mysql_connect assigns a value to conn then the condition is true.
>
> ;-)
>
> Since $conn doesn't exist before the connect statement, it is unlikely
to
> ever equal the result of mysql_connect unless the connection fails.

Well...just so I wouldn't "speak to soon" I did a little test. I did
this with a "correct" user, server, and password, and then I did it by using an incorrect password, etc.:

if($conn = mysql_connect($server, $user, $password)) {
echo "yes";
}

It echoed "yes" both times.


DOH! Just goes to show you should always test before posting :-)


Okay....now if you could help on one tiny little problem: I *don't* want

php to display error messages to my browser. I don't have access to server compile files, so in the past simply placing "error_reporting(E_NONE);" at the top of my script worked. Now it doesn't work. What am I doing wrong? Or does that
directive work on some servers but not others???

Jul 17 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: NotGiven | last post by:
I'd like to delete a record and all its children records at one time. How do I do that? Can you, in one SQL statement, delete from table 1 where id = 3 delete from table 2 where id = 12...
4
by: Chris | last post by:
Hi, sorry to post OT but i cant find the MySQL newsgroup, however i am hoping to pick up on some expert advice from php/mysql gurus here. I'm having some trouble performing a delete across two...
0
by: Gordon | last post by:
I have 2 tables t and t1. In this case, t1 is a copy of t. I want to delete rows from t1 based on criteria on the t table and a relationship between t ad t1 (in this case the id column). In the...
2
by: michael | last post by:
Gotta post because this is driving me nuts. Trying to DELETE orphans. I can successfully: SELECT GroupID FROM Groups LEFT JOIN Users ON UsersID = UserID WHERE UsersID IS NULL; but when I...
1
by: Andrew DeFaria | last post by:
I created the following .sql file to demonstrate a problem I'm having. According to the manual: If |ON DELETE CASCADE| is specified, and a row in the parent table is deleted, then InnoDB...
5
by: Mike | last post by:
I am not a sql person and could use some help with a delete...here is what I want: I have the following tables/fields (only including necessary fields) answers result_id results result_id
3
by: saracen44 | last post by:
Hi I have MyISAM tables When I'm deleting parent I want to delete children in the same time. How can I do It? What are possibilities? Thanks
4
by: Stephen | last post by:
Hello People, Using MS Access 2003 VBA I get the error 3020 Update or CancelUpdate without AddNew or Edit when I run through the following code. Can anyone help suggest anything to try? Thanks....
8
by: starman7 | last post by:
i have a table with objects in categories and their positions. there will be several rows with category 400, and they will have various positions, i want to delete only the row with the lowest...
9
by: Dejan | last post by:
Hy, Sorry for my terreble english I have this simple code for deleting rows in mysql table... Everything works fine with it. So, what do i wanna do...: my sql table looks something like...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
1
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,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.