一个按钮工作,另一个没有

时间:2018-04-15 21:31:51

标签: php jquery mysql ajax

我有两个输入按钮。一个将MySQL数据库值设置为0另一个将其设置为1.我有输入按钮将值成功设置为1.选择按钮不执行任何操作。

我正在使用PHP,MYSQL,JQUERY和AJAX

这是我的所有代码

    <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");
}

?>

1 个答案:

答案 0 :(得分:0)

setting_name函数中的查询中的列名select()缺少反引号“`”更改查询

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

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