我有一个由Dreamweaver CS5创建的简单的insert.php公式。我希望能够在这个公式中输入我的新闻标题到列"news_headline"在我的数据库中,然后自动将这个标题添加到列"news_slug"小写和空格转换为减号。
如果我在表单的标题输入栏中输入"This is a headline",它就会在"news_headline"栏中输入"This is a headline",并在"news_slug"栏中添加"This -is-a-headline"。
到目前为止,我得到了以下代码:if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$news_headline = $_POST['news_headline'];
$news_slug = str_replace(' ', '-', $news_headline);
$insertSQL = sprintf("INSERT INTO tbl_news (news_headline, news_slug) VALUES (%s, %s)",
GetSQLValueString($_POST['news_headline'], "text"),
GetSQLValueString($_POST['news_slug'], "text"));
The Basic Rest
mysql_select_db($database_Jahrhundertkomet, $con);
$Result1 = mysql_query($insertSQL, $Jahrhundertkomet) or die(mysql_error());}
mysql_select_db($database_Jahrhundertkomet, $con);
$query_Jahrhundertkomet = "SELECT * FROM tbl_news";
$Jahrhundertkomet = mysql_query($query_Jahrhundertkomet, $con) or die(mysql_error());
$row_Jahrhundertkomet = mysql_fetch_assoc($con);
$totalRows_Jahrhundertkomet = mysql_num_rows($con);
mysql_free_result($con);
?>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
<table align="center">
<tr valign="baseline">
<td nowrap align="right">News_headline:</td>
<td><input type="text" name="news_headline" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right"> </td>
<td><input type="submit" value="Datensatz einfügen"></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1">
</form>
但是现在,当我在代码中输入标题时,它会得到错误:列'news_slug'不能为空。我需要做些什么来将我的输入从news_headline转换成news_slug ?
如果你只是想替换分数的空白,你可以使用str_replace:此外,您使用的$_POST['news_slug'];
不接收任何内容,因为在您的表单中没有名为news_slug
的输入。
试试这个:
$news_headline = $_POST['news_headline'];
$news_slug = str_replace(' ', '-', $news_headline);
并将这些行放在这一行的后面:
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {