Jump to content

using php to retrieve data after updating mysql


jdauthre

Recommended Posts

I have a web site that lists students in a database on a server, and can add a students details. It then relists the students pulling the new list off the database.  All worked well until I transferred everything to a new webhost.   It still works to the point of relisting when I get the old list not the updated one. Checking the database(mysql) directly it has actually put the new student in. and if I log out of the web site and back in the new student's details are there.  how do get this to work? relevent code is below:

html/javascript:

function listStudents(password){

var oReq = new XMLHttpRequest();
    oReq.onload = function() {
        // This is where you handle what to do with the response.
        // The actual data is found on this.responseText
    allTheStudents =  JSON.parse(this.responseText);
    
    if(allTheStudents){
     select = document.getElementById("audioFile");
for(index in allTheStudents) {
    select.options[select.options.length] = new Option(allTheStudents[index], index);
            }    

    }else{
    alert("There are no students");
    }

document.getElementById("fileLabel").innerHTML= "Select Student(s) from the list:";
document.getElementById("fileLabel").style = "display:show;font-family: Calibri; font-size: 16px; font-weight: bold;color:#E3C208;";
   };
    oReq.open("get", "listStudents.php?x=" + username, true);
    oReq.send();
oReq.close;
}

function addStudent(){
 
    document.getElementById("familyNameId").innerHTML="";
 document.getElementById("qInput").style="display:none";

familyName = document.getElementById("familyNameId").value;
givenName = document.getElementById("givenNameId").value;
email = document.getElementById("emailId").value;
studentdetails = familyName+","+ givenName +","+email+","+ username ;
listNewStudent = familyName+" "+ givenName +" "+email;
addaStudent = JSON.stringify(studentdetails);
var oReq = new XMLHttpRequest();
    oReq.onload = function() {
    };
    oReq.open("get", "saddStudent.php?x=" + addaStudent, true);
    oReq.send();
    oReq.close;
    //alert("username "+ username);
    alert("about to list");
    

    select = document.getElementById("audioFile");
    //select.options = new Option(listNewStudent);
    select.options[select.options.length] = new Option(listNewStudent);

listStudents();
}

php:
<?php

$studentdetails = json_decode($_GET["x"],false);

//$var = file_get_contents('php://input');
//$studentArray = json_decode($var);

//$length = count($studentArray);
$servername = xxxxx
$user =  xxxxxx
$pass=xxxxx
$db ='xxxxxx

    // Create connection
    
    $con = new mysqli($servername, $user, $pass, $db) or die("Unable to connect to Database: " . $db);

    if ($con->connect_error) {
      die("Connection failed: " . $con->connect_error);
    }

    
    $studentElements = str_getcsv($studentdetails,",","");
    $fname=$studentElements[0];
    $gname=$studentElements[1];
    $ename=$studentElements[2];
    
    $sqla = "INSERT INTO students (familyName,givenName,email,)
    VALUES ('$fname','$gname','$ename);";

    
if (mysqli_multi_query($con,$sqla) === TRUE) {
  //      echo "New records created successfully";
  } else {

  }

  $con->close();
?>

Thanks in advance

Edited by jdauthre
minor correction
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...