2

I have this code

<?php
$servername = "localhost";
$username = "username";
$password = "pass";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

//Take the data from the request
$json = file_get_contents('php://input');
$data = json_decode($json, true);

$tableto = $data['tabledestination'];
$Question = $data['question'];
$answerone = $data['answerone'];
$answertwo = $data['answertwo'];
$answerthree = $data['answerthree'];
$Feedback = $data['feedback'];
$Correctanswer = $data['Correctanswer'];
$reparcial = $data['partial'];


//Insert the data into the database


$sql = "INSERT INTO mydatabase."$tableto" (Question, Answer_1, Answer_2, Answer_3, Correct_Answer, Parcial, Feedback)
VALUES ('" . $Question . "', '" . $answerone . "', '" . $answertwo . "', '" . $answerthree . "', '" . $Correctanswer . "', '" . $reparcial . "', '" . $Feedback . "')";

if ($conn->query($sql) === TRUE) {
    echo '{ "result": "success" }';
} else {
    echo '{ "result": "error" }';
}


//Close the connection
$conn->close();

?>

I pass the information through a URLRequest with HTTPMethod "POST" I want to send though the dictionary that I posted the table destination of my database so I can assign the destination of the table depending on the string I pass

davejal
  • 6,009
  • 10
  • 39
  • 82

1 Answers1

0

the following

$sql = "INSERT INTO mydatabase."$tableto" (Question, Answer_1, Answer_2, Answer_3, Correct_Answer, Parcial, Feedback)
VALUES ('" . $Question . "', '" . $answerone . "', '" . $answertwo . "', '" . $answerthree . "', '" . $Correctanswer . "', '" . $reparcial . "', '" . $Feedback . "')";

Has a problem because of the use of "

When declaring $sql you're using the concatenation of a string and you missed the . in your concatenation.

try changing to:

$sql = "INSERT INTO mydatabase." . $tableto . " (Question, Answer_1, Answer_2, Answer_3, Correct_Answer, Parcial, Feedback)
    VALUES ('" . $Question . "', '" . $answerone . "', '" . $answertwo . "', '" . $answerthree . "', '" . $Correctanswer . "', '" . $reparcial . "', '" . $Feedback . "')";
davejal
  • 6,009
  • 10
  • 39
  • 82