所以我删除了我的最后一篇文章,因为我觉得我的问题不够简明。基本上,我想创建一个使用从我的主网页传递变量我的PHP文件中的MySQL连接(JS)我的索引页面传递变量,如下所示:传递给php文件的数据不允许MySQL连接
$.ajax({
url: "php/ajax.php",
type: "POST",
data: {name:'3404-11833'},
success: function(data){
window.location.assign("http://www.dasite.com/" + myloc + ".html");
}
});
}
的PHP文件如下:
<?php
/* Database Configuration.*/
$name = $_POST['name'];
$test = strval('3404-11833');
$dbOptions = array(
'db_host' => '',
'db_user' => '',
'db_pass' => '',
'db_name' => $name
);
/* Database Config End */
error_reporting(E_ALL^E_NOTICE);
require "classes/DB.class.php";
require "classes/Chat.class.php";
require "classes/ChatBase.class.php";
require "classes/ChatLine.class.php";
require "classes/ChatUser.class.php";
session_name('webchat');
session_start();
if(get_magic_quotes_gpc()){
array_walk_recursive($_GET,create_function('&$v,$k','$v = stripslashes($v);'));
array_walk_recursive($_POST,create_function('&$v,$k','$v = stripslashes($v);'));
}
try{
// Connecting to the database
DB::init($dbOptions);
$response = array();
// Handling the supported actions:
switch($_GET['action']){
case 'login':
$response = Chat::login($_POST['name']);
break;
case 'checkLogged':
$response = Chat::checkLogged();
break;
case 'logout':
$response = Chat::logout();
break;
case 'submitChat':
$response = Chat::submitChat($_POST['chatText']);
break;
case 'getUsers':
$response = Chat::getUsers();
break;
case 'getChats':
$response = Chat::getChats($_GET['lastID']);
break;
default:
throw new Exception('Wrong action');
}
echo json_encode($response);
}
catch(Exception $e){
die(json_encode(array('error' => $e->getMessage())));
}
?>
现在,当我使用$ test时,代码工作正常,但是当我切换到$ name时,出现数据库错误。这是一个格式问题还是我做了其他错误? *另外,是的,我知道注射,这里不是问题。
你应该发布数据库代码和变量的var_dump()。但用户提供的输入不应该导致数据库错误。 – jeroen
没有用户提供的输入。我应该在这里发布完整的数据库代码,还是应该关闭它并提出一个新问题? – user3634006
我加了完整的php文件。你是否也需要查看所需的类文件? – user3634006