jamesxg1 Posted February 8, 2009 Share Posted February 8, 2009 i am making a user to user messaging system but the delete query is not working :S, heres the inbox script <?php session_start(); require("../db/db.php"); //include database file require("../db/config.php"); //include configuration file require("../db/util.php"); require("../db/settings.php"); isloggedin(); accessneeded("C"); $user = $_SESSION['username']; //This checks to see if a user is logged in or not by seeing if the sessioned username varialble exists. //You could change this check to however you want to validate your members, this is just how I did it. { //Query the database to see how many messages the logged in user has, then do a little math //Find the percentage that your inbox is full (message count divided by 50) //50 messages maximum, you can change that $sql = mysql_query ("SELECT pm_count FROM users WHERE username='$user'"); $row = mysql_fetch_array ($sql); $pm_count = $row['pm_count']; //This is the math to figure out the percentage. //The message could divided by 50 then multiplied by 100 so we dont have a number less than 1 $percent = $pm_count/'50'; $percent = $percent * '100'; //Next we come out of PHP and show some HTML, the inbox fullness, links to the other message system pages, the messages, etc. ?> <br> <center> <b><p><a href="inbox.php">Inbox</a></b> <b><p><?php echo "$pm_count"." of 50 Total | "."$percent"."% full"; ?></p></b> </center> <br> <?php //This stuff and the while loop will query the database, see if you have messages or not, and display them if you do $query = "SELECT id, sender, subject, message FROM messages WHERE reciever='$user'"; $sqlinbox = mysql_query($query); //We have a mysql error, we should probably let somone know about the error, so we should print the error if(!$sqlinbox) { ?> <p><?php print '$query: '.$query.mysql_error();?></p> <?php } //There are no rows found for the user that is logged in, so that either means they have no messages or something broke, lets assume them they have no messages elseif (!mysql_num_rows($sqlinbox) ) { ?> <center><p><b>You have no messages to display</b></p></center> <?php } //There are no errors, and they do have messages, lets query the database and get the information after we make a table to put the information into else { //Ok, Lets center this whole table Im going to make just because I like it like that //Then we create a table 80% the total width, with 3 columns, The subject is 75% of the whole table, the sender is 120 pixels (should be plenty) and the select checkboxes only get 25 pixels ?> <P ALIGN=CENTER> <form name="send" method="post" action="delete.php"> <table width="80%"> <tr> <td width="75%" valign="top"><p><b><u>Subject</u></b></p></td> <td width="120px" valign="top"><p><b><u>Sender</u></b></p></td> <td width="25px" valign="top"><p><b><u>Select</u></b></p></td> </tr></P> <?php //Since everything is good so far and we earlier did a query to get all the message information we need to display the information. //This while loop goes through the array outputting all of the message information while($inbox = mysql_fetch_array($sqlinbox)) { //These are the variables we get from the array as it is going through the messages, we have the id of the private message, we have the person who sent the message, we have the subject of the message, and yeah thats it $pm_id = $inbox['id']; $sender = $inbox['sender']; $subject = $inbox['subject']; //So lets show the subject and make that a link to the view message page, we will send the message id through the URL to the view message page so the message can be displayed //And also let the person see who sent it to them, if you want you can make that some sort of a link to view more stuff about the user, but Im not doing that here, I did it for my game though, similar to the viewmsg.php page but a different page, and with the senders id //And finally the checkboxes that are all stuck into an array and if they are selected we stick the private message id into the array //I will only let my users have a maximum of 50 messages, remeber that ok? Because that's the value I will later in another page //Here is finally the html output for the message data, the while loop keeps going untill it runs out of messages ?><P ALIGN=CENTER> <tr> <td width="75%" valign="top"><p><a href="viewmsg.php?msg_id=<?php echo $pm_id; ?>"><?php echo $subject; ?></a></p></td> <td width="120px" valign="top"><p><?php echo $sender; ?></p></td> <td width="25px" valign="top"><td width="25px" valign="top"><input name="pms[]" type="checkbox" value="<?php print $pm_id; ?>"></td></td> </tr></P> <?php //This ends the while loop } //Here is a submit button for the form that sends the delete page the message ids in an array ?> <tr> <td colspan="3"><input type="submit" name="Submit" value="Delete Selected"></td> <td></td> <td><a href="#" class="lbAction" rel="deactivate">Cancel</a></td> </tr> </table> </center> <?php //So this ends the else to see if it is all ok and having messages or not } //This ends that first thing that checks if you are logged in or not } ?> and heres the delete <?php session_start(); require("../db/db.php"); //include database file require("../db/config.php"); //include configuration file require("../db/util.php"); require("../db/settings.php"); isloggedin(); accessneeded("C"); $user = $_SESSION['username']; //We do not have a user check on this page, because it seems silly to, you just send data to this page then it directs you right back to inbox //We need to get the total number of private messages the user has $sql = mysql_query ("SELECT pm_count FROM users WHERE username='$user'"); $row = mysql_fetch_array ($sql); $pm_count = $row['pm_count']; //Delete the PM from the database mysql_query("DELETE FROM `messages` WHERE `messages`.`id` = '$pm_id' AND reciever='$user' LIMIT 1"); //Subtract a private message from the counter! YAY! $pm_count = $pm_count - '1'; //Now update the users message count with the new value mysql_query("UPDATE users SET pm_count='$pm_count' WHERE username='$user'"); header("Location:inbox.php"); exit; ?> this is the checkbox they tick per message they want to delete <td width="25px" valign="top"><td width="25px" valign="top"><input name="pms[]" type="checkbox" value="<?php print $pm_id; ?>"></td></td> </tr></P> any ideas on why it isnt working :S ? Quote Link to comment https://forums.phpfreaks.com/topic/144342-delete-query-doesnt-work-any-help/ Share on other sites More sharing options...
gevans Posted February 8, 2009 Share Posted February 8, 2009 Where are you getting $pm_id from? Quote Link to comment https://forums.phpfreaks.com/topic/144342-delete-query-doesnt-work-any-help/#findComment-757409 Share on other sites More sharing options...
jamesxg1 Posted February 8, 2009 Author Share Posted February 8, 2009 Where are you getting $pm_id from? <?php $query = "SELECT id, sender, subject, message FROM messages WHERE reciever='$user'"; $sqlinbox = mysql_query($query); if(!$sqlinbox) { ?> <p><?php print '$query: '.$query.mysql_error();?></p> <?php } elseif (!mysql_num_rows($sqlinbox) ) { ?> <center><p><b>You have no messages to display</b></p></center> <?php } //There are no errors, and they do have messages, lets query the database and get the information after we make a table to put the information into else { ?> <P ALIGN=CENTER> <form name="send" method="post" action="delete.php?id=$pm_id"> <table width="80%"> <tr> <td width="75%" valign="top"><p><b><u>Subject</u></b></p></td> <td width="120px" valign="top"><p><b><u>Sender</u></b></p></td> <td width="25px" valign="top"><p><b><u>Select</u></b></p></td> </tr></P> <?php while($inbox = mysql_fetch_array($sqlinbox)) { $pm_id = $inbox['id']; $sender = $inbox['sender']; $subject = $inbox['subject']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/144342-delete-query-doesnt-work-any-help/#findComment-757410 Share on other sites More sharing options...
gevans Posted February 8, 2009 Share Posted February 8, 2009 But on the page run you run the query, you haven't assigned it from anywhere Quote Link to comment https://forums.phpfreaks.com/topic/144342-delete-query-doesnt-work-any-help/#findComment-757413 Share on other sites More sharing options...
gevans Posted February 8, 2009 Share Posted February 8, 2009 After this line.. $user = $_SESSION['username']; put... $pm_id = (int) $_GET['id']; Quote Link to comment https://forums.phpfreaks.com/topic/144342-delete-query-doesnt-work-any-help/#findComment-757415 Share on other sites More sharing options...
jamesxg1 Posted February 8, 2009 Author Share Posted February 8, 2009 After this line.. $user = $_SESSION['username']; put... $pm_id = (int) $_GET['id']; still got nothing Quote Link to comment https://forums.phpfreaks.com/topic/144342-delete-query-doesnt-work-any-help/#findComment-757417 Share on other sites More sharing options...
jamesxg1 Posted February 8, 2009 Author Share Posted February 8, 2009 heres the full inbox.php code (iv taken all the text out so it is easy to read) <?php session_start(); require("../db/db.php"); //include database file require("../db/config.php"); //include configuration file require("../db/util.php"); require("../db/settings.php"); isloggedin(); accessneeded("C"); $user = $_SESSION['username']; $pm_id = (int) $_GET['id']; { $sql = mysql_query ("SELECT pm_count FROM users WHERE username='$user'"); $row = mysql_fetch_array ($sql); $pm_count = $row['pm_count']; $percent = $pm_count/'50'; $percent = $percent * '100'; ?> <br> <center> <b><p><a href="inbox.php">Inbox</a></b> <b><p></p></b> </center> <br> <?php $query = "SELECT id, sender, subject, message FROM messages WHERE reciever='$user'"; $sqlinbox = mysql_query($query); if(!$sqlinbox) { ?> <p><?php print '$query: '.$query.mysql_error();?></p> <?php } elseif (!mysql_num_rows($sqlinbox) ) { ?> <center><p><b>You have no messages to display</b></p></center> <?php } else { ?> <P ALIGN=CENTER> <form name="send" method="post" action="delete.php"> <table width="80%"> <tr> <td width="75%" valign="top"><p><b><u>Subject</u></b></p></td> <td width="120px" valign="top"><p><b><u>Sender</u></b></p></td> <td width="25px" valign="top"><p><b><u>Select</u></b></p></td> </tr></P> <?php while($inbox = mysql_fetch_array($sqlinbox)) { $pm_id = $inbox['id']; $sender = $inbox['sender']; $subject = $inbox['subject']; ?> <P ALIGN=CENTER> <tr> <td width="75%" valign="top"><p><a href="viewmsg.php?msg_id=<?php echo $pm_id; ?>"><?php echo $subject; ?></a></p></td> <td width="120px" valign="top"><p><?php echo $sender; ?></p></td> <td width="25px" valign="top"><td width="25px" valign="top"><input name="pms[]" type="checkbox" value="<?php print $pm_id; ?>"></td></td> </tr></P> <?php } ?> <tr> <td colspan="3"><input type="submit" name="Submit" value="Delete Selected"></td> <td></td> <td><a href="#" class="lbAction" rel="deactivate">Cancel</a></td> </tr> </table> </center> <?php } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/144342-delete-query-doesnt-work-any-help/#findComment-757420 Share on other sites More sharing options...
gevans Posted February 8, 2009 Share Posted February 8, 2009 OK, lets see what you're actually executing change these lines; //Delete the PM from the database mysql_query("DELETE FROM `messages` WHERE `messages`.`id` = '$pm_id' AND reciever='$user' LIMIT 1"); to //Delete the PM from the database $query = "DELETE FROM `messages` WHERE `messages`.`id` = '$pm_id' AND reciever='$user' LIMIT 1"; mysql_query($query) or die("MySQL Error: ".mysql_error()); die($query); And post what was printed to screen. Quote Link to comment https://forums.phpfreaks.com/topic/144342-delete-query-doesnt-work-any-help/#findComment-757422 Share on other sites More sharing options...
gevans Posted February 8, 2009 Share Posted February 8, 2009 Ahhhh.... this line $pm_id = (int) $_GET['id']; should be on the page that the delete query is executed, no where you have it Quote Link to comment https://forums.phpfreaks.com/topic/144342-delete-query-doesnt-work-any-help/#findComment-757424 Share on other sites More sharing options...
jamesxg1 Posted February 8, 2009 Author Share Posted February 8, 2009 Ahhhh.... this line $pm_id = (int) $_GET['id']; should be on the page that the delete query is executed, no where you have it ok done all thats displayed is this DELETE FROM `messages` WHERE `messages`.`id` = '0' AND reciever='jamesxg1' LIMIT 1 but i tryed to delete the message id 42 not 0 :S Quote Link to comment https://forums.phpfreaks.com/topic/144342-delete-query-doesnt-work-any-help/#findComment-757437 Share on other sites More sharing options...
gevans Posted February 8, 2009 Share Posted February 8, 2009 Ahhhh.... this line $pm_id = (int) $_GET['id']; should be on the page that the delete query is executed, no where you have it Did you do that on the right page? Quote Link to comment https://forums.phpfreaks.com/topic/144342-delete-query-doesnt-work-any-help/#findComment-757439 Share on other sites More sharing options...
jamesxg1 Posted February 8, 2009 Author Share Posted February 8, 2009 Ahhhh.... this line $pm_id = (int) $_GET['id']; should be on the page that the delete query is executed, no where you have it Did you do that on the right page? i belive so :s, Delete.php <?php session_start(); require("../db/db.php"); //include database file require("../db/config.php"); //include configuration file require("../db/util.php"); require("../db/settings.php"); isloggedin(); accessneeded("C"); $user = $_SESSION['username']; $pm_id = (int) $_GET['id']; //We do not have a user check on this page, because it seems silly to, you just send data to this page then it directs you right back to inbox //We need to get the total number of private messages the user has $sql = mysql_query ("SELECT pm_count FROM users WHERE username='$user'"); $row = mysql_fetch_array ($sql); $pm_count = $row['pm_count']; //Delete the PM from the database $query = "DELETE FROM `messages` WHERE `messages`.`id` = '$pm_id' AND reciever='$user' LIMIT 1"; mysql_query($query) or die("MySQL Error: ".mysql_error()); die($query); //Subtract a private message from the counter! YAY! $pm_count = $pm_count - '1'; //Now update the users message count with the new value mysql_query("UPDATE users SET pm_count='$pm_count' WHERE username='$user'"); header("Location:inbox.php"); exit; ?> Quote Link to comment https://forums.phpfreaks.com/topic/144342-delete-query-doesnt-work-any-help/#findComment-757441 Share on other sites More sharing options...
gevans Posted February 8, 2009 Share Posted February 8, 2009 change this; $user = $_SESSION['username']; $pm_id = (int) $_GET['id']; to $user = $_SESSION['username']; $pm_id = (int) $_GET['id']; die($_GET['id'] .' '. $pm_id); That will show us what is passing to the page Quote Link to comment https://forums.phpfreaks.com/topic/144342-delete-query-doesnt-work-any-help/#findComment-757444 Share on other sites More sharing options...
printf Posted February 8, 2009 Share Posted February 8, 2009 But $_GET['id'] will equal '' because in your form you assign $pm_id before it's even set by the query that returns it... <form name="send" method="post" action="delete.php?id=$pm_id"> Quote Link to comment https://forums.phpfreaks.com/topic/144342-delete-query-doesnt-work-any-help/#findComment-757445 Share on other sites More sharing options...
jamesxg1 Posted February 8, 2009 Author Share Posted February 8, 2009 But $_GET['id'] will equal '' because in your form you assign $pm_id before it's even set by the query that returns it... <form name="send" method="post" action="delete.php?id=$pm_id"> that works but i cant use it, it seems a little vunrable :S, as then a user could just change the id to any other id and then it would delete the message :S Quote Link to comment https://forums.phpfreaks.com/topic/144342-delete-query-doesnt-work-any-help/#findComment-757450 Share on other sites More sharing options...
gevans Posted February 8, 2009 Share Posted February 8, 2009 View source on that page... You'll see that pm id isn't set in your html Quote Link to comment https://forums.phpfreaks.com/topic/144342-delete-query-doesnt-work-any-help/#findComment-757452 Share on other sites More sharing options...
jamesxg1 Posted February 8, 2009 Author Share Posted February 8, 2009 View source on that page... You'll see that pm id isn't set in your html true for some reason the pm_id is set to 0 and the query isnt even getting the id from the inbox page Quote Link to comment https://forums.phpfreaks.com/topic/144342-delete-query-doesnt-work-any-help/#findComment-757455 Share on other sites More sharing options...
gevans Posted February 8, 2009 Share Posted February 8, 2009 But $_GET['id'] will equal '' because in your form you assign $pm_id before it's even set by the query that returns it... <form name="send" method="post" action="delete.php?id=$pm_id"> Quote Link to comment https://forums.phpfreaks.com/topic/144342-delete-query-doesnt-work-any-help/#findComment-757462 Share on other sites More sharing options...
jamesxg1 Posted February 8, 2009 Author Share Posted February 8, 2009 But $_GET['id'] will equal '' because in your form you assign $pm_id before it's even set by the query that returns it... <form name="send" method="post" action="delete.php?id=$pm_id"> Basically all i need is the inbox.php to post the $pm_id from the checkboxes that are ticked and for delete.php to get the and query them. Quote Link to comment https://forums.phpfreaks.com/topic/144342-delete-query-doesnt-work-any-help/#findComment-757484 Share on other sites More sharing options...
gevans Posted February 8, 2009 Share Posted February 8, 2009 Yes, but you're setting it in the form field before the value has been assigned to $pm_id. You're using $pm_id before it has a value Quote Link to comment https://forums.phpfreaks.com/topic/144342-delete-query-doesnt-work-any-help/#findComment-757487 Share on other sites More sharing options...
jamesxg1 Posted February 8, 2009 Author Share Posted February 8, 2009 Yes, but you're setting it in the form field before the value has been assigned to $pm_id. You're using $pm_id before it has a value ok, how do i change this im a littlr confused :s Quote Link to comment https://forums.phpfreaks.com/topic/144342-delete-query-doesnt-work-any-help/#findComment-757503 Share on other sites More sharing options...
gevans Posted February 8, 2009 Share Posted February 8, 2009 Check the variable is being set in the form before sending it. If not check that you're getting it from the query Quote Link to comment https://forums.phpfreaks.com/topic/144342-delete-query-doesnt-work-any-help/#findComment-757509 Share on other sites More sharing options...
jamesxg1 Posted February 8, 2009 Author Share Posted February 8, 2009 Check the variable is being set in the form before sending it. If not check that you're getting it from the query in the code it has this : <?php while($inbox = mysql_fetch_array($sqlinbox)) { $pm_id = $inbox['id']; $sender = $inbox['sender']; $subject = $inbox['subject']; ?> before the checkbox is displayed doesnt this get the id ? Quote Link to comment https://forums.phpfreaks.com/topic/144342-delete-query-doesnt-work-any-help/#findComment-757523 Share on other sites More sharing options...
gevans Posted February 8, 2009 Share Posted February 8, 2009 yes, but what is being assigned, check that you're getting the right number from your query Quote Link to comment https://forums.phpfreaks.com/topic/144342-delete-query-doesnt-work-any-help/#findComment-757526 Share on other sites More sharing options...
jamesxg1 Posted February 9, 2009 Author Share Posted February 9, 2009 yes, but what is being assigned, check that you're getting the right number from your query if i print the var ($pm_id) it shows me the correct id's wich makes me belive that it could be the checkbox that is causing the problem just a guess thoe :S Quote Link to comment https://forums.phpfreaks.com/topic/144342-delete-query-doesnt-work-any-help/#findComment-757815 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.