connect_error); } //Take the data fro-6ren">
gpt4 book ai didi

php - 如何在 INSERT INTO PHP 中使用字符串变量作为表目标

转载 作者:行者123 更新时间:2023-11-29 03:26:45 26 4
gpt4 key购买 nike

我有这个代码

<?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();

?>

我通过带有 HTTPMethod“POST”的 URLRequest 传递信息我想通过我发布的字典发送我的数据库的表目标,这样我就可以根据我传递的字符串分配表的目标

最佳答案

以下

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

因为使用"

而出现问题

当声明 $sql 时,您正在使用字符串的连接,而您错过了 .在你的连接中。

尝试更改为:

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

关于php - 如何在 INSERT INTO PHP 中使用字符串变量作为表目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34936683/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com