Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

select & delete mysql queries

General discussions related to php

Moderators: macek, egami, gesf

select & delete mysql queries

Postby superman » Fri Jul 18, 2003 7:37 pm

i've try to insert and delete record at the same page, but it only can insert and can't delete the record.
when i click submit button from the previous form, the record is insert into table B and delete from the table A.
is the code correct?


<? $Connect = mysql_connect("localhost","root","");
mysql_select_db("mw");
$TodayDate = strftime("%Y-%m-%d");
$TodayTime = strftime("%H:%M:%S");
$result=mysql_query("insert into LeaveApplication1 (StaffID, LeaveType, JobTitle, FromDate, ToDate, Duration, Reason, TodayDate, TodayTime, Subjects1, DateAffected1, DateReplaced1, Subjects2, DateAffected2, DateReplaced2, Subjects3, DateAffected3, DateReplaced3, Subjects4, DateAffected4, DateReplaced4, Subjects5, DateAffected5, DateReplaced5, Subjects6, DateAffected6, DateReplaced6) values ('$StaffID', '$LeaveType', '$JobTitle', '$FromDate', '$ToDate', '$Duration', '$Reason', '$TodayDate', '$TodayTime', '$Subjects1', '$DateAffected1', '$DateReplaced1', '$Subjects2', '$DateAffected2', '$DateReplaced2', '$Subjects3', '$DateAffected3', '$DateReplaced3', '$Subjects4', '$DateAffected4', '$DateReplaced4', '$Subjects5', '$DateAffected5', '$DateReplaced5', '$Subjects6', '$DateAffected6', '$DateReplaced6')");
$result=mysql_query("delete from LeaveApplication where StaffID='$StaffID' and LeaveType='$LeaveType' and JobTitle='$JobTitle' and FromDate='$FromDate' and ToDate='$FromDate' and Duration='$Duration' and Reason='$Reason' and TodayDate='$TodayDate' and TodayTime='$TodayTime' and Subjects1='$Subjects1' and DateAffected1='$DateAffected1' and DateReplaced1='$DateReplaced1' and Subjects2='$Subjects2' and DateAffected2='$DateAffected2' and DateReplaced2='$DateReplaced2' and Subjects3='$Subjects3' and DateAffected3='$DateAffected3' and DateReplaced3='$DateReplaced3' and Subjects4='$Subjects4' and DateAffected4='$DateAffected4' and DateReplaced4='$DateReplaced4' and Subjects5='$Subjects5' and DateAffected5='$DateAffected5' and DateReplaced5='$DateReplaced5' and Subjects6='$Subjects6' and DateAffected6='$DateAffected6' and DateReplaced6='$DateReplaced6'");
mysql_close();
?>
superman
New php-forum User
New php-forum User
 
Posts: 36
Joined: Fri Jun 27, 2003 11:54 pm

Postby swirlee » Fri Jul 18, 2003 9:47 pm

I'm sorry, but I found your code so illegible that I couldn't even start trying to answer your question. You may scoff now, but code readability is a big deal if you ever intend to write any PHP application collaboratively or with more than a couple hundred lines. You put a 787-byte string on a single line. Ow.

Okay, so predictably enough, I took the time to reformat your code that you (and people who'll potentially help you out) will still be able to read and comprehend two weeks from now. Since it's still to big to burden this message with, I'll just post a convenient link:

http://www.swirlee.org/misc/php-forum/something2.php

Highlighted for your reading enjoyment.

Okay, now on to your problem. Your MySQL syntax is correct. You're not doing anything wrong so far as I can tell in order to do two queries. My advice is this: Don't run your MySQL queries through PHP until you know they work! So, in your PHP script, instead of mysql_query, just echo the query in question. Now copy the echoed query to the clipboard and open up the MySQL console (mysql.exe, try mysql --help from the command prompt if you've never used it before). Paste in your query. If it works, you know it's a problem with PHP. If it doesn't work, you know it's a problem with your MySQL query.

I can say this, though: Your gigantic MySQL queries look far too complicated to be useful. If every row has a unique key (StaffID?), then your DELETE command should only be about seven words long:

DELETE FROM LeaveApplication WHERE StaffID = $StaffID;

If every row doesn't have a unique key, then you need to start considering why it doesn't and whether or not it should. Here's a hint: It almost always should.

I just have one more question: Your code appears to be inserting a row and then (attempting to) immediately delete it. How is this useful?
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby bezmond » Sat Jul 19, 2003 12:10 am

I just have one more question: Your code appears to be inserting a row and then (attempting to) immediately delete it. How is this useful?


I'm guessing he's asking for help on queries like those in general...

Andrew
User avatar
bezmond
Moderator
Moderator
 
Posts: 312
Joined: Sat Apr 05, 2003 4:33 am
Location: Mansfield, UK

Re: select & delete mysql queries

Postby WiZARD » Sat Jul 19, 2003 12:35 am

Hi!
i'm propose write code like you know what wrong, if you get error..... cause correct error may tell to you all info where you wrong.....
http://ua.php.net/mysql_query
Just example:
Code: Select all
<php
$result = mysql_query("SELECT * WHERE 1=1")
    or die("Invalid query: " . mysql_error());
?>
User avatar
WiZARD
Moderator
Moderator
 
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol

Postby superman » Sat Jul 19, 2003 11:55 pm

thanx!
superman
New php-forum User
New php-forum User
 
Posts: 36
Joined: Fri Jun 27, 2003 11:54 pm


Return to PHP General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron