MySql绑定参数不起作用

时间:2016-05-24 15:45:09

标签: javascript php html mysql

我有一个非常烦人的问题...我想用我的数据库中的值更新div background-image。问题是查询没有返回任何内容。我检查过,如果数据发送正确,那就是。只有查询无效。

<?php 

include_once '../php/functions.php';
include_once '../php/db_connect.php';

session_start();


$stmt3 = $mysqli->prepare("SELECT avatar FROM users WHERE username = ? LIMIT 1") ;
$stmt3->bind_param('s', $_POST['user1']);
$stmt3->execute();
$stmt3->store_result();
$stmt3->bind_result($user_avatar);
$stmt3->fetch();
echo $user_avatar;
?>

这是JS:

updatepicture('#user1_image' , document.getElementById("user1").innerHTML);
function updatepicture(ussid , uss)
{
var c1='url(\'../images/users/';
var c2='\')';
$.post("../php/get_avatar_game.php",
{
    user1:uss
},
function(data, status)
{
    if( status == 'success' )
    {
    alert(data);
        $(ussid).css("background-image", c1.concat(data,c2));   
    }
    else
    {
        alert( "Proleme la incarcarea imaginii" );
    }   
});
}

1 个答案:

答案 0 :(得分:0)

好的,所以这里的第一个问题是你应该使用dbo,但我会在mySqli的背景下给你一个答案:

然后现在让我们看看你的myPHPController.php文件:

//The first issue here is that you don't have an opening PHP tag:
<?php
include_once '../php/functions.php';
include_once '../php/db_connect.php';

//Next issue is you're using Sessions
//Don't use sessions as this makes your program vulnerable
session_start();

//Next add a variable for user1
$user_one = $_POST['user1']

// You do know you're only allowing one record here?
// And double check the name of the table in your database is definitely avatar
$stmt3 = $mysqli->prepare("SELECT avatar FROM users WHERE username = ? LIMIT 1") ; 
$stmt3->bind_param('s', (string)$userOne); //Add the variable
$stmt3->execute();
$stmt3->store_result();
$stmt3->bind_result($user_avatar); // Have you defined $user_avatar
$stmt3->fetch();
echo $user_avatar;
?>

如果您已经检查了我提到的所有内容,那么此代码应该可以正常编译