One button works, the other one doesn't

31
April 16, 2018, at 07:16 AM

I have two input buttons. One to set the MySQL database value to 0 the other to set it to 1. I have got the input button to succesfully set the value to 1. The select button does not do anything.

I'm using PHP, MYSQL, JQUERY, and AJAX

Here is all of my code

    <input type="submit" class="button" name="insert" value="insert"/>
    <input type="submit" class="button" name="select" value="select"/>
    <script>
    $(document).ready(function(){
        $('.button').click(function(){
            var clickBtnValue = $(this).val();
            var ajaxurl = '<?php echo BASE_URL; ?>/actions/settings.php',
            data =  {'action': clickBtnValue};
            $.post(ajaxurl, data, function (response) {
                // Response div goes here.
                alert("This is where the success message goes.");
            });
        });
    });
    </script>

settings.php

<?php
include_once (__DIR__ . "/../oc-config.php");
if (isset($_POST['action'])) {
    switch ($_POST['action']) {
        case 'insert':
            insert();
            break;
        case 'select':
            select();
            break;
    }
}
function select() {
  $site = BASE_URL;
  $link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  if (!$link)
  {
      die('Could not connect: ' . mysql_error());
  }
  $query = "UPDATE settings SET setting_status='0' WHERE `setting_name='CIV_REG'";
  try
  {
      $stmt = mysqli_prepare($link, $query);
      mysqli_stmt_bind_param($stmt, "i", $uid);
      $result = mysqli_stmt_execute($stmt);
      if ($result == false)
      {
          die(mysqli_error($link));
      }
  }
  catch(Exception $e)
  {
      die("Failed to run query: " . $e->getMessage());
  }
  mysqli_close($link);
  session_start();
  $_SESSION['accessMessage'] = '<div class="alert alert-success"><span>Allowed Civilian Registration</span></div>';
  sleep(1);
  header("Location:".BASE_URL."/oc-admin/settingsManagement.php");
}
function insert() {
  $site = BASE_URL;
  $link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  if (!$link)
  {
      die('Could not connect: ' . mysql_error());
  }
  $query = "UPDATE settings SET setting_status='1' WHERE setting_name='CIV_REG'";
  try
  {
      $stmt = mysqli_prepare($link, $query);
      mysqli_stmt_bind_param($stmt, "i", $uid);
      $result = mysqli_stmt_execute($stmt);
      if ($result == false)
      {
          die(mysqli_error($link));
      }
  }
  catch(Exception $e)
  {
      die("Failed to run query: " . $e->getMessage());
  }
  mysqli_close($link);
  session_start();
  $_SESSION['accessMessage'] = '<div class="alert alert-success"><span>Allowed Civilian Registration</span></div>';
  sleep(1);
  header("Location:".BASE_URL."/oc-admin/settingsManagement.php");
}
?>
Answer 1

You have a missing backtick " ` " for the column name setting_name in your query inside the select() function checnge your query

from

 $query = "UPDATE settings SET setting_status='0' WHERE `setting_name='CIV_REG'";

to

$query = "UPDATE settings SET `setting_status`='0' WHERE `setting_name`='CIV_REG'";
READ ALSO
Same JavaScript file for multiple JSON files, but selecting which JSON data is shown in a specific part of the site

Same JavaScript file for multiple JSON files, but selecting which JSON data is shown in a specific part of the site

I am trying to set-up a website which would contain multiple quizzes - all on a different topic but all in the same multiple-choice formatWhat I would like to do is have each topic placed in different parts of the website (something I am doing with HTML/CSS)

50
Intercept all outgoing requests

Intercept all outgoing requests

In short, is there any way to intercept all outgoing requests in a page in order to append an Authorization token to the header prior to sending the request? This includes all requests that are out of the control of the user (non-clickable ones) such as request...

76
Display API Data in HTML for Stock/BTC Price?

Display API Data in HTML for Stock/BTC Price?

I'm trying to display the data from this api: https://blockchaininfo/q/24hrprice and display it in an HTML file using JS under the price header for the coins

68