从插入的行中获取id

时间:2017-06-12 11:06:14

标签: php mysql

我有一个表单,用于在名为t_id的表中插入数据。它插入成功。但是当我尝试获取它if(isset($_POST['submit'])) { $cat = $_POST['categories_chk']; $tag = $_POST['tag']; // echo print_r($cat); $query1 = "select * from tags where tag_name='$tag' "; $search_cat = mysqli_query($con, $query1); $count = mysqli_num_rows($search_cat); if ($count == 0) { $query = "insert into tags values('','$tag')"; $result = mysqli_query($con, $query); if ($result) { $fetch_id = mysqli_query($con, "select t_id from tags where tag_name='$tag'"); while ($row = mysqli_fetch_array($fetch_id, MYSQLI_ASSOC)) { $tag_id = $row['t_id']; echo $tag_id; } } } else { echo "<script>alert('Row with the same name exists! ')</script>"; } // This is line 130 } 时,输出来了

  

警告:mysqli_fetch_array()要求参数1为mysqli_result,第130行的\ tags.php中给出布尔值

代码

now.getDay()

2 个答案:

答案 0 :(得分:0)

选中此项,您需要一个条件来检查true

if ($search_cat = mysqli_query($con, $query1))
{
   $count = mysqli_num_rows($search_cat);
}

答案 1 :(得分:0)

您可以简单地使用last insert ID函数,该函数返回最新查询中使用的自动生成的ID

if ($result) {
            $fetch_id = mysqli_insert_id($con); 

            echo $fetch_id;

            }