我正在开设一个问题门户网站。现在我有一个问题,每个问题应该是一个按钮“添加到测试”。如果单击此按钮,content-div
的内容将保存到MySQL表中。我使用以下代码....
主要代码
<div id="content">
This content shut be safe in the MySQL table..
</div>
<script>
$(function(){
$('input.add').on('click',function(){
var div_contents = $("#content").html();
$.post('tomysql.php', { content: div_content });
});
});
</script>
<input type="button" class="add" value="Add to Test" />
tomysql.php
<?php
$con = mysql_connect('127.0.0.1', 'root');
mysql_select_db($teste, $con);
$div = mysql_real_escape_string($_POST['content']);
$sql = "INSERT INTO teste (content) VALUES ('{$div}')";
$query = mysql_query($sql, $con);
if($query) {
// Success!
} else {
// Failure :(
}
?>
但它现在不起作用。问题在哪里?
答案 0 :(得分:2)
将变量放在单引号之间将其视为文本:
$sql = "INSERT INTO teste (content) VALUES ('{$div}')";
试试这个:
$sql = "INSERT INTO teste (content) VALUES ('". $div . "')";
答案 1 :(得分:1)
你忘了密码
$con = mysql_connect('127.0.0.1', 'root','your_password');
答案 2 :(得分:1)
ich有问题的答案
<div id="content">
<a href="google.de">google</a>
Dieser inhalt soll in die MySQL Tabelle gespeichert werden.....
</div>
<script language="javascript">
function test()
{
var div_content = document.getElementById("content").innerHTML;
location.href = "tomysql.php?c=" + div_content ,"tab";
}
</script>
和tomysql.php
<?php
if (isset($_GET['c']))
{
$con = mysql_connect('127.0.0.1', 'root', '');
mysql_select_db('teste', $con);
$sql = "INSERT INTO teste (content) VALUES ('". $_GET['c']. "')";
$query = mysql_query($sql, $con);
if($query) {
echo("ok");
} else {
echo("nicht ok");
}
mysql_close($con);
}
?>
它有效: - )
答案 3 :(得分:0)
$sql = "INSERT INTO teste (content) VALUES ('". addslashes($div) . "')";