更新:连接到MySQL数据库时文章ID错误

时间:2014-05-30 02:24:13

标签: php mysqli

我正在创建一个电子邮件表单,允许将评论发布到本地主机服务器。

我有三个PHP文件:

  • page1.phppage2.php包含表单并显示评论。
  • manage_comments.php包含将表单连接到服务器的代码。

manage_comments.php文件是我遇到问题的地方。提交表单后,我收到了错误代码。

  

错误代码:1193无效的文章ID

文章ID 指的是page1.phppage2.php的ID,它们是" 1"和" 2"分别。

mywebsite.com/page1.php?id=1, mywebsite.com/page2.php?id=2

在表单教程中,我被指示写一个隐藏的值来保存文章的id。

<form>
    <input type='hidden' name='articleid' id='articleid' value='<? echo $_GET["id"]; ?> />
</form>

然后在manage_comments.php中,教程指示我写这个:

  $articleid = $_GET['id'];

  if( ! is_numeric($articleid) )
      die('invalid article id'); 

我更改了代码,但仍然收到了1193错误。该教程是在&#34; mysqli&#34;之前编写的。我不得不将代码从mysql转换为mysqli。以下是manage_comments.php的代码:

      <?

      if( $_POST )
      {

      $mysqli = new mysqli('localhost', 'sosdeff', 'edflash9', 'sosdeff_testerforms');

      /* check connection */
      if (mysqli_connect_errno()) {
      printf("Connect failed: %s\n", mysqli_connect_error());
      exit();
      }

      if (!mysqli_query($mysqli, "SET a=1")) {
      printf("Errorcode: %d\n", mysqli_errno($mysqli));
      }

      mysqli_select_db($mysqli, "sosdeff_testerforms");

      $users_name = $_POST['name'];
      $users_email = $_POST['email'];
      $users_website = $_POST['website'];
      $users_comment = $_POST['comment'];

      $users_name = mysqli_real_escape_string($mysqli, $users_name);
      $users_email = mysqli_real_escape_string($mysqli, $users_email);
      $users_website = mysqli_real_escape_string($mysqli, $users_website);
      $users_comment = mysqli_real_escape_string($mysqli, $users_comment);

      $articleid = $_GET['articleid'];

      if( ! is_numeric($articleid) )

      die('invalid article id');

      $query = "INSERT INTO `sosdeff_testerforms`,`comments` (`id`, `name`, `email`, `website`, `comment`, `timestamp`, `articleid`)

     mysqli_query($query);
     echo "<h2>Thank you for your Comment!</h2>";
     mysqli_close($mysqli);

     }
     ?>

1 个答案:

答案 0 :(得分:1)

在您的表单中,该字段称为articleid

表单提交后,如果表单中没有id字段,则ID为空,仅articleid

manage_comments.php中,将您的代码更改为:

$articleid = $_GET['articleid'];

if( ! is_numeric($articleid) )
    die('invalid article id');