在此代码中,数据未插入数据库。此节目数据已添加
在此代码中,数据未插入数据库。此节目数据已添加
<?php session_start();
include('../includes/dbcon.php');
$uid=$_POST['uid']; $category = $_POST['category'];
$result = mysqli_query($con,"SELECT cat_name FROM category where user_id=".$_SESSION['id']);
$count = mysqli_num_rows($result);
if ($count==0)
{
mysqli_query($con,"INSERT INTO category(cat_name,user_id)
VALUES('$category','$uid')")or die(mysqli_error());
echo "<script type='text/javascript'>alert('Successfully added new category!');</script>";
echo "<script>document.location='category.php'</script>";
}
else
{
echo "<script type='text/javascript'>alert('Category already added!');</script>";
echo "<script>document.location='category.php'</script>";
}
?>
答案 0 :(得分:0)
正如您在评论中所说,似乎错误是:
&#34;通过此声明,我只能添加一个类别,因为它显示已添加类别&#34;
但这正是你的代码所做的。它工作正常。
插入新类别时,存储的user_id是会话变量。
mysqli_query($con,"INSERT INTO category(cat_name,user_id)
VALUES('$category','$uid')")or die(mysqli_error());
然后你这样检查:
$result = mysqli_query($con,"SELECT cat_name FROM category where user_id=".$_SESSION['id']);
$count = mysqli_num_rows($result);
因此,当您插入新类别时,会存储您的会话ID。然后,当您尝试使用相同$ _SESSION [&#39; id&#39;]的新插入时,结果已经存在。
希望它有用