表单不是在数据库中插入输入的数据,但不显示任何错误

时间:2013-08-31 03:10:22

标签: php database insert submit

伙计们,我希望有人可以帮我找到这段代码中的问题,使其无法在数据库中插入输入的数据。

我在这里看过类似的帖子,我纠正了一两件事,但似乎没有用,这就是我创建这篇文章的原因。

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

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

任何人都可以帮我找到原因吗?

    <?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>

请你好,伙计们,我是一个全新的人。 :)

2 个答案:

答案 0 :(得分: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']."')");

答案 1 :(得分:0)

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

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