2016-07-06 33 views
1

當我嘗試發送消息給其他用戶,然後我得到消息,沒有這樣的用戶存在,但實際上他們是我的數據庫中的用戶我不知道問題在哪裏。發現從互聯網這個腳本.... plz幫助我此評論 //我們檢查,如果收件人不屬於實際用戶錯誤在我的SQL SELECT語句檢查用戶是否存在

<?php 
//We check if the user is logged 
if(isset($_SESSION['username'])) 
{ 
$form = true; 
$otitle = ''; 
$orecip = ''; 
$omessage = ''; 
//We check if the form has been sent 
if(isset($_POST['title'], $_POST['recip'], $_POST['message'])) 
{ 
    $otitle = $_POST['title']; 
    $orecip = $_POST['recip']; 
    $omessage = $_POST['message']; 
    //We remove slashes depending on the configuration 
    if(get_magic_quotes_gpc()) 
    { 
     $otitle = stripslashes($otitle); 
     $orecip = stripslashes($orecip); 
     $omessage = stripslashes($omessage); 
    } 
    //We check if all the fields are filled 
    if($_POST['title']!='' and $_POST['recip']!='' and $_POST['message']!='') 
    { 
     //We protect the variables 
     $title = mysql_real_escape_string($otitle); 
     $recip = mysql_real_escape_string($orecip); 
     $message = mysql_real_escape_string(nl2br(htmlentities($omessage, ENT_QUOTES, 'UTF-8'))); 
     //We check if the recipient exists 
     $dn1 = mysql_fetch_array(mysql_query('select count(id) as recip, id as recipid, (select count(*) from pm) as npm from users where username="'.$recip.'"')); 
     if($dn1['recip']==1) 
     { 
      //We check if the recipient is not the actual user 
      if($dn1['recipid']!=$_SESSION['userid']) 
      { 
       $id = $dn1['npm']+1; 
       //We send the message 
       if(mysql_query('insert into pm (id, id2, title, user1, user2, message, timestamp, user1read, user2read)values("'.$id.'", "1", "'.$title.'", "'.$_SESSION['userid'].'", "'.$dn1['recipid'].'", "'.$message.'", "'.time().'", "yes", "no")')) 
       { 
?> 
<div class="message">The message has successfully been sent.<br /> 
<a href="list_pm.php">List of my personnal messages</a></div> 
<?php 
        $form = false; 
       } 
       else 
       { 
        //Otherwise, we say that an error occured 
        $error = 'An error occurred while sending the message'; 
       } 
      } 
      else 
      { 
       //Otherwise, we say the user cannot send a message to himself 
       $error = 'You cannot send a message to yourself.'; 
      } 
     } 
     else 
     { 
      //Otherwise, we say the recipient does not exists 
      $error = 'The recipient does not exists.'; 
     } 
    } 
    else 
    { 
     //Otherwise, we say a field is empty 
     $error = 'A field is empty. Please fill of the fields.'; 
    } 
} 
elseif(isset($_GET['recip'])) 
{ 
    //We get the username for the recipient if available 
    $orecip = $_GET['recip']; 
} 
if($form) 
{ 
//We display a message if necessary 
if(isset($error)) 
{ 
    echo '<div class="message">'.$error.'</div>'; 
} 
//We display the form 
?> 
<div class="content"> 
    <h1>New Personnal Message</h1> 
    <form action="new_pm.php" method="post"> 
     Please fill the following form to send a personnal message.<br /> 
     <label for="title">Title</label><input type="text" value="<?php echo htmlentities($otitle, ENT_QUOTES, 'UTF-8'); ?>" id="title" name="title" /><br /> 
     <label for="recip">Recipient<span class="small">(Username)</span></label><input type="text" value="<?php echo htmlentities($orecip, ENT_QUOTES, 'UTF-8'); ?>" id="recip" name="recip" /><br /> 
     <label for="message">Message</label><textarea cols="40" rows="5" id="message" name="message"><?php echo htmlentities($omessage, ENT_QUOTES, 'UTF-8'); ?></textarea><br /> 
     <input type="submit" value="Send" /> 
    </form> 
</div> 
<?php 
} 
} 
else 
{ 
    echo '<div class="message">You must be logged to access this page.</div>'; 
} 
?> 
+0

你有重複的'username'嗎? – Deepak

+0

沒有他們是沒有重複的用戶名,爲此我只在我的數據庫中創建了兩個用戶 –

+0

什麼是你得到確切的錯誤信息? – Deepak

回答

1

您遇到的問題與您的查詢以及mysql函數的用法有關。

$dn1 = mysql_fetch_array(mysql_query('select count(id) as recip, id as recipid, (select count(*) from pm) as npm from users where username="'.$recip.'"')); 

到:

從改變你的代碼

$query_result = mysql_query('SELECT COUNT(id) AS recip, 
            id AS recipid, 
            (SELECT COUNT(*) from pm) AS npm 
          FROM users 
          WHERE username="'.$recip.'" 
          GROUP BY id') or die(mysql_error()); 
$dn1 = mysql_fetch_assoc($query_result); 

注:查詢可以得到改進,也不要使用mysql_*。他們正在使用PHP的最新版本。使用mysqli_*代替

+0

替換到您的代碼後,這是我得到的消息,我已經嘗試過此前「混合組列(MIN(),MAX(),COUNT( ),...)沒有GROUP列是非法的,如果沒有GROUP BY子句「 –

+0

cehck my edit .. – Deepak

+0

nope still said it」Can not on'recip'「 –

0

我不知道這是答案,但$ DN1查詢有一個聚合函數,但它沒有group by子句。