我正在为简单的论坛制作API,现在尝试将信息添加到数据库并保存(保存帖子)
控制页面上的:savePost.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
if(!isset($_GET['id']))
{
die('bad access');
}
$_id = (int)$_GET['id']; // to avoid injection and typing codes in url
if ($_id == 0)
{
die('Bad Access');
}
require_once('fourmsAPI.php');
require_once('postsAPI.php');
$forum = tinyf_forums_get_by_id($_id);
if(!$forum){
tinyf_db_close(); //important
die('Bad Forum ID');
}
if(!isset($_POST['title']) or (!isset($_POST['content']))) {
die('bye');
}
require_once('fourmsAPI.php');
$result = tinyf_post_add($_id,0,0,trim($_POST['title']),trim($_POST['content'])) ;
tinyf_db_close();
if($result){
die('sucess');
}
else{
die('Failed');
}
结果=====&gt;失败
Apifile:
postsAPI.php
<?php
//Posts APIs
function tinyf_posts_get($extra ='')
{
global $tf_handle;
$query = sprintf("SELECT * FROM `posts` %s ",$extra );
$qresult = mysqli_query($tf_handle, $query);
if (!$qresult)
return NULL;
$recount = mysqli_num_rows($qresult);
if ($recount == 0)
return NULL ;
$posts = array();
for($i = 0 ; $i < $recount ; $i++)
$posts[count($posts)] = mysqli_fetch_object($qresult);
//mysql_free_result($qresult);
return $posts;
}
function tinyf_posts_get_by_id($pid)
{
$id = (int)$pid;
if($id == 0 )
return NULL ;
$result = tinyf_posts_get('WHERE `id` ='.$id);
if($result == NULL)
return NULL;
$post = $result[0];
return $post;
}
//get result is array()
function tinyf_posts_get_reply_by_id($pid)
{
$id = (int)$pid;
if($id == 0 )
return NULL ;
$result = tinyf_posts_get('WHERE `pid` ='.$id);
if($result == NULL)
return NULL;
return $post;
}
function tinyf_post_add($fid,$pid,$uid,$title,$content)
{
global $tf_handle;
$_fid = (int)$fid;
$_pid = (int)$pid;
$_uid = (int)$uid;
if(($_fid == 0) /* || ($_uid == 0)*/){
return false ;
}
if ((empty($title)) || (empty($content)))
return false;
$n_title = mysqli_real_escape_string($tf_handle, strip_tags($title));
$n_content = mysqli_real_escape_string($tf_handle, strip_tags($content));
$query = sprintf("INSERT INTO `users` VALUE(NULL,'%d','%d','%d','%s','%s')",$_fid,$_pid,$_uid,$n_title,$n_content);
$qresult = mysqli_query($tf_handle, $query);
if(!$qresult)
return false;
return true;
}
function tinyf_posts_delete_reply($pid)
{
global $tf_handle;
$id = (int)$pid;
if($id == 0 )
return false ;
$query = sprintf ("DELETE FROM `posts` WHERE `pid`= %d",$id);
$qresult = mysqli_query($tf_handle, $query);
if(!$qresult)
return false;
return true;
}
function tinyf_posts_delete($pid)
{
global $tf_handle;
$id = (int)$pid;
if($id == 0 )
return false ;
$query = sprintf ("DELETE FROM `posts` WHERE `id`= %d",$id);
tinyf_posts_delete_reply($pid);
$qresult = mysqli_query($tf_handle, $query);
if(!$qresult)
return false;
return true;
}
function tinyf_posts_update($_id,$_fid = 0,$_pid = 0 ,$_uid = 0,$title = NULL,$content = NULL)
{
global $tf_handle;
//if pid == 0 -----> mawdo3
$id = (int)$_id;
$fid = (int)$_fid;
$pid = (int)$_pid;
$uid = (int)$_uid;
if($id <=0){
return false;
}
$post = tinyf_posts_get_by_id($id);
if(!$post)
return false;
if ((empty($title)) && (empty($content)) && ($post ->fid == $fid) &&
($post->pid == $pid) && ($post->uid == $uid)){
return false;
}
if($post->pid <= 0){
if($_fid == 0)
{
$_fid = $post ->fid ;
}
$_pid = 0;
}
else
{
$_fid = 0;
if($_pid <= 0){
$_pid = $post -> pid;
}
}
if($_uid <= 0){
$_uid = $post ->uid;
}
$fields = array() ;
$query = 'UPDATE `posts` SET ' ;
if(!empty($title))
{
$n_title = mysqli_real_escape_string($tf_handle, strip_tags($title));
$fields[count($fields)] = "`title` = '$n_title'";
}
if(!empty($content))
{
$n_content = mysqli_real_escape_string($tf_handle,strip_tags($name));
$fields[count($fields)] = "`content` = '$n_content'";
}
$fields[count($fields)] = "`fid` = '$_fid'";
$fields[count($fields)] = "`pid` = '$_pid'";
$fields[count($fields)] = "`uid` = '$_uid'";
$fcount = count($fields);
for($i = 0; $i < $fcount ; $i++)
{
$query .= $fields[$i];
if($i != ($fcount - 1)) // i = 0 that the first element in the array .. 2 will be - 1 last 3shan hwa by3ed el array mn wa7ed :D
$query .=' , ';
}
$query .= ' WHERE `id` = '.$id;
$qresult = mysqli_query($tf_handle, $query);
if(!$qresult)
return false;
else
return true;
}
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
点击保存帖后的结果是==&gt; &#39;现场&#39;
我预计会保存信息(帖子)
来自
的错误$query = sprintf("INSERT INTO `users` VALUE(NULL,'%d','%d','%d','%s','%s')",$_fid,$_pid,$_uid,$n_title,$n_content);
$qresult = mysqli_query($tf_handle, $query);
if(!$qresult)
{
echo "3";
return false;
}
我认为函数tinyf_post_add()导致了这个问题,或者因为我从我的另一个API文件复制了一些函数
答案 0 :(得分:1)
这是帮助回答这个问题的步骤:
第一个建议:
当然tinyf_post_add($fid, $pid, $uid, $title, $content)
造成了这种情况。您可以尝试调试代码并在return语句之前回显,并使用数字标识每个echo
。然后你会知道你看到的最后一个echo
导致它失败。
来自@MarcB的建议
他建议使用更有用的调试信息,这些信息可以由if (!$qresult) { die(mysqli_error($tf_handle)); }
返回,以获取有关MySQL端发生的更多有用信息。
<强>反馈强>
在SQL语句行中发现错误。错误是SQL语句中的列计数不正确。
第二个建议
首先,查看正确的INSERT syntax,如果您不想插入所有列,只需在VALUES()
部分之前在括号中指定所需的列。其次,你可以正确地使用prepare()
函数来完成SQL注入。