我的PHP代码没有输入数据到mySQL并且没有给我任何反馈

时间:2016-05-09 23:49:29

标签: php html mysql

我不确定为什么我的数据库没有从我的网站接收信息。此外,没有错误消息弹出,所以我不确定数据是否会发生任何变化。它似乎是让我连接到数据库,但当我点击添加员工我只是得到一个空白页面。有什么建议? 编辑:我已经将我的代码更改为只输入一个变量,但我仍然只返回一个空白页。

<html>
   <head>
      <title>Add New Record in MySQL Database</title>
   </head>
   <body>
      <?php
         if(isset($_POST['add'])) {
            $dbhost = '';
            $dbuser = 'j';
            $dbpass = 'os';
            $conn = mysql_connect($dbhost, $dbuser, $dbpass);

            if(! $conn ) {
               die('Could not connect: ' . mysql_error());
            }

            if(! get_magic_quotes_gpc() ) {
               $emp_name = addslashes ($_POST['employee_name']);

            } else {
               $emp_name = $_POST['employee_name'];
            }

            $sql = "INSERT INTO employee ". "(employee_name) ". "VALUES('$emp_name')";

            mysql_select_db('test_db');
            $retval = mysql_query( $sql, $conn );

            if(! $retval ) {
               die('Could not enter data: ' . mysql_error());
            }

            echo "Entered data successfully\n";

            mysql_close($conn);
         } else {
      ?>
            <form method = "post" action = "<?php $_PHP_SELF ?>">
               <table width = "400" border = "0" cellspacing = "1" cellpadding = "2">
                  <tr>
                     <td width = "100">
                        Employee Name
                     </td>
                     <td>
                        <input name = "employee_name" type = "text" id = "employee_name">
                     </td>
                  </tr>

                  <tr>
                     <td width = "100"> 
                     </td>
                     <td> 
                     </td>
                  </tr>

                  <tr>
                     <td width = "100"> 
                     </td>
                     <td>
                        <input name = "add" type = "submit" id = "add" value = "Add Employee">
                     </td>
                  </tr>
               </table>
            </form>
            <?php
         }
            ?>
   </body>
</html>

1 个答案:

答案 0 :(得分:1)

$sql = "INSERT INTO employee ". "(employee_name) ". "VALUES('$emp_name', NOW())";

您在sql中传递了2个值,但只指定了一列。 employee_name

你可能需要删除NOW()值或添加另一列(employee_name,date_added)

$sql = "INSERT INTO employee ". "(employee_name) ". "VALUES('$emp_name')";

$sql = "INSERT INTO employee ". "(employee_name, date_added) ". "VALUES('$emp_name', NOW())";