Aimee Katherine
asked on
SQL doesn't UPDATE the AJAX call when WHERE is specified
Can somebody please help me? I'm writing code to tell when someone enters or leaves a webpage chatroom. The SQL query doesn't work when I add the WHERE portion. When I remove the WHERE username='$userLoggedIn' part, it does update, but it'll list all the users in the database. When I echo the variable $userLoggedIn right before the SQL query, it gives me the correct value each time, so I don't know why this value isn't passing to the SQL query. Another curious thing is that when I take the AJAX PHP code (from userfetch.php) and put it back to the start of the main page (where the call itself was made publicchat.php), the code updates and works well. However, the page ends up looking really messed up though, but I can see that the code executed and UPDATEd on that page. It won't update where it's supposed to, though.
I should also mention that the code does work as it should if I assign a value to $userLoggedIn. This leads me to believe that the problem lies in the variable $userLoggedIn. I did try changing the name itself, but had the same problem. Any thoughts?
Any help is so greatly appreciated!!!
Here is the code:
userfetch.php:
<?php
include_once('config/confi g.php');
if(isset($_POST['userLogge dIn'])){
$userLoggedIn = $_POST['userLoggedIn'];
//enter users into database
if(isset($_POST['userincha t'])){
$userinchat = $_POST['userinchat'];
if ($userinchat === "yesway"){
$date_time_now = date("Y-m-d H:i:s");
echo $userLoggedIn;
mysqli_query($conn, "UPDATE users SET in_chatroom='yes', lasttime_spotted='$date_ti me_now' WHERE username='$userLoggedIn' ");
}
}
?>
publicchat.php
<script>
function users(){
var xhr1 = new XMLHttpRequest();
xhr1.open('POST' , 'userfetch.php' , true);
xhr1.setRequestHeader('con tent-type' ,'applicat ion/x-www- form- urlencoded');
xhr1.send('userinchat=' + userinchat + '&userLoggedIn=' + username);
xhr1.onreadystatechange = function()
{
document.getElementById('l oginperson ').innerHT ML = xhr1.responseText;
}
}
</script>
I should also mention that the code does work as it should if I assign a value to $userLoggedIn. This leads me to believe that the problem lies in the variable $userLoggedIn. I did try changing the name itself, but had the same problem. Any thoughts?
Any help is so greatly appreciated!!!
Here is the code:
userfetch.php:
<?php
include_once('config/confi
if(isset($_POST['userLogge
$userLoggedIn = $_POST['userLoggedIn'];
//enter users into database
if(isset($_POST['userincha
$userinchat = $_POST['userinchat'];
if ($userinchat === "yesway"){
$date_time_now = date("Y-m-d H:i:s");
echo $userLoggedIn;
mysqli_query($conn, "UPDATE users SET in_chatroom='yes', lasttime_spotted='$date_ti
}
}
?>
publicchat.php
<script>
function users(){
var xhr1 = new XMLHttpRequest();
xhr1.open('POST' , 'userfetch.php' , true);
xhr1.setRequestHeader('con
xhr1.send('userinchat=' + userinchat + '&userLoggedIn=' + username);
xhr1.onreadystatechange = function()
{
document.getElementById('l
}
}
</script>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER