【发布时间】:2012-02-06 18:11:15
【问题描述】:
这是正在使用的代码示例。我将此代码的修改版本用于我的另一个页面。另一个页面使用文本框而不是文本区域。文本框版本工作得很好。当我单击提交时,它只是返回到当前页面并且文本区域被擦除并且没有添加到数据库中。我做错了什么?
<? function renderForm($words, $type, $error){ ?>
<?php if ($error != '') { echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>'; } ?>
<form action="" method="post">
<select name="type">
<option value="intro">Intro</option>
<option value="phrase">Phrase</option>
<option value="phrase2">Phrase2</option>
<option value="phrase3">Phrase3</option>
<option value="phrase4">Phrase4</option>
<option value="phrase5">Phrase5</option>
<option value="phrase6">Phrase6</option>
<option value="phrase7">Phrase7</option>
<option value="phrase8">Phrase8</option>
<option value="phrase9">Phrase9</option>
<option value="phrase10">Phrase10</option>
<option value="phrase11">Phrase11</option>
<option value="phrase12">Phrase12</option>
<option value="phrase13">Phrase13</option>
<option value="phrase14">Phrase14</option>
<option value="phrase15">Phrase15</option>
<option value="phrase16">Phrase16</option>
<option value="phrase17">Phrase17</option>
<option value="phrase18">Phrase18</option>
<option value="phrase19">Phrase19</option>
<option value="phrase20">Phrase20</option>
<option value="keyword" selected>Keyword</option>
<option value="keyword2">Keyword2</option>
<option value="keyword3">Keyword3</option>
</select>
<br>
<textarea rows="20" cols="10" name="words"></textarea>
<input type="submit" name="submit" value="Add">
</form>
<?php
}
// connect to the database
include('connection.php');
// check if the form has been submitted. If it has, start to process the form and save it to the database
if (isset($_POST['submit']))
{
$words = $_POST['words'];
$type = $_POST['type'];
if ($words == '' )
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
// if either field is blank, display the form again
renderForm($words, $error);
}
else
{
$item = explode("\n", $words);
foreach($item as $words){
mysql_query("INSERT subs SET word='$words', type='$type'") or die(mysql_error());
}
}
}
else
// if the form hasn't been submitted, display the form
{ renderForm('',''); }
?>
【问题讨论】:
-
您有 SQL 漏洞。尝试将此代码放入您的文本区域:Example';删除表子; --