Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 1st, 2003, 06:43 AM
Authorized User
 
Join Date: Jul 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Delete record but how?

I have another problem now. I cannot delete a record in table “doctors”. The name of the field that I specify is “doctor”. My code is here:
<html>
<head>
<title>Deleting Record</title> </head>
<body>
<?php
// Set the variables for the database access:
$Host = "***";
$DBName = "***";
$TableName = "Doctors";
$Link = mysql_connect ($Host, '***', '***'); if ($Link==false) {
echo mysql_errno().": ".mysql_error()."<BR>\n";
}
mysql_select_db ($DBName, $Link); $Query = "DELETE FROM $Tablename WHERE Doctor = "ppp""; if (mysql_query ($Query, $Link)) {
     print "The query was successfully executed!<BR>\n"; }
else { print "The query could not be executed!<BR>\n";
    print mysql_errno().": ".mysql_error()."<BR>\n"; }
?>
</body> </html> I receive the error message
Parse error: parse error in c:\***\***\***\deleterow.php on line 17
Line 17 is: $Query = "DELETE FROM $Tablename WHERE Doctor = "ppp"";
When I change that line to
$Query = "DELETE FROM $Tablename WHERE Doctor = ‘ppp’";
I receive this error message:
Warning: Undefined variable: Tablename in c:\***\***\***\deleterow.php on line 17
The query could not be executed!
1064: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE Doctor = 'ppp'' at line 1

I could not understand what is wrong here. Help me pls.

MEG
 
Old August 1st, 2003, 10:20 AM
Authorized User
 
Join Date: Jul 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

SORTED Thanx.

 
Old August 1st, 2003, 11:56 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

You have another problem with quotes.

$Query = "DELETE FROM $Tablename WHERE Doctor = 'ppp'"

If you are using double quotes around the outer string, then you should use single quotes around the inner strings which construct the SQL syntax.

Variable names are also case sensitive,

So if you are using $Tablename in the query command, it isn't the same as $TableName as you have it defined.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old August 1st, 2003, 12:14 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Also, some observations:
Your code is hard to sort through when it isn't formatted to be pleasant to the eye, indent your code - or at the very least double space it!

Stay consistent with your variable names! I think capitalization is a bad idea, because then if you misspell a variable, you're all day trying to figure out what went wrong. As was the case in $Tablename. I like all lowercase with underscores to separate words. But that's just me. Pick a standard and stay with it.

Code:
<?php
// Set the variables for the database access:

$Host = "***";
$DBName = "***";
$TableName = "Doctors";
$Link = mysql_connect ($Host, '***', '***'); 

     if ($Link==false) {

          echo mysql_errno().": ".mysql_error()."<br />\n";

     }

mysql_select_db ($DBName, $Link); 
$Query = "DELETE FROM $Tablename WHERE Doctor = "ppp""; 

if (mysql_query ($Query, $Link)) {

     print "The query was successfully executed!<br />\n"; 

} else {      

    print "The query could not be executed!<br />\n";
    print mysql_errno().": ".mysql_error()."<br />\n"; 

} 

?>
The <BR> tag that you use is also deprecated under HTML 4.01. Deprecated means slated for deletion from the standard. Under a strict docutype setting that tag should fail.

See this article on how to do it right:
http://p2p.wrox.com/topic.asp?TOPIC_ID=2251

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::





Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I delete a record from a form? dstein4d Access VBA 8 August 24th, 2007 12:09 PM
Delete a record row, not just the record. Coby Access VBA 1 April 30th, 2007 06:29 AM
Trying to delete a record... Can't do it... lguzman Access VBA 11 August 13th, 2004 12:41 PM
automatic record delete. joeore Beginning PHP 5 April 29th, 2004 11:27 AM
Can't delete record Trojan_uk SQL Server 2000 3 November 27th, 2003 01:03 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.