我想提交格式为纯文本的javascript代码,以便在HTML页面上显示JS示例。
当我通过SQL提交以下代码段时,查询成功运行。但是,当我通过正确连接到数据库的textarea提交完全相同的文本时,查询不会完成,但不会显示错误。
<text>
var input = document.getElementById(/'numberInput/');
if (input < 0) {
alert(/'Number was negative./');
}else if(input <= 10){
alert(/'Number was between 0 - 10/');
}else{
alert(/'Number was greater than 10./');
}
</text>
当我运行以下查询时,该行已成功添加...
INSERT INTO `test` (`id`, 'data')
VALUES (NULL, '<text>\r\nvar input =
document.getElementById(/\'numberInput/\');\r\n
if (input < 0)
{\r\n
alert(/\'Number was negative./\');
\r\n}
else if(input <= 10)
{\r\n
alert(/\'Number was between 0 - 10/\');
\r\n}
else{\r\n
alert(/\'Number was greater than 10./\');\r\n}\r\n</text>'
);
下面是php代码,该代码接受来自textarea的字符串并将其保存在var $ data
中<?php
$con = mysqli_connect('127.0.0.1', 'root', '', '');
if(!$con)
{
echo 'Error Connecting to DB Server';
}
if(!mysqli_select_db($con, 'a3'))
{
echo 'Error Selecting DB';
}
$data = $_POST['data'];
$query = "INSERT INTO test (data) VALUES ('$data')";
if(!mysqli_query($con,$query))
{
echo 'Error Inserting Page to DB';
}else
{
echo 'Inserted Page to DB';
}
header("refresh:0; url=index.php");
?>