插入一式三份的代码记录几次,现在定期工作 [英] Code inserted triplicate records a few times and now works periodically

查看:234
本文介绍了插入一式三份的代码记录几次,现在定期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当user1批准帖子时,我有代码在点表中插入一行。我写了代码,以便它只批准帖子,如果用户不是帖子的作者。最多可以有10个帖子随时选择批准。



然而,虽然这只会批准一个帖子(并插入一个记录点),如果当前用户不是帖子的作者,它不会插入已批准帖子的记录。相反,它会插入点数表,因为有用户的职位。



我想要做的是插入一个批准当前用户所在帖子的作者不是当前用户。



我非常接近。这段代码的作用,除了它会插入所有记录,如果有其他用户有多个帖子,而不是当前用户选择批准的一个帖子。

  $ results2 = $ dbh->准备(select 
wp_users.ID,
wp_users.display_name,
stories.ID AS ID1,
stories .SID,
writing.ID AS ID2,
writing.WID,
writing.text
FROM writing
在writing.SID = stories.SID $上的LEFT JOIN故事b $ b LEFT JOIN wp_users ON writing.ID = wp_users.ID
WHERE(stories.SID = $ the_SID)
通过书写顺序.WID asc limit 10
);

$ results2-> bindParam(':wp_users.ID',$ user_ID,PDO :: PARAM_INT);
$ results2-> bindParam(':display_name',$ display_name,PDO :: PARAM_STR);
$ results2-> bindParam(':stories.ID',$ ID1,PDO :: PARAM_INT);
$ results2-> bindParam(':stories.SID',$ the_SID,PDO :: PARAM_STR);
$ results2-> bindParam(':writing.WID',$ WID,PDO :: PARAM_STR);
$ results2-> bindParam(':writing.ID',$ ID2,PDO :: PARAM_INT);
$ results2-> bindParam(':text',$ text,PDO :: PARAM_STR);
$ results2-> execute();
$ row2 = $ results2-> fetchAll(PDO :: FETCH_ASSOC);
$ b foreach($ row2 as $ result5){
$ blurb = $ result5 ['ID2'];
settype($ blurb,integer);




$ b // PA APPROVE INSERT CONTROL

if(isset($ _ POST ['yes']) )){

//从表单获取值
$ yes_WID = $ _POST ['yes'];

$ yesupdate =更新写入SET批准=:批准,位置=:位置
WHERE WID =:WID;
$ stmt2 = $ dbh-> prepare($ yesupdate);
$ stmt2-> bindParam(':WID',$ yes_WID,PDO :: PARAM_INT);
$ stmt2-> bindParam(':approved',$ e = Y,PDO :: PARAM_STR);
$ stmt2-> bindParam(':position',$ row2 [0] ['position'],PDO :: PARAM_INT);
$ stmt2-> execute();




$ yes_WID = $ _POST ['yes'];

//只要用户不是作者,就试图给出分数

$ contpoint = 3;
foreach($ row2 as $ result5){
if($ blurb!== $ user_ID){

$ yesupdate2 =INSERT INTO points(ID,
SID,
WID,
PID)VALUES(
:ID,
:SID,
:WID,
:PID)
$ stmt9 = $ dbh-> prepare($ yesupdate2);
$ stmt9-> bindParam(':ID',$ blurb,PDO :: PARAM_INT);
$ stmt9-> bindParam(':SID',$ the_SID,PDO :: PARAM_INT);
$ stmt9-> bindParam(':WID',$ yes_WID,PDO :: PARAM_INT);
$ stmt9-> bindParam(':PID',$ contpoint,PDO :: PARAM_INT);
$ stmt9-> execute();


$ b $ / code $ / $ p

解决方案

这个代码的目的是什么?它只是为每个值设置$ blurb,然后对结果不做任何处理,除非代码清单之间的大括号没有正确显示。那么它是一个foreach内的foreach?

在任何情况下,它不会出现,您设置$ user_ID作为一个整数与$
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
settype($ blurb,integer);





$ b

取消第一个foreach循环并将其与第二个循环结合起来:

  foreach($ row2 as $ result5){
$ blurb = $ result5 ['ID2'];
$ userID = $ result5 ['user_ID'];
settype($ blurb,integer);
settype($ userID,integer);
if($ blurb!== $ userID){
... etc


I have code that inserts a row in the points table when a user1 approves a post. I wrote code so that it only approves the post if the user is not the author of the post. There can be a maximum of 10 posts at any time to choose from for approve.

However, while this will only approve a post (and insert a record in points) if the current user is not the author of the post, it will not just insert the record for the post that is approved. Rather it will insert as many rows into points table as there are posts for user.

What I want to do is insert into points for a post that is approved by the current user where the post author is not the current user.

I am very close. This code works, except that it will insert all records if there are multiple posts by other users instead of just the one post that the current user chooses to approve.

$results2 = $dbh->prepare("select 
wp_users.ID,
wp_users.display_name,
stories.ID AS ID1,
stories.SID,
writing.ID AS ID2,
writing.WID,
writing.text
FROM writing
LEFT JOIN stories on writing.SID = stories.SID
LEFT JOIN wp_users ON writing.ID = wp_users.ID
WHERE (stories.SID = $the_SID)
order by writing.WID asc limit 10
");

$results2->bindParam(':wp_users.ID', $user_ID, PDO::PARAM_INT); 
$results2->bindParam(':display_name', $display_name, PDO::PARAM_STR); 
$results2->bindParam(':stories.ID', $ID1, PDO::PARAM_INT); 
$results2->bindParam(':stories.SID', $the_SID, PDO::PARAM_STR);
$results2->bindParam(':writing.WID', $WID, PDO::PARAM_STR);  
$results2->bindParam(':writing.ID', $ID2, PDO::PARAM_INT); 
$results2->bindParam(':text', $text, PDO::PARAM_STR); 
$results2->execute();
$row2 = $results2->fetchAll(PDO::FETCH_ASSOC);

foreach ($row2 as $result5) {
     $blurb = $result5['ID2'];
     settype($blurb, "integer");
}




//PA APPROVE INSERT CONTROL

if(isset($_POST ['yes'])){

// Get values from form 
$yes_WID = $_POST['yes'];

$yesupdate = "UPDATE writing SET approved = :approved, position = :position   
WHERE WID = :WID";
$stmt2 = $dbh->prepare($yesupdate);
$stmt2->bindParam(':WID', $yes_WID, PDO::PARAM_INT);        
$stmt2->bindParam(':approved', $e = Y, PDO::PARAM_STR);   
$stmt2->bindParam(':position', $row2[0]['position'], PDO::PARAM_INT);                                        
$stmt2->execute();




$yes_WID = $_POST['yes'];

//trying to give points as long as user is not the author

$contpoint = 3;
foreach($row2 as $result5){
     if($blurb !== $user_ID){

          $yesupdate2 = "INSERT INTO points(ID, 
          SID,
          WID,
          PID) VALUES(
          :ID,
          :SID,
          :WID,
          :PID)";
          $stmt9 = $dbh->prepare($yesupdate2);
          $stmt9->bindParam(':ID', $blurb, PDO::PARAM_INT);      
          $stmt9->bindParam(':SID', $the_SID, PDO::PARAM_INT);        
          $stmt9->bindParam(':WID', $yes_WID, PDO::PARAM_INT);        
          $stmt9->bindParam(':PID', $contpoint, PDO::PARAM_INT);     
          $stmt9->execute();

     }
}

解决方案

What is the purpose of this code? It just sets $blurb to each of the values and then does nothing with the result unless the brace in between the code listings is not correctly shown. So is it a foreach inside of a foreach?

In any case, it does not appear that you set $user_ID as an integer in the same manner as you do for $blurb.

foreach ($row2 as $result5) {
$blurb = $result5['ID2'];
settype($blurb, "integer");
}

Do away with the first foreach loop and combine it with the second as such:

foreach($row2 as $result5){
    $blurb = $result5['ID2'];
    $userID = $result5['user_ID'];
    settype($blurb, "integer");
    settype($userID, "integer");
if($blurb !== $userID){
   ...etc

这篇关于插入一式三份的代码记录几次,现在定期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆