Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

php - Code inserted triplicate records a few times and now works periodically

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();

     }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It would appear that your if condition is not being evaluated properly. Are $blurb and $user_ID the same type?

From http://php.net/manual/en/language.operators.comparison.php

$a != $b Not equal TRUE if $a is not equal to $b after type juggling.

$a !==$b Not identical TRUE if $a is not equal to $b, or they are not of the same type.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...