sql insert html,Insert html into sql table

本文探讨了在尝试将含有HTML内容的变量插入MySQL数据库时遇到的语法错误问题,并提供了使用mysql_real_escape_string方法来避免SQL注入攻击的有效解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I'm trying to insert a value into my sql table that has html in it: like follows

$story ="

$mont$day

lkjljt

Posted $name | $school, $date | Rating

$message
";

$db = mysql_connect("host", "user", "password");

mysql_select_db("db", $db);

if (!$db)

{

die('Could not connect: ' . mysql_error());

}

$sql = "INSERT INTO Post VALUES ('', '$date', '$time', '$story', '$school','$location', '$sex', '$zipcode', '$name');";

$result = mysql_query($sql);

if($result)

{ $success = " Your hookup has been submitted ";}

else{

$error = "something went horribly wrong" . mysql_error();}

?>

I keep getting a syntax error when I submit this page, and if I comment $story out, the query runs fine. How can I fix this?

The most likely reason is that $story contains single quotes, which will break the query. Protect it using mysql_real_escape_string

In general, this is a bad idea as it is open to SQL injection.

$sql = "INSERT INTO Post VALUES ('', '$date', '$time', '$story',

'$school','$location', '$sex', '$zipcode', '$name');";

At least, use mysql_real_escape_string which will protect the input for characters that have special meaning in a MySQL query. Use it on all textual columns.

$sql = "INSERT INTO Post VALUES ('', '$date', '$time', '" .

mysql_real_escape_string($story) . "','".

mysql_real_escape_string($school) . "','".

mysql_real_escape_string($location) . "', '$sex', '$zipcode', '" .

mysql_real_escape_string($name) ."');";

more

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值