函数$不能在PHP和MYSQL组合中工作

时间:2015-04-24 17:04:52

标签: php mysql

我想知道为什么这不会执行,我的意思是这次成功,但是reff只是$ r但是我想成为我写的东西,

$r = $_GET['r'];

并且$ r的昵称类似于page.php?r =昵称。

$query_new_user_insert = $this->db_connection->prepare('INSERT INTO users (user_name, user_password_hash, user_email, user_activation_hash, user_registration_ip, user_registration_datetime, reff) VALUES(:user_name, :user_password_hash, :user_email, :user_activation_hash, :user_registration_ip, now(), "$r")');

你们可以尝试解决这个问题吗,我只尝试了$ r但它不会写...就像错误或其他东西..请帮助,最好的问候。

错误:#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':user_name, :user_password_hash, :user_email, :user_activation_hash, :user_regis' at line 1

4 个答案:

答案 0 :(得分:2)

  

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在':user_name

附近使用正确的语法
  • 注意引用=> ':user_name? MySQL正在和你说话。

在这里走出困境,但我很确定这就是问题所在。

您的数据库连接很可能是mysqli_mysql_,并且您正在使用PDO语法进行查询。 之前我见过这种类型的错误。

这些不同的MySQL API不会相互混合,这解释了语法错误。

你在那里使用mysql_

同样,不同的MySQL API 相互混合 从连接到查询使用单个API。

答案 1 :(得分:1)

问题在于变量替换。在字符串周围使用双引号时,变量将替换为其值。如果您使用单引号,则不是。您最终得到文字字符串$variableName,而不是该变量中的值。

要修复,您需要在查询周围使用双引号或将变量连接到字符串中。

$someVar = "test";
echo "This variable will show it's value: $someVar";
//This variable will show it's value: test

echo 'This variable will show it\'s name: $someVar";
//This variable will show it's value: $someVar

echo 'This variable is concatenated and will show it\'s value: '.$someVar;
//This variable is concatenated and will show it's value: test

除此之外,您还可以使用sql注入。你真的应该用你的每个其他值之类的参数绑定你的查询。

答案 2 :(得分:0)

你可以试试这种方式

$stmt = $this->db_connection->prepare("INSERT INTO users (user_name, user_password_hash, user_email, user_activation_hash, user_registration_ip, user_registration_datetime, reff) VALUES(:user_name, :user_password_hash, :user_email, :user_activation_hash, :user_registration_ip, now(), :r))");

$stmt->bindParam(':user_name', $name);
//bind other params
..........................
$stmt->bindParam(':r', $r);

$name = 'one';
// other values
..........................
$r = $_GET['r'];
$stmt->execute();

答案 3 :(得分:0)

这就是你想要的。阅读marc评论中的链接,它们很重要。

$query_new_user_insert = $this->db_connection->prepare('INSERT INTO users (
    user_name, 
    user_password_hash, 
    user_email, 
    user_activation_hash, 
    user_registration_ip, 
    user_registration_datetime, 
    reff) 
VALUES(
    :user_name, 
    :user_password_hash, 
    :user_email, 
    :user_activation_hash, 
    :user_registration_ip, 
    :user_registration_datetime,
    :reff)');

$query_new_user_insert->execute(array(
    ':user_name' => $user_name,
    ':user_password_hash' => $user_password_hash,
    ':user_email' => $user_email,
    ':user_activation_hash' => $user_activation_hash,
    ':user_registration_ip' => $user_registration_ip,
    ':user_registration_datetime' => now(),
    ':reff' => $_GET['r']
    ));
$results = $query_new_user_insert->fetchAll();