我现在已经运行了这个代码大约一个星期了,直到今天才解决这个问题。当我第一次加载我的网页时,初始化数据库并创建表(如果它们中的任何一个尚不存在)。但是,在同一个HTML页面上,我调用了一个Javascript函数来创建数据库中的数据表。此函数调用生成一个错误,表示数据库中不存在该表,但是我登录到WAMP并且表存在,并且在后续加载(数据库已经存在)中,该函数正常工作。这里出了什么问题?
actions.js:
$(document).ready(function() {
$.ajax({
url: "action.php",
type: "GET",
data: { param1: "INITIALIZE"}
});
var json = [];
//Function for getting decks from database
function getDecks(hide) {
$.ajax({
url: "get_decks.php",
type: "GET",
data: {hideDecks : hide},
success: function(data) {
alert(data);
json = JSON.parse(data);
if(hide == "TRUE") {
createTable();
} else {
createHideDeckTable();
}
}
});
}
}
action.php的:
define("SERVER_NAME", "localhost");
define("USERNAME", "root");
define("PASSWORD", "");
define("DATABASE", "flashcards");
define("CHAPTERS_TABLE", "chapters");
define("DECKS_TABLE", "decks");
define("CARDS_TABLE", "cards");
//Function to execute database queries
function executeQuery($sql_query) {
//Connecting to database
$mysqli = mysqli_connect(SERVER_NAME, USERNAME, PASSWORD, DATABASE);
//Check database connection
if($mysqli === false) {
die ("\nCould not connect: " . mysqli_connect_error());
}
$result = mysqli_query($mysqli, $sql_query);
if($result === false){
echo nl2br("\n\nERROR: Could not able to execute $sql_query. " . mysqli_error($mysqli));
}
//Close database connection
mysqli_close($mysqli);
}
//Function to execute database queries and return the result
function executeQueryWithResult($sql_query) {
//Connecting to database
$mysqli = mysqli_connect(SERVER_NAME, USERNAME, PASSWORD, DATABASE);
//Check database connection
if($mysqli === false) {
die ("\nCould not connect: " . mysqli_connect_error());
}
$rows = [];
$result = mysqli_query($mysqli, $sql_query);
if($result === false){
echo nl2br("\n\nERROR: Could not able to execute $sql_query. " . mysqli_error($mysqli));
} else {
while($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
return $rows;
}
//Close database connection
mysqli_close($mysqli);
}
//This function initializes the database and all tables in the database
function initializeDatabase() {
$mysqli = mysqli_connect(SERVER_NAME, USERNAME, PASSWORD);
//Check database connection
if($mysqli === false) {
die ("\nCould not connect: " . mysqli_connect_error());
}
//Query to create flashcards database
$sql = "CREATE DATABASE IF NOT EXISTS " . DATABASE;
mysqli_query($mysqli, $sql);
//Set flashcards database as default database
mysqli_select_db($mysqli, DATABASE);
//Create the "Chapters" table
$sql_create_chapters_table = "CREATE TABLE IF NOT EXISTS " . CHAPTERS_TABLE . "(
chapter_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL UNIQUE
)";
executeQuery($sql_create_chapters_table);
//Create the "Decks" table
$sql_create_decks_table = "CREATE TABLE IF NOT EXISTS " . DECKS_TABLE . "(
deck_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
chapter_ID INT NOT NULL,
deck_description VARCHAR(255) NOT NULL,
faculty BOOLEAN NOT NULL,
hidden BOOLEAN NOT NULL,
FOREIGN KEY (chapter_ID) REFERENCES chapters(chapter_ID)
)";
executeQuery($sql_create_decks_table);
//Create the "Cards" table
$sql_create_cards_table = "CREATE TABLE IF NOT EXISTS " . CARDS_TABLE . "(
card_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
deck_ID INT NOT NULL,
front_text VARCHAR(255) NOT NULL,
front_media VARCHAR(255),
back_text VARCHAR(255) NOT NULL,
back_media VARCHAR(255),
FOREIGN KEY (deck_ID) REFERENCES decks(deck_ID)
)";
executeQuery($sql_create_cards_table);
//Close database connection
mysqli_close($mysqli);
}
//Process GET request
if(isset($_GET["param1"])) {
$arg = $_GET["param1"];
//"INITIALIZE" means database and tables should be created
if($arg == "INITIALIZE") {
initializeDatabase();
}
}
get_decks.php:
include "action.php";
$sql_query = "SELECT * FROM " . DECKS_TABLE . " JOIN " . CHAPTERS_TABLE . " ON " . CHAPTERS_TABLE . ".chapter_ID=" . DECKS_TABLE . ".chapter_ID";
//Check whether or not hidden decks should be returned by the query
if(isset($_GET["hideDecks"])) {
if(strcmp($_GET["hideDecks"], "TRUE")==0) {
$sql_query .= " WHERE hidden=\"0\"";
}
}
$rows = executeQueryWithResult($sql_query);
echo json_encode($rows);
的index.html:
<!DOCTYPE html>
<html>
<head>
<title>My Application</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="actions.js" type="text/javascript"></script>
</head>
<body>
<h1 align="center">WELCOME TO MY APPLICATION</h1>
<script>getDecks("TRUE");</script>
</body>
</html>