Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
onclick function fails to update sql

I have this button

<button id = "addBtn-1" onclick = "noneAdd();add1()">Add to My Classes</button> 

It calls a function called add1() from my js file, which adds and removes classes and also calls add():

function add(){
    $.ajax({url:"add.php",
    success:function(result){
        alert("Added to My Classes");
    }
    });
}

which calls my php file, add.php:

require_once "config.php";
include "classes.php";

$myun = $_SESSION["un"];
function bookmark1Add(){

    $sql = "SELECT testclass1 FROM classes WHERE username = ?";
    if($stmt = mysqli_prepare($db, $sql)){

        mysqli_stmt_bind_param($stmt, "s", $param_un);
        $param_un = $myun;

        if(mysqli_stmt_execute($stmt)){

            mysqli_stmt_bind_result($stmt, $myclass1);

            if(mysqli_stmt_fetch($stmt)){

                if($myclass1 == 0){

                    $sql2 = "UPDATE classes
                    SET testclass1 = 1";

                    if($stmt2 = mysqli_prepare($db, $sql2)){

                        mysqli_stmt_bind_param($stmt2, "s", "param_un");
                        $param_un = $myun;

                        if(mysqli_stmt_execute($stmt2)){
                            header("Refresh:0");
                        }
                        else{
                            echo "Something went wrong, please try again later!";
                        }
                    }
                    mysqli_stmt_close($stmt2);
                }
            }
        }
    }
    mysqli_stmt_close($stmt);
}

bookmark1Add();

This line of code is supposed to update the data in my table, but it doesn't seem to be working. When I click the button, nothing changes in the database, however the adding and removing of classes from add1() works so it succeeded in calling the function

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

nvm I made the code much simpler since the one above was way too complicated and rat it on its own to see if there were any bugs.

<?php
require_once "config.php";
include "classes.php";

$myun = $_SESSION["un"];
$sql = "UPDATE classes SET testclass1 = 1 WHERE username = '$myun'";

if (mysqli_query($db, $sql)) {
    header: "Refresh:0";
}
?>

this worked way better

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!