-1


I seem to have an issue inserting the post values into my database, and i don't see the error in the coding. I've been looking at it for a while now and to me everything looks right, however when i use the form and submit the data the page reload but no data get inserted into the database.
It would be much appreciated if someone could help me identify the error in the coding.

If you have any questions feel free to ask!

Kind regards Jim

FORM

<?php 

 //Show the form if the user is a Admin

 if(isset($_SESSION['username'])){
   $username == $_SESSION['username'];

   $results = $mysqli->query("SELECT authority FROM users WHERE username='$username' LIMIT 1");
   while($row = $results->fetch_object()){
     $aut = $row->authority;
     }
  } 
  if($aut == 1){
?>

<form action="index.php" method="post">
 <table>
  <tr>
   <td> &nbsp; Title: </td>
   <td><input type="text" name="title"></td>
  </tr>
  <tr>
   <td valign="top"> &nbsp; News: </td>
   <td><textarea name="information"></textarea></td>
  </tr>
  <tr>
   <td> <input type="hidden" value="news"> </td>
   <td><input type="submit"></td>
  </tr>
 </table> <hr>
</form>

MYSQLI

<?php
}

//Insert into the database

if(isset($_POST['news'])){
 $title = $_POST['title'];
 $information = $_POST['information'];

 $mysqli->query("INSERT INTO  `news` (`title`, `information`) VALUES ( '".$title."', '".$information."')");
}
4

3 回答 3

1

<input type="hidden" value="news">应该<input type="hidden" name="news">

这就是为什么isset($_POST['news'])永远不会是真的。

于 2013-11-03T14:21:30.763 回答
1

除了那个愚蠢的错字问题之外,您的代码还遭受了两次真正的灾难。

  1. 您没有错误报告,这使您对这些愚蠢的错误束手无策
  2. 您将数据直接添加到查询中,同时应该使用占位符。
于 2013-11-03T14:37:03.237 回答
-1

我不确定原始查询中的反引号和句​​点是什么意思。以我有限的经验,我的查询采取以下形式:

$mysqli->query("INSERT INTO news(title, information) VALUES ('$title', '$information')");

我会说优先级 #1 是以 php 函数的返回值或访问 php 错误日志的形式获取一些调试信息。

于 2013-11-03T14:26:48.660 回答