通过PHP将类插入数据库

时间:2013-08-31 15:52:54

标签: php mysql css database interaction

<!DOCTYPE>
<html>
<head>
<meta charset=utf-8" />
<title> data input form </title>
<link rel="stylesheet" type="text/css" href="css/form.css"/>
</head>

<body>

    <h1> Insert data here</h1>

    <form method="get" action="insert-data.php">
        <input type="hidden" name="submitted" value="true" />
        <fieldset>
            <ul>
                <li><label> English Word <input type="text" name="englishWord"/>                                 </label></li>
                <li><label> Beginner German <input type="text" name="beginnerGerman"/>      </label></li>
            </ul>
            <input type="submit" value="add new record" />
        </fieldset>
    </form>
    <? php

    echo $newrecord;

    ?>
</body>
</html>

<?php
//connect to database
$host = "localhost";
$username = "root";
$password = "mypass";
$database = "dictionary";
$table = "engdic";

mysql_connect("$host", "$username", "$password") or die(mysql_error());
mysql_select_db("$database") or die(mysql_error());

$englishWord = $_GET['englishWord'];
$beginnerGerman=$_GET['beginnerGerman'];
$mysql = "INSERT INTO $table (englishWord,beginnerGerman) VALUES ('$englishWord','<div class='beginner_german'>$beginnerGerman</div>')";
if(!mysql_query($mysql))
die(mysql_error());

echo "data inserted";
mysql_close();
?>
//INSIDE DATABASE 
<div class=`beginner_german`>Beginner German Word</div>

所以我将有输入表格,用户将把翻译单词添加到数据库中,当用户添加时可以说出单词Apple,它还会将div与css类添加到数据库中。这样当另一个用户将搜索单词Apple时我可以用css类来设置它。我的问题是 当我尝试使用class =“beginner_german”添加div时,我在php中出现错误,当我使用clas ='beginner_german'时,它在数据库中插入正常,但是当我无法调用css类时,因为语法必须是class =“ beginner_german”。那么解决方案是什么呢?谢谢你的时间。

1 个答案:

答案 0 :(得分:0)

你的双引号需要反斜杠

$mysql = "INSERT INTO $table (englishWord,beginnerGerman) VALUES ('$englishWord','<div class=\"beginner_german\">$beginnerGerman</div>')";