Jump to content

Messaging Script


Ambuu

Recommended Posts

Alright, now I need help with a simple messaging script for my site. The problem is that I can echo $_GET['id'] (from the url below), and it will come up as "2". However, when I try to send a message to the person (whose id is 2), I'll recieve one of two error messages.

 

the url is basically http://localhost/test1/send_pm.php?id=2

 

Here is the script (minus the database connection):


    if($do_send == 1) {
      if($_POST[subject] == "" OR $_POST[message] == "") {
        $output = "You did not fill in all of the required fields. Please go back and try again.";
      }else{
        $_POST[subject] = strip_tags(trim($_POST[subject]));
        if(strlen($_POST[subject]) > 20) {
          $_POST[subject] = substr($_POST[subject], 0, 20);
        }
        if(strlen($_POST[message]) > 255) {
          $output = "Your message must be less than 255 characters. At the moment it is " . strlen($_POST[message]) . " characters long.";
        }else{
          $_POST[message] = strip_tags(trim($_POST[message]));
          $time = time();
          $sql = "INSERT INTO pm
                  (pm_from, pm_to, message, subject, date, seen)
                  VALUES
                  ('$_COOKIE[id]', '$_POST[id]', '$_POST[message]', '$_POST[subject]', '$time', '0')";
          $result = mysql_query($sql);
          $output = "Your message has been sent!";
        }
      }
    }else{

        if(!isset($_GET['id'])) {
          $output = "Invalid User ID: This user may have deleted his/her account. (1)";
        }
                                                 
        $userid = $_GET['id'];
        $sql = "SELECT user FROM user
                WHERE user.id='$userid'";
        $result = mysql_query($sql);
        $user = mysql_fetch_assoc($result);

        if(mysql_num_rows($result) == 0) {
          $output = "Invalid User ID: This user may have deleted his/her account. (2)";
        }else{
?>

(there is some html stuff inbetween that doesn't affect the form)

<?php
          echo "<b>To:</b> " . $user[user];
          echo "<br><br>";
          echo "<form action='send_pm.php' method='POST'>";
          echo "<input type='hidden' name='do_send' value='1'>";
          echo "<input type='hidden' name='id' value='" . $_GET[id] . "'>";
          echo "<b>Subject:</b><br><input type='text' name='subject' size='20' maxsize='20'>";
          echo "<br>";
          echo "<br>";
          echo "<br>";
          echo "<b>Message:</b><br><textarea rows='5' name='message' cols='20'></textarea>";
          echo "<br>";
          echo "<br>";
          echo "<input type='submit' value='Send'>";
          echo "</form>";
        }
      }
    }

?>

 

 

The two error messages are (1) and (2).  The main problem looks like:

$userid = $_GET['id'];
        $sql = "SELECT user FROM user
                WHERE user.id='$userid'";
        $result = mysql_query($sql);
        $user = mysql_fetch_assoc($result);

        if(mysql_num_rows($result) == 0) {
          $output = "Invalid User ID: This user may have deleted his/her account. (2)";
        }else{

 

How would I fix this problem?

Link to comment
https://forums.phpfreaks.com/topic/118514-messaging-script/
Share on other sites

Testing it on a blank page (blank.php?id=1), $num_rows returns "1".

 

$userid = $_GET['id'];

	$result = mysql_query("SELECT user FROM user WHERE user.id='$userid'") 
	or die(mysql_error());

	$num_rows = mysql_num_rows($result);
        $user = mysql_fetch_assoc($result);
echo $num_rows;
        if($num_rows == 0) {
          $output = "Invalid User ID: This user may have deleted his/her account. (2)";
        }

 

It echos "1", but as soon as I press send (on the form), it echos "0"

Link to comment
https://forums.phpfreaks.com/topic/118514-messaging-script/#findComment-610155
Share on other sites

if you put my little code here (below), what happens?

 


$userid = $_GET['id'];

/*
        $sql = "SELECT user FROM user
                WHERE user.id='$userid'";
        $result = mysql_query($sql);
        $user = mysql_fetch_assoc($result);
*/

$result = mysql_query("SELECT user FROM user WHERE user.id='$userid'") 
or die(mysql_error());

$num_rows = mysql_num_rows($result);

echo "$num_rows";
exit();



        if(mysql_num_rows($result) == 0) {
          $output = "Invalid User ID: This user may have deleted his/her account. (2)";
        }else{

 

If $num_rows returns '1', then the error message shouldn't run because $num_rows is not equal to 0

Link to comment
https://forums.phpfreaks.com/topic/118514-messaging-script/#findComment-610159
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.