mysqli_insert_id() is returning 0 when used inside an insert

Asked

Viewed 229 times

0

Every time I try to catch the id of that first INSERT, for mysqli_insert_id(), he returns 0 in the second Insert and returns the correct number in the header below, which can be?

require_once('../connects/connection.php');
session_name(md5('inv_log'));
session_start();


$evntowner = $_SESSION['uid'];



if(isset($_POST['evntnamesu']) && isset($_FILES['bgenvtpopup'])){
    $eventname = $_POST['evntnamesu'];
    $eventtype = $_POST['evntypesu'];
    $eventaccess = $_POST['evntaccsu'];


    $imagem = $_FILES["bgenvtpopup"];
    if(empty($imagem['name'])){
        $limit = "";
    }else{
    $ext = pathinfo($imagem["name"], PATHINFO_EXTENSION);
    $pasta = "../eventimgs/bg2/";
    $limit =  $pasta.md5($imagem["name"].mt_rand()).".".$ext;
    move_uploaded_file($imagem["tmp_name"], $limit);
}


    $eventprice = $_POST['input-money'];

    $eventlocal = $_POST['localnamesu'];
    $eventlocaladrs = $_POST['adrslocalsu'];

    $starTimeHorValue = $_POST['startTimeHor'];
    $starTimeMinValue = $_POST['startTimeMin'];
    $endTimeHorValue = $_POST['endTimeHor'];
    $endTimeMinValue = $_POST['endTimeMin'];
    $starteventtime = $starTimeHorValue.':'.$starTimeMinValue;
    $endeventtime = $endTimeHorValue.':'.$endTimeMinValue;



    $eventday = $_POST['evntdayvlpop'];
    $eventmonth = $_POST['evntmonthvlpop'];
    $eventyear = $_POST['evntyearvlpop'];
    $evntfulldate = $eventyear.'-'.$eventmonth.'-'.$eventday;

    $eventdescr = $_POST['descriptevent'];


    $sql = "INSERT INTO `events` (`event_id`, `event_owner_id`, `event_name`, `event_type`, `event_access`, `event_bg2`, `event_price`, `event_local`, `event_local_adrs`, `event_time_start`, `event_time_end`, `event_day`, `event_descr`) VALUES (NULL, '$evntowner', '$eventname', '$eventtype', '$eventaccess', '$limit', '$eventprice', '$eventlocal', '$eventlocaladrs', '$starteventtime', '$endeventtime', '$evntfulldate', '$eventdescr')";
    $run = mysqli_query($connection, $sql);
    $last_id = mysqli_insert_id($connection);

}
$ids = $_POST['invitedVal'];
$party_id = $last_id;
if(empty($ids)){

}else{
    $idsDivide = explode(',', $ids);
        for($i = 0;$i < count($idsDivide); $i++){
            mysqli_query($connection, "INSERT INTO invitations (inv_id, party_id, user_id, inv_status, inv_code, inv_user_status) VALUES (null, '$party_id', '$idsDivide[$i]', '1', '0', '0')");
        }
    }
    /*Redirect by function on geteventinfos.js:469*/

  • Has a } lost in the code, the first Insert does not give error?

  • 1

    It’s because there’s more code on top, which I cut to make it easier to understand

  • 1

    Make a: var_dump(mysqli_insert_id($connection));

  • The first Insert is inside a if. Are you sure he’s being executed?

  • yes, because it inserts things inside the db so that comes the mysqli_insert_id returning 0

1 answer

-1


Has a { lost in code ,is just a piece of the correct code?

Another thing you should take into consideration is to see if all your query data meets the table requirements for insertion (check if you are not going null and etc for some field).

What I guided you to first is to make a var_dump in its variable $sql and a die() then, then you take this query that it generated and then try to manually insert into the database to see if the error initially isn’t it instead of in the code, if it works, then you have to re-examine your code by checking the connection (it may be lost or may be having trouble communicating with the server).

If the problem persists and continue try to separate this piece only from your application in a separate module and check if it works.

Sometimes the problem may come from some other part of your code, debug the same you will easily find the solution and if possible post both the complete code and the connection code so we can analyze better and give you better support.

  • 1

    Blz man, you put!!!

  • 1

    but what I’m trying to do is there’s no restriction on??

  • It depends on your user of the database, if he has restriction to do Inserts doing the method I informed you to take the pure query and execute it you will find out right away and will already be smart to solve your problem. But let’s debug calmly and parts by parts, first check if your query is correct

  • 2

    The first correct query ta!!

  • Beauty, you already know that you can make insertions and that your query is correct, the next part is you see if during the insertion you are able to connect with your database, gives a var_dump on your connection before doing the query via php and a die() and checks the return.

  • blzz, let me see here!

  • I gave vardump in connection, appeared a very big deal

  • does not fit here!!

  • that would be important? ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["Errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0)??

  • Put it in a gist or something for me to analyze

  • https://gist.github.com/anonymous/bbe5787ac3cf83ab43fed99c67932b2b

  • There’s nothing wrong with your connection, it’s the way you’re adding even, your drivers are ok?

  • how do I check this man

  • Var_dump your Insert

  • By the way, it was you who put -1 in my answer?

  • 1

    think, it wasn’t me

  • 1

    already gave the right one there

  • 1

    Bro, return true in my Sert!!

  • Excellent, probably could be a connection problem or something like that is always good debug the code when it happens these things, so you can better understand what happened, I’m glad you found the solution.

  • 1

    vlww mano é noizzz

Show 15 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.