将数据发送到php以重新运行数据库中的数据

beq87vna  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(248)

我想让所有用户都有一个特定的名字,这个名字在一个javascript变量中,而这个代码不起作用,我应该怎么做?

<table id="table">
        <tr>
        <td>id</td>
        <td>name</td>
        <td>info</td>
        </tr>
    </table>
<script>
        var table = document.getElementById("table");
$.ajax({ 
        type:"POST", 
        url:"process.php", 
        data:{name:"mike"}
        });
</script>
<?php include 'process.php'; ?>
<?php foreach ($users as $user) : ?>
<script>
var row = table.insertRow(1);
  var cell1 = row.insertCell(0);
  var cell2 = row.insertCell(1);
  var cell3 = row.insertCell(2);
  cell1.innerHTML="<?= $user->id ?>";
  cell2.innerHTML="<?= $user->name ?>";
  cell3.innerHTML="<?= $user->info ?>";
</script>
<?php endforeach; ?>

php

$name = $_POST["name"];
$database = [ 
    'host'   => 'localhost', 
    'dbname' => 'dbname', 
    'user'   => 'user', 
    'pass'   => '' 
];
try { 
    $db = new PDO("mysql:host={$database['host']};dbname={$database['dbname']}", $database['user'], $database['pass']); 
} catch (PDOException $e) { 
    die("An error happend, Error: " . $e->getMessage()); 
}
function getAllUsers(){
    global $db;
    $sql = "SELECT * FROM users WHERE $name = name";
    $stmt = $db->prepare($sql);
    $stmt->execute();
    return $stmt->fetchAll(PDO::FETCH_OBJ);
}
$users = getAllUsers();

错误是process.php第2行中的“undefined array key”name“您能帮我将数据发送到我的php文件并基于此获取查询吗?”

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题