-1

我正在开发一个工作问题门户。现在我有一个问题,每个问题应该是一个按钮“添加到测试”。如果单击此按钮,则内容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 :( 
} 
?>

但它现在不起作用。问题出在哪里?

4

4 回答 4

2

将变量放在单引号之间会将其视为文本:

$sql = "INSERT INTO teste (content) VALUES ('{$div}')";

试试这个:

$sql = "INSERT INTO teste (content) VALUES ('". $div . "')";
于 2013-03-15T09:38:49.247 回答
1

我有问题的答案

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

有用 :-)

于 2013-03-16T17:34:29.153 回答
1

你在这里忘记了密码

$con = mysql_connect('127.0.0.1', 'root','your_password');
于 2013-03-15T09:37:33.380 回答
0
$sql = "INSERT INTO teste (content) VALUES ('". addslashes($div) . "')";
于 2013-03-15T10:57:59.560 回答