使用ajaxphp在数据库之间切换

1sbrub3j  于 2021-06-17  发布在  Mysql
关注(0)|答案(3)|浏览(215)

我正在尝试在数据库连接之间切换(只需更改主机名),具体取决于所选的数据库名称。
我得到了 <a> 带值的标记。

<div class="user-menu dropdown-menu">
    <a class="nav-link" href="db" id="db_one"><i class="fa fa-database"></i> DB 1</a>
    <a class="nav-link" href="db" id="db_two"><i class="fa fa-database"></i> DB 2</a>   
</div>

现在我在查 <a> 然后取值 db_one/db_two 并通过ajax发送到我的php连接文件。
我的错在哪里?我仍然有默认连接

$("a[href='db']").click(function(e) {
  e.preventDefault();
  var hostname = e.target.id;
  console.log(hostname);
  $.ajax({
    url: 'connection.php',
    data: hostname,
    type: "post",
    success: function() {
      alert('done');
      console.log('this is: ' + hostname);
      location.reload();
    },
  });
});

我正在重新加载页面与数据库“重新连接”。
最后是我的php连接。

<?php

    $host = $_POST['hostname'];
    $hostname = $host;

    switch ($hostname) {
      case "db_one":
        $conn = mysqli_connect("$hostname", "my_user", "my_password", "my_db");
        if($conn === false){
            die("ERROR: Could not connect. " . mysqli_connect_error());
        } else {
          echo "<script>console.log('connected');</script>";
        }
        break;
      case "db_two":
        $conn = mysqli_connect("$hostname", "my_user", "my_password", "my_db");
        if($conn === false){

            die("ERROR: Could not connect. " . mysqli_connect_error());

        } else {
          echo "<script>console.log('Polaczenie z baza nawiazane');</script>";
          echo "<script>console.log('connected');</script>";

        }
        break;
      default:
        $conn = mysqli_connect("localhost", "my_user", "my_password", "my_db");
        if($conn === false){
            die("ERROR: Could not connect. " . mysqli_connect_error());

        } else {
          echo "<script>console.log('default connect');</script>";

        }
    }
?>
agyaoht7

agyaoht71#

您没有发送密钥/值对…只发送一个值。
改变

data: hostname

data: {hostname:hostname}
czfnxgou

czfnxgou2#

现在使用完整的代码

<?php
if ((function_exists('session_status') //start session if not started
  && session_status() !== PHP_SESSION_ACTIVE) || !session_id()) {
 $lifetime=strtotime('+7 days', 0);
  session_set_cookie_params($lifetime);
  session_start();
} if(isset($_POST['hostname'])){
    $_SESSION['hostname']=$_POST['hostname'];
}  $hostname =$_SESSION['hostname'];

    switch ($hostname) {
      case "db_one":
        $conn = mysqli_connect("$hostname", "my_user", "my_password", "my_db");
        if($conn === false){
            die("ERROR: Could not connect. " . mysqli_connect_error());
        } else {
          echo "<script>console.log('connected');</script>";
        }
        break;
      case "db_two":
        $conn = mysqli_connect("$hostname", "my_user", "my_password", "my_db");
        if($conn === false){

            die("ERROR: Could not connect. " . mysqli_connect_error());

        } else {
          echo "<script>console.log('Polaczenie z baza nawiazane');</script>";
          echo "<script>console.log('connected');</script>";

        }
        break;
      default:
        $conn = mysqli_connect("localhost", "my_user", "my_password", "my_db");
        if($conn === false){
            die("ERROR: Could not connect. " . mysqli_connect_error());

        } else {
          echo "<script>console.log('default connect');</script>";

        }
    }
?>
uoifb46i

uoifb46i3#

使用以下代码发送ajax请求:

$("a[href='db']").click(function(e) {
  e.preventDefault();
  var hostname = this.id;
  console.log(hostname);
  $.ajax({
    url: 'connection.php',
    data: {'hostname': hostname},
    type: "post",
    success: function() {
      alert('done');
      console.log('this is: ' + hostname);
      location.reload();
    },
  });
});

希望这能解决问题

相关问题