Jump to content

Return id number?


johnnys
Go to solution Solved by ginerjm,

Recommended Posts

Hi,

 

Firstly apologies I'm still new to php.

 

I have a form that submits data to my DB successfully (name, dept etc..).

 

What I want to do is, once the form is submitted a message will appear saying 'Thank you - your order number is xxx' (xxx being the auto-increment id)

 

I have been researching the mysql_insert_id but I can't seem to get it working, currently it returns '0'

 

My current code

$sql="INSERT INTO ip_requests  (make,model,qub_id,mac,dnsname,)
VALUES ('".$make."', '".$model."', '".$qub_id."','".$mac."','".$dnsname."')

         $id = mysql_insert_id();
         echo $id;
         
         $objLog->logDebug('New Request: '.$sql);
         $stmt = DB::exec($sql);

Then It displays this (well a zero!)

         echo "<h1>Request Submitted - Order Number below</h1>
            ";
         echo $id;

Any help is much appreciated,

 

J

 

Link to comment
Share on other sites

Most important - we don't see where you actually executed the query.

 

Please don't show us re-typed code samples - time and time again they will have errors in them.  Paste in the section of code you are questioning instead of trying to type it in for us.  Waste of your time and ours.

 

Tip - you can use single quotes around your php vars in the query string since it is wrapped in double quotes already.

 

$sql="INSERT INTO ip_requests  (make,model,qub_id,mac,dnsname,) VALUES ('$make', '$model', '$qub_id', 'mac', '$dnsname' ")

 

Much less typing and easier to read and understand.

Link to comment
Share on other sites

Thanks for the tips guys, apologies for the code, still teaching myself :)

 

This is the actual code below; - I hope it makes sense

 

      
  if(isset($_REQUEST["submit"])){
        try{
        $make=f::getValue("make");
        $model=f::getValue("model");
        $qub_id=f::getValue("inventory_number");
        $mac=f::getValue("mac_address");
        $arrSymbols=array(":","-"," ",";",".");
        $mac=substr(strtoupper(str_replace($arrSymbols, "", $mac)),0,12);
        $dnsname=f::getValue("dnsname");

         $sql="INSERT INTO ip_requests  (make,model,qub_id,mac,dnsname,)
             VALUES ('".$make."', '".$model."', '".$qub_id."','".$mac."','".$dnsname."')";

         $id = mysql_insert_id();
         echo $id;
         
         $objLog->logDebug('New Request: '.$sql);
         $stmt = DB::exec($sql);
         
         if(sendQf22Email($make, $model, $qub_id, $mac,$dnsname,)){
         $boolMailSent = true;
         }else{
         $boolMailSent = false;
         }

         $objDisplay->getHeader($pagetitle);

         echo "<h1>Request Submitted - Order Number Below</h1> ";
         echo $id;

         $objDisplay->getFooter();
         exit;
    }catch(PDOException $e){
         $objLog->logError('Error inserting: '.$sql);
            echo $e->getMessage();
        }
}

Thanks in advance,

 

J

Link to comment
Share on other sites

Again - no execute in sight prior to getting the inserted id value.

 

Also - you appear to be mixing the use of MySQL functions with pdo - no can do.

 

What is the getValue function - I am unfamiliar with that one (amongst a lot of other things!)

 

Again - your code is flawed - can't possibly be running. Hanging comma in your query statement. Do you have error checking enabled???

Put this at the top of every script until you are done developing them:

error_reporting(E_ALL | E_STRICT);

ini_set('display_errors', '1');}

set_time_limit(2); // avoid runaway conditions change if necessary.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.