'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
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mikan |
