2

I know this site doesn't like "spot my mistake" code, but I'm desperate. I have a website that needs to access user-specific data from a database (PHP), convert the data into a JSON file, and then change a HTML header to display that specific data. The database table has the user email, password, and class name, among other things. I have a login page that establishes the session variables for the email and the password. When the user logs in, I want their class name to be entered into HTML text. I've used dozens of sources, mostly W3schools, and came up with this code:

PHP:

<?php
session_start();

 header("Content-Type: application/json; charset=UTF-8");
 $obj = json_decode($_GET["x"], false);

 if (!$obj) {
 die(mysqli_error());
 }

    $servername = "localhost";
$username = "id5143969_enviroquest1";
$password = "codeteam1";
$database = "id5143969_enviroquest1";
$link = mysqli_connect($servername, $username, $password, $database);

$result = $link->query("SELECT UserClassName FROM ".$obj->UserInfo1." WHERE ".$obj->UserEmail."= '". mysqli_real_escape_string($link, 
$_SESSION['useremail']) . "' and ".$obj->UserPassword." = '" . mysqli_real_escape_string($link, $_SESSION['userpassword']) . "'");

if (!$result) {
die(mysqli_error());
}

$_SESSION['classname'] = $result->fetch_assoc();

if (!$_SESSION['classname']) {
die(mysqli_error());
}

     echo json_encode($_SESSION['classname']);

Javascript:

function getclassname() {
var obj, dbParam, xmlhttp, myObj, x, txt = "";
obj = { "UserInfo1":"UserClassName"};
dbParam = JSON.stringify(obj);
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
    myObj = JSON.parse(this.responseText);
    document.getElementById("UserClassName").innerHTML = myObj;
    }
};
xmlhttp.open("GET", "php2.php" + dbParam, true);
xmlhttp.send();
}

HTML:

<h1 class="text-center" id="UserClassName" name="UserClassName" onload= 
"getclassname()"> </h1>

I have no idea what's going wrong, and am too new to coding to figure it out by myself.

Mekade24
  • 25
  • 5
  • Welcome to the site! Check out the [tour](https://stackoverflow.com/tour) and the [how-to-ask page](https://stackoverflow.com/help/how-to-ask) for more about asking questions that will attract quality answers. You can [edit your question](https://stackoverflow.com/posts/49466941/edit) to include more information. What is happening, and what do you expect should be happening? Also, would you please add the relevant parts of your HTML? I note your PHP code is never using `$obj` after decoding it. – cxw Mar 24 '18 at 16:12
  • I added the relevant html and more data on what I need it to do. To be completely honest, I don't understand what the obj variable is supposed to do, I've just pieced everything together from existing code on this site and w3schools and it seems to be necessary. There's also an obj variable in the javascript – Mekade24 Mar 24 '18 at 16:53

1 Answers1

1

Try this (I can't test it, but)—

PHP:

  • Remove the ?> at the end of the file. Pure-PHP files should always leave off the closing tag.
  • Change (MYSQLI_ASSOC) to just () - per this and the docs, you don't need it.

Javascript:

  • Remove the session_start() call
  • Change

    for (x in myObj) {
        txt += myObj[x].name + "<br>";
    }
    

    to

    txt = myObj.UserClassName
    

    The fetch_assoc() call in PHP gives you a mapping that uses the database field names ("each key in the array represents the name of one of the result set's columns" per the docs) for a single row. Therefore, if the JSON encode/decode worked OK, you should be able to refer directly to the field.

To test this, in the developer tools, set a breakpoint at the txt = ... line and see what myObj is.

I don't think you need $obj, dbParam, or ?x=, but I would not suggest changing them unless the above doesn't help.

Good luck!

cxw
  • 16,685
  • 2
  • 45
  • 81
  • Thank you for the suggestions, unfortunately the code is still not working. – Mekade24 Mar 24 '18 at 17:17
  • @Mekade24 What errors are you getting in the PHP or JavaScript? Did you try inspecting `myObj` in the browser debugger? – cxw Mar 24 '18 at 17:24
  • I'm not getting any errors on the PHP or Javascript end. I don't know how to set a breakpoint or use the browser debuggger. I have been coding for less than a week. – Mekade24 Mar 24 '18 at 17:41
  • @Mekade24 Ah! Take a look at [this tutorial for Chrome](https://developers.google.com/web/tools/chrome-devtools/javascript/) or [this for Firefox](https://developer.mozilla.org/en-US/docs/Tools/Debugger). I would suggest leaving this question open until you have learned the tools and have a bit more information. Good luck! – cxw Mar 24 '18 at 17:51
  • Even with the tutorial, I can't get the browser debugger to work and I really don't have the time to learn a whole new program. I mean, I got this code practically letter-for-letter from w3schools. There has to be an identifiable error I'm making in this code. Websites get information like this all of the time, so it the method of doing it must be popular and easily reproducible. I can't wrap my head around why there's no answer anywhere to this seemingly simple function. – Mekade24 Mar 24 '18 at 18:20
  • I understand that you are frustrated; it's perfectly legitimate. I've given you what I have. One general thought, though --- in programming, [data is often more important than code](https://softwareengineering.stackexchange.com/q/163185/105281). Knowing the value of `myObj` at the point you are setting `txt` will probably make the bug evident, but without that I would be guessing if I went any farther. What I'm saying is that an "identifiable error" is not necessarily the same as an "*easily* identifiable error" :) . – cxw Mar 24 '18 at 18:24
  • I understand. Thank you for your help, I'm just going to keep changing things and hopefully something clicks. – Mekade24 Mar 24 '18 at 18:27