0

Hopefully I'm not duplicating this question, but I wasn't able to find the solution to the issue I am having.

I have a set of buttons, that are populated from a mySQL database. When the user clicks on ONE of the buttons, I want it to add 10 to the current value of the cell 'score' in that table where the id of the button equals the table row.

I really don't know where I'm going wrong, here.

HTML:

<button class="addPoints" data-id="person01"><span>Person01</span></button>
<button class="addPoints" data-id="person02"><span>Person02</span></button>
<button class="addPoints" data-id="person03"><span>Person03</span></button>
<button class="addPoints" data-id="person04"><span>Person04</span></button>

JS:

$(".addPoints").on('click', function(){
        var $id = $( this ).data( 'id' );
        console.log($id);
        $.ajax({
            type: "POST",
            cache: false,
            async: true,
            url: "addPoints.php",
            data: {class: $id},
            success: function (data) {},
            error: function(xhr, ajaxOptions, thrownError){
                console.log(xhr.status);
                console.log(thrownError);
            }
        })
    });

PHP:

require "config.php";

    $conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $class=$_POST["class"];

    $sql = "UPDATE roster SET score = score + '10' WHERE `class` = ".$class;

    $conn->close();

Every row in my mySQL Database "roster" has the following columns:

id, class, name, score

I'm using the same config.php to populate the buttons with their data-id and the text, so I've ruled out that it is an issue with that file. when I go to the php page 'addPoints.php', I get this error:

Notice: Undefined index: class in addPoints.php

Jay Blanchard
  • 32,731
  • 15
  • 70
  • 112
Murphy1976
  • 1,243
  • 5
  • 36
  • 83
  • [Have you watched the AJAX request / response in the browser's developer tools? Have you included the jQuery library in the project? Are there any errors reported? Are you running this on a web-server?](http://jayblanchard.net/basics_of_jquery_ajax.html) – Jay Blanchard Apr 19 '17 at 20:17
  • @jay-blanchard, could you at least link me to the duplicate question, if this is, in fact a duplicate question. – Murphy1976 Apr 19 '17 at 20:18
  • Look at the top of the page and you will see the link. – Jay Blanchard Apr 19 '17 at 20:19
  • ok, I declared $class, and then attempted to set it with **$class = isset($_POST['class']) ? $_POST['class'] : ''**, I'm not getting the line error anymore, but the value is still not being updated in the database. – Murphy1976 Apr 19 '17 at 20:25
  • Can you answer the questions from my first comment? – Jay Blanchard Apr 19 '17 at 20:27
  • By the way, I'm just running this on XAMPP, the only response I am getting from Developer Tools is a few console.log() where I was checking that the $id was getting populated properly. Other than that... no errors until I go to the addPoints.php – Murphy1976 Apr 19 '17 at 20:28
  • What is in the request / response? – Jay Blanchard Apr 19 '17 at 20:29
  • Response Headers, Request Headers, or Form Data? I don't know where to look for the information you're asking for. – Murphy1976 Apr 19 '17 at 20:32
  • In the network tab of the developer tools. You should see what AJAX is sending and what PHP is returning. – Jay Blanchard Apr 19 '17 at 20:33
  • in the XHR, under addPoints.php, after I click a button, under RESPONSE, all I'm seeing is "this request has no response data available", but in the headers, the Form Data is showing exactly what I intend to send to the database "class: person01" – Murphy1976 Apr 19 '17 at 20:36
  • Do you have a person01 in the database? – Jay Blanchard Apr 19 '17 at 20:41
  • yes, under the 'class' column of table 'roster' – Murphy1976 Apr 19 '17 at 20:42
  • Then I cannot see any other problem from here. Check the error logs on your web server as they should have additional information. – Jay Blanchard Apr 19 '17 at 20:43
  • I am pulling all of the data in each of the buttons, data-id and the text FROM the database, I have a separate Query populating the buttons. – Murphy1976 Apr 19 '17 at 20:43
  • Do yourself a favor, simply place a `print_r($_POST)` or `var_dump($_POST)` in the PHP page which receives the form submission. Fill out your form and submit. – Jay Blanchard Apr 19 '17 at 20:44
  • Response: Array([class] => person01) – Murphy1976 Apr 19 '17 at 20:45
  • Add some space around the `=` here: `$class = $_POST["class"]` – Jay Blanchard Apr 19 '17 at 20:47
  • Also put quotes around the string in your query: `WHERE class = '$class'";` No need to concatenate, which leads to... – Jay Blanchard Apr 19 '17 at 20:48
  • ...[Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Apr 19 '17 at 20:48
  • Update the code in the question with what you have now. Quotes shouldn't be causing problems. – Jay Blanchard Apr 19 '17 at 20:51
  • I'm still not getting any update in the database. – Murphy1976 Apr 19 '17 at 20:53
  • Update your question with the code you're using after the fixes we've talked about. – Jay Blanchard Apr 19 '17 at 20:54
  • Also, you never execute the query. My apologies, long day and I am not paying full attention here. – Jay Blanchard Apr 19 '17 at 20:58
  • http://php.net/manual/en/mysqli.query.php – Jay Blanchard Apr 19 '17 at 21:11
  • WELL DERP! I knew it had to be something simple that I was just forgetting.... UGH.. yeah... long day here too. Let me try this out, and see what's good. – Murphy1976 Apr 19 '17 at 21:40
  • That was the issue. I just added `if ($conn->query($sql) === TRUE) { echo "Updated data successfully\n"; } else { echo $conn->error; }` and is all updated successfully. Thank you for your assistance. – Murphy1976 Apr 20 '17 at 13:55
  • 1
    You bet. Glad I could help. – Jay Blanchard Apr 20 '17 at 15:52

0 Answers0