1

I have been trying to figure it out with searches, but I am running into deprecated code and conflicting advice.

I am trying to select the number of yes votes with an ID that is passed through the url. I want to then add one to that number and UPDATE the table. I'm not quite sure exactly where I've gone wrong.

EDIT: I have tried using yesvotes=yesvotes +1 and that gets me from 0 to 1 but will not get me from 1 to 2 or any further. I just need a clicked link (which passes the needed 'ID' through the URL and retrieved using $_GET) to add 1 to the 'yesvotes' column. I thought it was a straightforward process but it seems like I am missing something or have a syntax error somewhere.

Sorry for the newb shit, just trying to learn.

    <?php

    $entry = $_GET ['id'];

    include 'connect.php';

    $result = $mysqli->query("SELECT * FROM yesvotes WHERE ID = '" . $entry ."'"); 

    $vote = $result->fetch_array();

    $votetotal = $vote++;


    $sql = 
    "UPDATE showsubmit
    SET yesvotes = '" . $votetotal . "'
    WHERE ID = '" . $entry . "'";

    $conn->query($sql);

    $conn->close();


    ?>
Bj Emery
  • 31
  • 3
  • 2
    If you just want to add 1 to the existing value, you can simply do `UPDATE showsubmit SET yesvotes=yesvotes+1 WHERE ID=....` – Qirel Dec 02 '16 at 08:42
  • What's the concurrency issue of this? This seems like a problem. – Drew Dec 02 '16 at 08:50
  • `$votetotal = $vote++;` You've got a problem here. `$vote` is an array, not a value. Since you're using `fetch_array()` (and you don't enumerate your fields) you should use `$votetotal = $vote[0] + 1;` – Machavity Dec 02 '16 at 18:22
  • I tried doing "yesvotes = yesvotes +1 ...etc" before and it successfully got me from 0 to 1 but would not go any further. no matter how many times I run the script (click the link) it just stays at 1. – Bj Emery Dec 03 '16 at 04:33
  • Machavity, what is the significance of the [0] in the code you suggested? – Bj Emery Dec 03 '16 at 04:36

1 Answers1

2

You don't need all this ... you can perform a update-join like

UPDATE showsubmit ss
JOIN ( SELECT ID, COUNT(*) as total
       FROM yesvotes WHERE ID = 2334 ) XXX ON ss.ID = XXX.ID
    SET ss.yesvotes = XXX.total + 1;
Rahul
  • 76,197
  • 13
  • 71
  • 125