传入php文件的数据不允许MySQL连接


Data passed to php file does not allow MySQL connection

所以我删除了我的上一篇文章,因为我认为我的问题不够简洁。基本上,我想在我的php文件中创建一个MySql连接,使用从我的主页(JS)传递的变量,我的索引页传递变量如下:

$.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时,我得到一个数据库错误。这是格式问题还是我做错了什么?*另外,是的,我知道注射,这不是一个问题。

如果两个字符串包含完全相同的值,它们要么都有效,要么都不有效。

我个人不使用以数字开头的数据库名,但我认为您需要像转义表或列名一样转义它们:使用反引号:

'db_name' => "`{$name}`";