图像上传限制为5,如果从db之前增加,则自动增加

Have few questions hare : First question : how to set max numbers of files to upload to 5 so users cannot upload more then 5 items , was tried to search on this forum don't found anything that's helps at all . Second question : so basically what i am doing is inserting a post similar to ebay listing ware you put a ( title ,descriptions ,price , photos and ect. ) so i have two tables in my database one is post ( witch is like listing) and another one is images ( images for a posts) so now i will add a code of mine and below will explain everything more :

if (isset($_POST["postad"])){

        $catid = $_POST["catid"];
        $action = $_POST["action"];
        $title = $_POST["title"];
        $desc = $_POST["details"];
        $price = $_POST["price"];
        $ocp = $_POST["ocp"];
        $phone = $_POST["phonenumber"];
        $email = $_POST["email"];
        $location = $_POST["location"];
        $name = $_POST["name"];
        $web = $_POST["web"];
        $time = time();
        if(isset($_SESSION["userid"])){
        $userid = $_SESSION["userid"];
        } else {
         $userid = "0";
        }

        $sql = sprintf(
        "INSERT INTO posts 
        (catid,userid,dateadded,title,action,description,price,ocp,location,name,telno,email,website) 
        VALUES
        ('%s', '%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s')",
        mysqli_escape_string($connect, $catid),
        mysqli_escape_string($connect, $userid),
        mysqli_escape_string($connect, $time),
        mysqli_escape_string($connect, $title),
        mysqli_escape_string($connect, $action),
        mysqli_escape_string($connect, $desc),
        mysqli_escape_string($connect, $price),
            mysqli_escape_string($connect, $ocp),
            mysqli_escape_string($connect, $location),
            mysqli_escape_string($connect, $name),
            mysqli_escape_string($connect, $phone),
            mysqli_escape_string($connect, $email),
            mysqli_escape_string($connect, $web) 
        );
        $res = mysqli_query($connect,$sql);

        if($res != false){
            $poststyle = "scspost";
            $postmsg = "added";
            mysqli_close($connect);
        }


    if(isset($_SESSION["username"]) && time() - $_SESSION["CREATED"] < 1800){
         $username = $_SESSION["username"];
         $userdir = "images/users/$username/";
        }else {
         $userdir = "images/guest";
        }

        $uploadOk = 1;
        for($i=0; $i<count($_FILES['image']['name']); $i++) {

            $target_file = $userdir . basename($_FILES["image"]["name"][$i]);
            $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

                    // Check if image file is a actual image or fake image
                $check = getimagesize($_FILES["image"]["tmp_name"][$i]);
                if($check !== false) {
                    echo "File is an image - " . $check["mime"] . ".
";
                    $uploadOk = 1;
                } else {
                    echo "File is not an image.
";
                    $uploadOk = 0;
                }

                // Check if file already exists
                if (file_exists($target_file)) {
                    echo "Sorry, file already exists.
";
                    $uploadOk = 0;
                } 

                 // Check file size
                if ($_FILES["image"]["size"][$i] > 5000000) {
                    echo "Sorry, your file is too large.
";
                    $uploadOk = 0;
                } 
                // Allow certain file formats
                if($imageFileType != "jpg"  && $imageFileType != "jpeg") {

                    echo "Sorry, only JPG, JPEG files are allowed.
";
                    $uploadOk = 0;
                } 


                 // Check if $uploadOk is set to 0 by an error
                    if ($uploadOk != 0) {
                            if (!file_exists($userdir)){
                             mkdir($userdir, 0777, true);
                            }
                        if (move_uploaded_file($_FILES["image"]["tmp_name"][$i], $target_file)) {

                            $image1 = $_FILES["image"]["name"][0];
                            $image2 = $_FILES["image"]["name"][1];
                            $image3 = $_FILES["image"]["name"][2];
                            $image4 = $_FILES["image"]["name"][3];
                            $image5 = $_FILES["image"]["name"][4];
                            $sql = sprintf(
                            "INSERT INTO users (postid,path1,path2,path3,path4,path5) VALUES ('%s', '%s', '%s', '%s', '%s', '%s')",
                            mysqli_escape_string($connect, $postid),
                            mysqli_escape_string($connect, $image1),
                            mysqli_escape_string($connect, $image2),
                            mysqli_escape_string($connect, $image3),
                            mysqli_escape_string($connect, $image4),
                            mysqli_escape_string($connect, $image5)
                            );


                                $res = mysqli_query($connect,$sql);


                            echo "The file ". basename( $_FILES["image"]["name"][$i]). " has been uploaded.
";
                        } else {

                            echo "Sorry, there was an error uploading your file.
";
                        }
                    }

        }


}

so basically this code adds all information about a post into table post and then i am trying to add an info to images table but before that i need to extract a id of a post that was just added to attach images to that id and later can use them by id of the post i mean hare i am trying to add to a database path of photos and postid ( witch i need to get before )

 $image1 = $_FILES["image"]["name"][0];
                            $image2 = $_FILES["image"]["name"][1];
                            $image3 = $_FILES["image"]["name"][2];
                            $image4 = $_FILES["image"]["name"][3];
                            $image5 = $_FILES["image"]["name"][4];
                            $sql = sprintf(
                            "INSERT INTO users (postid,path1,path2,path3,path4,path5) VALUES ('%s', '%s', '%s', '%s', '%s', '%s')",
                            mysqli_escape_string($connect, $postid),
                            mysqli_escape_string($connect, $image1),
                            mysqli_escape_string($connect, $image2),
                            mysqli_escape_string($connect, $image3),
                            mysqli_escape_string($connect, $image4),
                            mysqli_escape_string($connect, $image5)

so basically i need help by getting an id of post that was just added , please if i miss something or you need more information just ask