page1.php中
echo '<form action="page2.php" name="myform"><table>
<tr><td>name:<input type="text" name="n"></td></tr>
<tr><td>Choose Desc<select name="desc" value="0">
$descriptions = mysql_query('SELECT desc FROM desc_table');
while($row=mysql_fetch_array($descriptions))
{
$des = $row['desc'];
echo '<option>'.$des.'</option>';
}
</select>
</td></tr>';
<tr><td>Description:<textarea rows="10" cols="10" name="desc" value="1"></textarea></td> </tr>
<tr><td><input type="submit" value="submit"></td></tr>
</table>
</form>';
使page2.php
$n = $_POST['n'];
$desc = $_POST['desc'];
$sql = "INSERT INTO new (name,desc)
VALUES ($n,$desc)";
mysql_query($sql);
我需要通过选择描述或在textarea中键入描述来存储一个描述,但只应将一个描述存储到数据库中。
答案 0 :(得分:2)
你的表格严重搞砸了:
<form name="myform">
<table>
<tr>
<td>name:<input type="text" name="n"></td>
</tr>
<tr>
<td>Choose Desc
<select name="desc" value="0">
<?php
$descriptions = mysql_query('SELECT desc FROM desc_table');
while($row=mysql_fetch_array($descriptions))
{
$des = $row['desc'];
echo '<option>'.$des.'</option>';
}
?>
</select>
</td>
</tr>';
<tr>
<td>Description:<textarea rows="10" cols="10" name="desc" value="1"></textarea></td>
</tr>
<tr><td><input type="submit" value="submit"></td></tr>
</table>
</form>
答案 1 :(得分:1)
改为:
$n = $_REQUEST['n'];
$desc = $_REQUEST['desc'];
$sql = "INSERT INTO new (name,desc)
VALUES ("$n","$desc")";
mysql_query($sql);
并为您的代码添加方法
<FORM action="resultscript.php" method="POST"></FORM>
再试一次; - )
BTW:如果您在SQL中放入的变量可能包含字符串,则需要将它们放在双引号中(“)
答案 2 :(得分:0)
你到处都缺少引号,你似乎错过了启动和完成PHP代码:
echo '<form name="myform">
<table>
<tr><td>name:<input type="text" name="n"></td></tr>
<tr><td>Choose Desc
<select name="desc" value="0">';
$descriptions = mysql_query('SELECT desc FROM desc_table');
while($row=mysql_fetch_array($descriptions))
{
$des = $row['desc'];
echo '<option>'.$des.'</option>';
}
echo '
</select>
</td></tr>
<tr><td>Description:<textarea rows="10" cols="10" name="desc" value="1"></textarea></td></tr>
<tr><td><input type="submit" value="submit"></td></tr>
</table>
</form>';