我是php的新手,正在努力学习。这个例子不会上线!
我有一个html文本框,并发布到php。我正试图使用分解函数来分解字符串,并希望将这些单独的字符串添加到我选择的数据库和表中,作为数据库的"列"。任何帮助或建议都将非常感谢
$getMessage = $_POST["dbMessages"];
$questions = $_POST["dbMessagesName"];
$answers = $_POST["dbMessages"];
$combined = array_combine($questions, $answers);
$dbConnectionT = mysqli_connect($host, $user, $password, $dbName);
// create loop to assign input boxes together
foreach($combined as $question => $answer)
{
// create the tables and columns from the message text box
$sqlCreateTable = "CREATE TABLE " . $question . "(
ADD THE ANSWER AS COLUMN NAMES
)";
$answerSplit = explode(" ", implode($getMessage));
// if the connection and sql query is true echo for debugging
if ($dbConnectionT->query($sqlCreateTable) == TRUE) {
echo "created table";
print "{$question} = {$answer} ";
echo "</br>";
}
}
上面是代码,答案都在同一个用空格分隔的文本框中,我正试图将所有答案作为列添加到表单中创建的表中。
提前感谢
每条评论-
如果所有列名都在一个字符串中,并用空格分隔,则不需要使用分解。只需将空格替换为VARCHAR(60),
$columns = "`".str_replace(" ", "` VARCHAR(60), `", $answers)."` VARCHAR(60)";
// wrap column names with ` to prevent issues with reserved words
然后你可以在你的查询中使用这个
$sqlCreateTable = "CREATE TABLE " . $question . "( ". $columns. " )";