【问题标题】:php form mysql error to databasephp表单mysql错误到数据库
【发布时间】:2012-03-02 23:40:14
【问题描述】:

我的表单似乎不想将其数据写入我的数据库。我对 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 mysql database input


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 我已进行了您建议的更改,但提交时仍为“0”
    • 一旦我回到列表中,数据输入正确
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      不确定“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_escape_string() 包装您的数据库插入字符串更容易。除此之外,您实际上从未运行过查询,并且您不会用花括号将 if 语句包装起来。我什至不知道在这种情况下页面会怎么想。

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

      注意 - 我在您的标题位置之后添加了退出。另外,我在某个地方或另一个地方放了一个完整的 url 路径,我听说这是更好的做法。不过,我不支持这种说法。只是我在某处听到的。

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

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

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

      【讨论】:

      • 看起来我确实需要添加 mysql_real_escape_string() 因为某些描述中有引号
      • 您能否提供一个代码示例,说明如何添加转义字符串以让我的$descript 包含引号。
      • 你的意思是像下面这样吗? mysql_real_escape_string($query);
      • 已更新。顺便说一句,您似乎没有将您的帖子变量分配给您在查询中调用的简单形式。例如,看看我对 $descrip 做了什么。
      • 好的,我正在尝试你的新示例
      猜你喜欢
      • 1970-01-01
      • 2020-09-12
      • 2023-03-24
      • 1970-01-01
      • 2014-05-25
      • 2012-04-10
      • 2017-09-25
      • 1970-01-01
      • 2012-05-17
      相关资源
      最近更新 更多