【发布时间】:2023-03-09 06:03:01
【问题描述】:
我在将字符串插入 mysql 时遇到问题。我可以插入数字但不能插入字符串。有人可以看看这个吗?感谢您的帮助。
我的表(剧集)看起来像
id int(11) notnull
serie int(11) notnull
name varchar(255) notnull
comment varchar(255) notnull
embed varchar(255) notnull
这里是 PHP 代码:
<?php
require_once "config.php";
$connect = mysql_connect( "$db_host", "$db_user", "$db_pw") or die(mysql_error());
mysql_select_db("$db_name") or die( mysql_error() );
if(isset($_POST['pridatdiel'])) {
mysql_query("INSERT INTO `episodes` (`id`, `serie`, `name`, `comment`, `embed`)
VALUES ('', '$cisloserie', '$menodielu', '$popis', '$embed')");
header("refresh: 0;");
}
function stripinput($text) {
if (!is_array($text)) {
$text = stripslash(trim($text));
$text = preg_replace("/(&)+(?=\#([0-9]{2,3});)/i", "&", $text);
$search = array("&", "\"", "'", "\\", '\"', "\'", "<", ">", " ");
$replace = array("&", """, "'", "\", """, "'", "<", ">", " ");
$text = str_replace($search, $replace, $text);
} else {
foreach ($text as $key => $value) {
$text[$key] = stripinput($value);
}
}
return $text;
}
if(isset($_POST['menodielu'])) {$menodielu = stripinput($_POST['menodielu']);}
if(isset($_POST['cisloserie'])) {$cisloserie = stripinput($_POST['cisloserie']);}
if(isset($_POST['popis'])) {$popis = stripinput($_POST['popis']);}
if(isset($_POST['embed'])) {$embed = stripinput($_POST['embed']);}
echo '
<form method="post">
meno dielu<input type="text" name="menodielu">
<select name="cisloserie">';
$data = mysql_query("SELECT * FROM `series`") or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
echo "<option value=".$info['serie_id'].">".$info['serie_id']."</option>";
}
echo '
</select>
popis<input type="text" name="popis">
embed<input type="text" name="embed">
<input type="submit" value="pridať diel" name="pridatdiel">
</form>
';
?>
【问题讨论】:
-
您不检查错误。如果您不这样做,您如何期望解决这个问题?使用
mysql_error()获取 MySQL 报告的错误。 -
如果你使用准备好的语句,你已经完成了。而且您不必先进行所有输入修改。