dongzhan1383 2014-09-29 08:18
浏览 35
已采纳

@mention和文本或没有@mention和文本

I'm having a little issue with my php file and wondering if somebody can take a look. If I update with an @mention and some text with the php below it will update the database and output the ajax. If it doesn't have an @mention and just text, it outputs nothing. How can I rectify the code to do both.

They are both contained with $_POST['newmsg'];

I have yet to escape my variables to prevent SQL injection.

PHP:

<?php
   error_reporting(E_ALL); ini_set('display_errors', 1);
   error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
   session_start();
   require_once "rawfeeds_load.php";
   include_once "include/functions.youtube.php";
?>
<?
   if(isset($_SESSION['id'])){
     $user1_id=mysqli_real_escape_string($mysqli,$_SESSION['id']);
     if(isset($_POST['toid'])){
      if($_POST['toid']==""){$_POST['toid']=$_SESSION['id'];}
       if(isset($_POST['newmsg'])&& isset($_POST['toid'])&& isset($_POST['privacy'])&& isset($_POST['red'])){
          $_POST['newmsg']=str_replace('@'.$_POST['red'].'','<a href="profile.php?username='.$_POST['red'].'">'.$_POST['red'].'</a>', $_POST['newmsg']);
          $date=date('y:m:d H:i:s');
            if($_POST['toid']==$_SESSION['id']){
                rawfeeds_user_core::create_streamitem("1",mysqli_real_escape_string($mysqli,$_SESSION['id']),mysqli_real_escape_string($mysqli,$_POST['newmsg']),mysqli_real_escape_string($mysqli,$_POST['privacy']),mysqli_real_escape_string($mysqli,$_POST['toid']),mysqli_real_escape_string($mysqli,$date));
            }else{
                rawfeeds_user_core::create_streamitem("3",mysqli_real_escape_string($mysqli,$_SESSION['id']),mysqli_real_escape_string($mysqli,$_POST['newmsg']),mysqli_real_escape_string($mysqli,$_POST['privacy']),mysqli_real_escape_string($mysqli,$_POST['toid']),mysqli_real_escape_string($mysqli,$date));
            }
        }
    }

PHP USER_CORE

 public function create_streamitem($typeid,$creatorid,$content,$ispublic,$targetuser,$date){
      global $mysqli;
        $content = $content;
  //    $content =  strip_tags($content);

        if(strlen($content)>0){
           $date=date('y:m:d H:i:s');
          $insert = "INSERT INTO streamdata(streamitem_type_id,streamitem_creator,streamitem_target,streamitem_timestamp,streamitem_content,streamitem_public) VALUES ($typeid,$creatorid,$targetuser,'$date','$content',$ispublic)";
        $add_post = mysqli_query($mysqli,$insert) or die(mysqli_error($mysqli));
        $last_id  = mysqli_insert_id($mysqli); 
            if(!($creatorid==$targetuser)){
                $fromuser=rawfeeds_user_core::getuser($creatorid);
                rawfeeds_user_core::add_notification(2,$_POST['toid'],$fromuser['id'],$fromuser['fullname']." posted a status on your wall","../singlepoststreamitem.php?sid=$last_id");
                $_SESSION['id']==$content;
        }       
        return;
        }else{
            return false;
        }           
}

AJAX

$("form#myforms").submit(function(event) {
    event.preventDefault();
    var content = $(this).children("#toid").val();
    var newmsg= $(this).children("#newmsg").text();
    var username = $(".red").attr("href");
    var privacy = $("#privacy").val();

    $.ajax({
      type: "POST",
      url: "insert.php",
      cache: false,
      dataType: "json",
      data: { toid: content, newmsg: newmsg, privacy: privacy, red: username }, 
      success: function(response){ 
  • 写回答

1条回答 默认 最新

  • dongtaigan1594 2014-09-29 08:27
    关注

    Move the check for $_POST['red'] out of the main check:

    if(isset($_POST['newmsg'])&& isset($_POST['toid'])&& isset($_POST['privacy'])){
                                                   // remove $_POST['red'] here -^
    
      if (isset($_POST['red'])) { // check it here, otherwise your insert will not happen if $_POST['red'] is empty.
        $_POST['newmsg']=str_replace('@'.$_POST['red'].'','<a href="profile.php?username='.$_POST['red'].'">'.$_POST['red'].'</a>', $_POST['newmsg']);
      }
      $date=date('y:m:d H:i:s');
      if($_POST['toid']==$_SESSION['id']){
         rawfeeds_user_core::create_streamitem("1",mysqli_real_escape_string($mysqli,$_SESSION['id']),mysqli_real_escape_string($mysqli,$_POST['newmsg']),mysqli_real_escape_string($mysqli,$_POST['privacy']),mysqli_real_escape_string($mysqli,$_POST['toid']),mysqli_real_escape_string($mysqli,$date));
      }else{
         rawfeeds_user_core::create_streamitem("3",mysqli_real_escape_string($mysqli,$_SESSION['id']),mysqli_real_escape_string($mysqli,$_POST['newmsg']),mysqli_real_escape_string($mysqli,$_POST['privacy']),mysqli_real_escape_string($mysqli,$_POST['toid']),mysqli_real_escape_string($mysqli,$date));
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集