作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
connect_error); } //Take the data fro-6ren">
我有这个代码
<?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/
我是一名优秀的程序员,十分优秀!