php表单mysql错误到数据库


php form mysql error to database

我有一个表单似乎不想将其数据写入数据库。我对php-mysql有些陌生。当我测试脚本时,页面会重新加载,只显示"0"。我不确定我错过了什么?感谢您的帮助。

形成

<form action="new.php" method="POST">
      <table>
        <tr>
          <td>Season Number: </td>
          <td><input type="text" name="season_sum" size="50" value="<? echo "$season_num";?>"></td>
        </tr>
        <tr>
          <td>Episode Number: </td>
          <td><input type="text" name="eps_num" size="50" value="<? echo "$eps_num";?>"></td>
        </tr>
        <tr>
          <td>Temp Episode Number: </td>
          <td><input type="text" name="temp_eps_num" size="50"></td>
        </tr>
        <tr>
          <td>Title: </td>
          <td><input type="text" name="title" size="50"></td>
        </tr>
        <tr>
          <td>Description: </td>
          <td><textarea type="text" name="descrip" cols="50" rows="7"></textarea></td>
        </tr>
        <tr>
          <td colspan="2"><input type="hidden" name="id">
            <input type="Submit" value="New Item"></td>
        </tr>
      </table>
    </form>

new.php

<?php
require "db.php";
//Test for user input
if (!empty($_POST[season_sum])&&
    !empty($_POST[eps_num])&&
    !empty($_POST[temp_eps_num])&&
    !empty($_POST[title])&&
    !empty($_POST[descrip]))
if ( ! empty($_POST['ID']))
$id = (int)$_POST['ID'];
else $id = 'NULL';
//Insert new entry
$query = "INSERT INTO `season` (`ID`, `season_num`, `temp_eps_num`, `esp_num`, `title`, `descrip`) VALUES ({$id}, '{$season_sum}', '{$eps_num}', '{$temp_eps_num}', '{$title}', '{$descrip}')";
// Send user back to the list once the update is successfully finished
header("Location: form.html");
?>

如果php代码抛出错误,请禁用new.php中的以下行:

//header("Location: form.html")

然后,您需要使用mysql_query执行$query。

$query = "INSERT INTO ... ";
mysql_query($query);

您实际上从来没有发送查询,只是定义了查询字符串。要发送它,您需要使用mysql_query($query)。

有关更多详细信息,请参阅文档。http://php.net/manual/en/function.mysql-query.php

不确定"0",但总的来说,您的代码看起来像是为了可读性而裁剪掉的。如果不是。。。

    if (!empty($_POST[season_sum]) && !empty($_POST[eps_num]) && !empty($_POST[temp_eps_num]) && !empty($_POST[title]) && !empty($_POST[descrip]))
    {
        if ( !empty($_POST['ID']))
            $id = (int)$_POST['ID'];
        else 
            $id = 'NULL';
        // mysql_real_escape_string() example
        $descrip = mysql_real_escape_string($_POST['descrip']);
        //Insert new entry
        $query = mysql_query("INSERT INTO `season` (`ID`, `season_num`, `temp_eps_num`, `esp_num`, `title`, `descrip`) VALUES ({$id}, '{$season_sum}', '{$eps_num}', '{$temp_eps_num}', '{$title}', '$descrip')") or die(mysql_error());
        // Send user back to the list once the update is successfully finished
        header("Location: http://www.yoursite.com/form.html");
        exit;
    }

我没有加入转义,因为建议您用mysql_real_aescape_string()包装数据库插入字符串更容易。除此之外,您从来没有实际运行过查询,也没有用大括号包装if语句。我甚至不知道页面在这种情况下会怎么想。

请尝试应用这些更改,如果错误仍然存在,请告诉我们。

注意-我在你的标题位置后添加了出口。此外,我把一个完整的url路径放在某处或其他地方,我听说这是更好的做法。不过,我没有支持这种说法。只是我在什么地方听到的一件事。

mysql_real_eescape_string()解释:要使用它,您必须打开一个连接。这通常在您的db类中处理,所以如果您发现它什么都不做,请查看mysql_connect();要使用,只需这样调用:

$sql = mysql_query("SELECT * FROM `table` WHERE `id` = '".mysql_real_escape_string($string_to_escape)."'");

它不会添加单引号包装。它所做的只是帮助清理字符串以防止常见的sql注入攻击。