-2

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);

我需要通过选择描述或在文本区域中输入描述来存储任一描述,但只有一个描述应该存储到数据库中。

4

3 回答 3

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>
于 2012-08-10T12:07:47.730 回答
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>

然后再试一次 ;-)

顺便说一句:如果您放入 SQL 中的变量可能包含字符串,则需要将它们放在双引号 (") 中

于 2012-08-10T12:25:08.880 回答
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>';
于 2012-08-10T12:08:20.583 回答