SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Last_insert_id()

    this is a part of my code where m trying to retrieve the last autoincremented id
    PHP Code:
    if(!isset($_COOKIE['bill_no']) || $_COOKIE['bill_no']==){
    $sql=mysql_query("insert into bill_id values('null')");
    $sql1=mysql_query("select @last := Last_insert_id()");
    $row=mysql_fetch_array($sql1);
    setcookie("bill_no",$row[0],3600);} 
    but it is not returning the correct value

  2. #2
    SitePoint Addict kuldeepkaundal's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    239
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can try mysql_insert_id();

    like:

    $lastid=mysql_insert_id(); after your insert query.

  3. #3
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    its not working

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sneha24 View Post
    its not working
    What do you mean 'it is not working'? If rest of the conditions are fine and also if the row is inserted into the table then mysql_insert_id() must return the last inserted id as shown above.

    Edit:
    PHP Code:
    if(!isset($_COOKIE['bill_no']) || $_COOKIE['bill_no']==){
        
    $sql mysql_query("insert into bill_id values('null')") or die(mysql_error());
        echo 
    mysql_insert_id();

    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    if(!isset($_COOKIE['bill_no']) || $_COOKIE['bill_no']==)
    {
        
    $sql mysql_query("INSERT INTO bill_id VALUES('null')");
        
    setcookie('bill_no'mysql_insert_id(), 3600);
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks tat worked
    new problem is tat the cookie is not being set
    PHP Code:
    if(!isset($_COOKIE['bill_no'])){
    $sql=mysql_query("insert into bill_id values('null')");
    $lastid=mysql_insert_id();
    setcookie("bill_no",$lastid,3600);
    $i=0;
    foreach(
    $_REQUEST['check'] as $id){
    $selected =$_REQUEST["qty"];
    $sql=mysql_query("insert into bill(`id`,`bill_id`,`qty`) values($id,$lastid,$selected[$i])");
    $i++;
        }
    }
    //if cookie is set
    else{
    $i=0;
    $selected =$_REQUEST["qty"];
    foreach(
    $_REQUEST['check'] as $id){
    $sql=mysql_query("insert into bill(`id`,`bill_id`,`qty`) values($id,$_COOKIE[bill_no],$selected[$i])");
    $i++;
          }
    }
    header("Location: list.php"); 
    each time a new id is inserted

  7. #7
    SitePoint Addict kuldeepkaundal's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    239
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, that's grrt that it worked.

  8. #8
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you sure that you do not have any output before the function setcookie() to the browser? Otherwise it should work at least for an hour.

    Try print_r($_COOKIE) at the top and see if you see the bill_no is set there.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  9. #9
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it displayed
    Array ( [SMFCookie956] => a:4:{i:0;s:1:"1";i:1;s:40:"cd3761c049b2792c847b1215ada1631e8b4a2251";i:2;i:1426696719;i:3;i:2;} [PHPSESSID] => 826a2ihd2s9898ltiej190nss3 )
    for
    print_r($_COOKIE);
    the whole code is
    PHP Code:
    <?php require_once("../shared/common.php");
          
    session_cache_limiter(null);
          
    $tab "profile";
           
    $nav "profile";
          require_once(
    "../classes/Localize.php");
    if(!isset(
    $_COOKIE['bill_no'])){
    $sql=mysql_query("insert into bill_id values('null')");
    $lastid=mysql_insert_id();
    setcookie("bill_no",$lastid,3600);
    $i=0;
    foreach(
    $_REQUEST['check'] as $id){
    $selected =$_REQUEST["qty"];
    $sql=mysql_query("insert into bill(`id`,`bill_id`,`qty`) values($id,$lastid,$selected[$i])");
    $i++;
        }
    }
    //if cookie is set
    else{
    $i=0;
    $selected =$_REQUEST["qty"];
    foreach(
    $_REQUEST['check'] as $id){
    $sql=mysql_query("insert into bill(`id`,`bill_id`,`qty`) values($id,$_COOKIE[bill_no],$selected[$i])");
    $i++;
          }
    }
     
    header("Location: list.php");?>

  10. #10
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    plz help
    i seriously don understand what is wrong?

  11. #11
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sneha24 View Post
    plz help
    i seriously don understand what is wrong?

    try this:
    PHP Code:
    setcookie("bill_no"$lastidtime() + 3600); 
    you just used the number 3600. This is a time way in the past. So the coockie never gets sets. You need to give the current time + the time to live.

    Manual:
    http://php.net/manual/en/function.setcookie.php
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  12. #12
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do i need to send it as parameter to all pages?

  13. #13
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sneha24 View Post
    do i need to send it as parameter to all pages?
    Once the coockie is set, it is accessible on all pages of the same domain.
    So no parameter required to all pages...
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  14. #14
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    m using session for it now
    the problem with session is how do i unset it?

  15. #15
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i guess this will work fine
    unset($_SESSION['bill_no']);


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •