Tituuss10 Tituuss10 - 6 months ago 26
HTML Question

MySql bind parameter is not working

I'm having a really annoying problem... I want to update a

div
's
background-image
with a value from my database. The problem is that the query isn't returning anything. I checked, if the data is sent correctly and it is. Only the query doesn't work.

<?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;
?>


and here is the 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" );
}
});
}

Answer

Okay so the first issue here is you should probably be using dbo but I'll give you an answer in the context of mySqli:

Then Now let's look at your myPHPController.php file:

//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;
?>

This code should compile fine provided you've checked everything I have mentioned