0

伙计们,我希望有人能帮我找到这段代码中的问题,即阻止它在数据库中插入输入的数据。

我在这里看到过类似的线程,我纠正了一两件事,但它似乎仍然不起作用,这就是我创建这篇文章的原因。

此外,当它只有三个表单字段/变量时,此代码可以完美运行。现在我已经创建了更多,它不会将任何提交的值发送到数据库。

如果有人问,在同一页面中显示结果的函数工作正常,因为我尝试手动在数据库中插入值并显示在文件中,是什么让我认为问题确实出在插入函数上.

谁能帮我找出为什么它不去?

    <?php
mysql_connect("localhost","root","");
mysql_select_db("activitytest1");
if(isset($_POST['type']))       { $type = $_POST['type']; }
if(isset($_POST['description'])){ $description = $_POST['description']; }
if(isset($_POST['author']))     { $author = $_POST['author']; }
if(isset($_POST['pre']))        { $pre = $_POST['pre']; }
if(isset($_POST['while']))      { $while = $_POST['while']; }
if(isset($_POST['postact']))    { $postact = $_POST['postact']; }
if(isset($_POST['outcome']))    { $outcome = $_POST['outcome']; }
if(isset($_POST['path']))       { $path = $_POST['path']; }
if(isset($_POST['books']))      { $books = $_POST['books']; }
if(isset($_POST['grouping']))   { $grouping = $_POST['grouping']; }
if(isset($_POST['time']))       { $time = $_POST['time']; }
if(isset($_POST['submit']))     { $submit = $_POST['submit']; }

$dbLink = mysql_connect("localhost", "root", "");
    mysql_query("SET character_set_client=utf8", $dbLink);
    mysql_query("SET character_set_connection=utf8", $dbLink);

if (!empty($_POST['submit']))
{
if($type&&$description&&$author&&$pre&&$while&&$postact&&$outcome&&$path&&$books&&$grouping&&$time) {
$insert=mysql_query("INSERT INTO activitytest1 (type, description, author, pre, while, postact, outcome, path, books, grouping, time) VALUES ('".$_POST['type']."','".$_POST['description']."','".$_POST['author']."','".$_POST['pre']."', '".$_POST['while']."','".$_POST['postact']."','".$_POST['outcome']."','".$_POST['path']."', '".$_POST['books']."','".$_POST['grouping']."','".$_POST['time']."',)");
} else {
                    die ("Failed to connect to mysql: " . mysql_error());
}

}

$dbLink = mysql_connect("localhost", "root", "");
    mysql_query("SET character_set_results=utf8", $dbLink);
    mb_language('uni');
    mb_internal_encoding('UTF-8');

$getquery=mysql_query("SELECT * FROM activitytest1");
while($rows=mysql_fetch_assoc($getquery)) 
{
$type=$rows['type'];
$description=$rows['description'];
$author=$rows['author'];
$pre=$rows['pre'];
$while=$rows['while'];
$postact=$rows['postact'];
$outcome=$rows['outcome'];
$path=$rows['path'];
$books=$rows['books'];
$grouping=$rows['grouping'];
$time=$rows['time'];

echo '<br/>

<table width="909" border="1" align="center" cellpadding="5" cellspacing="0">
  <tr>
    <td width="125">' . $type . '</th>
    <td width="680">' . $description . '</th>
    <td width="120">' . $author . '</th>
  </tr></table> <br/>' .  $pre . '<br/>' . $while. '<br/>' . $postact . '<br/>' . $outcome . '<br/>' . $path . '<br/>' . $books . '<br/>' . $grouping . '<br/>' . $time . '
   <hr size="1"/> ' ;}

?>     
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test 1</title>
</head>
<body>

<br>
<table width="909" border="1" align="center" cellpadding="5" cellspacing="0">
  <tr>
    <th width="125" scope="col">Type</th>
    <th width="680" scope="col">Description</th>
    <th width="120" scope="col">Author</th>
  </tr></table>
  <br>
<form action="index.php" method="POST">

<table width="909" border="1" align="center" cellpadding="5" cellspacing="0">
     <td width="125"><label>

     </label>
       <select name="type" id="type" onChange="display(this,'Task','Semi-task','Practice','Exercise','Lead-in');">
         <option value="Unselected" selected="selected">Choose one:</option>
         <option value="Task">Task</option>
         <option value="Semi-task">Semi-task</option>
         <option value="Practice">Practice</option>
         <option value="Exercise">Exercise</option>
         <option value="Lead-in">Lead-in</option>
         <option value="Game">Game</option>
         <option value="Video">Video</option>
         <option value="Song">Song</option>
         <option value="Mimio">Mimio</option>
         <option value="Other">Other</option>
       </select></td>
    <td width="680"><div id="Semi-task" style="display: none;">
      Pre:<br>
    </div>
        <div id="Exercise"  style="display: none;">While:</div>
        <br>
        <div id="Practice" style="display: none;">
      Post:<br>
      <br>
       </div>
        <div id="Task"  style="display: none;"> Outcome: <br />
        <textarea name="description" cols="70" rows="2"></textarea>
            <br />
        </div>
        <div id="Lead-in" style="display: none;">
      Link/Path:<br>
      <br>
       </div></td>
    <td width="120"><input name="author" type="text" size="12" maxlength="25" /></td>
  </tr>
     <tr>
       <td colspan="3">      <textarea name="pre" id="Pre" cols="45" rows="5"></textarea>
                       <br /><textarea name="while" id="While" cols="45" rows="5"></textarea>
                       <br /><textarea name="postact" id="Post" cols="45" rows="5"></textarea>
                       <br /><textarea name="outcome" id="Outcome" cols="45" rows="5"></textarea>
                       <br /><input type="text" name="path" id="Path" />
                       <br /><input type="text" name="books" id="Books" />
                       <br /><input type="text" name="grouping" id="Grouping" />
                       <br /><input type="text" name="time" id="Time" />
                       </td>
     </tr>
    <tr>
       <td colspan="3"><input type="submit" name="submit" value="Comment" /></td>
     </tr>
</table>
</form>


</body>
</html>

伙计们,请友善,我是这方面的新手。:)

4

2 回答 2

0

首先检查您传递的数据是否有效。您的 $_POST['description'] 字段为空,因此指针未进入 mysql_query() 函数。

另一个想法是我在你的代码中看到你有 display : none; 具有 $_POST['description'] 元素的 div。删除该显示:无或删除该元素的验证部分。

于 2013-08-31T03:38:44.057 回答
0

在 mySQL 代码的末尾有一个额外的逗号。

尝试这个:$insert=mysql_query("INSERT INTO activitytest1 (type, description, author, pre, while, postact, outcome, path, books, grouping, time) VALUES ('".$_POST['type']."','".$_POST['description']."','".$_POST['author']."','".$_POST['pre']."', '".$_POST['while']."','".$_POST['postact']."','".$_POST['outcome']."','".$_POST['path']."', '".$_POST['books']."','".$_POST['grouping']."','".$_POST['time']."')");

于 2013-08-31T03:18:30.233 回答