0

I created a function to output a table row with data from a JS file. But I'd like to retrieve data from the MySQL database I made instead. Is it possible?

Function:

function outputCartRow(name, id, email, proj_title){
    document.write('<tr>');
    document.write('<td>'+name+'</td>');
    document.write('<td>'+id+'</td>');
    document.write('<td>'+email+'</td>');
    document.write('<td>'+proj_title+'</td>');
    document.write('</tr>');
}

I created a separate form that submits all the textfield input to the database, for reference:

PHP text:

<?php

    $conn = mysqli_connect('127.0.0.1:3307', '', '');

    if(!$conn){
        echo 'Missing server';
    }

    if(!mysqli_select_db($conn,'fyp')){
        echo 'Missing database';
    }
    
    $stud_name = $_POST['stud_name'];
    $stud_id = $_POST['stud_id'];
    $stud_email = $_POST['stud_email'];
    $proj_title = $_POST['proj_title'];
    
    $sql = "INSERT INTO student_assignment(stud_name, stud_id, stud_email, proj_title) values ('$stud_name', '$stud_id', '$stud_email', '$proj_title')";
    
    if(!mysqli_query($conn, $sql)){
        echo 'Update error';
    }
    else{
        echo 'Update successful';
    }
    
    header("refresh:2; url=supervisor_pa.html");

?>

Is there a way to retrieve all the data and refer it to the function?

  • Your script is vulnerable to [SQL Injection Attack](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). Even if [you are escaping variables, its not safe](https://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string])!  You should always use [prepared statements and parameterized queries](https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) in either the `MYSQLI` or `PDO` instead of concatenating user provided values into the query. – Andrea Olivato Apr 09 '22 at 12:10
  • To retrieve data from your PHP backend you can use an Ajax call. Javascript calls PHP, PHP calls Mysql. You can find plenty of result with a search. – Andrea Olivato Apr 09 '22 at 12:11

0 Answers0