douliu3831 2014-03-10 07:19
浏览 11

如何向所有用户发送评论(在1个表中)(指另一个表)?

Right now I have 2 tables. A comment table and a user table. Short and simple, I wanna sent a comment to all users. I need to know the sql command for it and how to display on PHP.

Comment table

  1. pk
  2. comment
  3. sender (refers to username)
  4. receiver (refers to username)

User table

  1. pk
  2. username
  3. etc.....

I tried using this:

$provider = $_REQUEST['provider'];

$cert = $_REQUEST['cert'];

$valid = $_REQUEST['valid'];

$lprice = $_REQUEST['lprice'];

$trial = $_REQUEST['trial'];

$cost = $_REQUEST['cost'];

echo $cert;

$sql=mysql_query("SELECT * FROM sslcert WHERE CertificateName='$cert'",$connect);
 if(mysql_num_rows($sql)>0)
   {
    echo"SSL already exists.";
    echo "<br><a href=addssl.php>Back</a>";
   }
 else
    {
      $insert = mysql_query("INSERT INTO sslcert (Provider, CertificateName, Validation,                    
      ListPrice, Trial, Cost) VALUES ('$provider', '$cert', '$valid', '$lprice', 
     '$trial',     '$cost')",$connect);


          $result77=mysql_query("SELECT username FROM user");
     while($row77=mysql_fetch_array($result77))
     {
       $insert1 = mysql_query("INSERT INTO notification (msg, sender, receiver,   notification) VALUES ('A new product is now available, $cert by $provider', '$username',   '$row77['username']', 1)");
       if(!$insert1){

            die("There's little problem: ".mysql_error());
            echo "<br><a href=quotesmanage1.php>Back</a>";
        }
     }

    if(!$insert){

       die("There's little problem: ".mysql_error());
       echo "<br><a href=addssl.php>Back</a>";
    }

    echo "<br>You have add a new Product <br><a href=adminhome.php>Back to Home</a>";

    }   
    mysql_close($connect);
}

the call for username is somewhere else on the page. It came out with a blank page (error on php), any ideas?

  • 写回答

1条回答 默认 最新

  • douyi2107 2014-03-11 14:58
    关注

    I believe you want a INSERT...SELECT query.

    INSERT INTO `Comment`(`Comment`.`comment`, `Comment`.`sender`, `Comment`.`receiver`)
    SELECT 'Message as text or variable substitution', `User`.`pk`, '1'
    FROM `User`
    

    You can substitute any ID instead of 1 at the end of the SELECT line, I just figured your administrative user is probably 1 (but you can create a new user for sending these mass messages and just use whatever number ID corresponds.

    By the way, in your comment table you should probably reference User.pk, not User.username. That's sort of why you have the key in the first place :-)

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题