johnnys Posted June 19, 2013 Share Posted June 19, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/279350-return-id-number/ Share on other sites More sharing options...
trq Posted June 19, 2013 Share Posted June 19, 2013 mysql_insert_id works as described. Your code however is riddled with syntax errors. Is this your actual code? Do you have error reporting enabled and display errors on? Quote Link to comment https://forums.phpfreaks.com/topic/279350-return-id-number/#findComment-1436819 Share on other sites More sharing options...
ginerjm Posted June 19, 2013 Share Posted June 19, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/279350-return-id-number/#findComment-1436821 Share on other sites More sharing options...
johnnys Posted June 19, 2013 Author Share Posted June 19, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/279350-return-id-number/#findComment-1436838 Share on other sites More sharing options...
cyberRobot Posted June 19, 2013 Share Posted June 19, 2013 mysql_insert_id() needs to happen after the the query is executed. http://us3.php.net/mysql_insert_id Quote Link to comment https://forums.phpfreaks.com/topic/279350-return-id-number/#findComment-1436841 Share on other sites More sharing options...
ginerjm Posted June 19, 2013 Share Posted June 19, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/279350-return-id-number/#findComment-1436853 Share on other sites More sharing options...
johnnys Posted June 19, 2013 Author Share Posted June 19, 2013 (edited) thanks ill try that now, again I'm still teaching myself this language so don't be surprised if it's incorrect. Edited June 19, 2013 by johnnys Quote Link to comment https://forums.phpfreaks.com/topic/279350-return-id-number/#findComment-1436855 Share on other sites More sharing options...
Solution ginerjm Posted June 19, 2013 Solution Share Posted June 19, 2013 turning on error checking will allow you to debug your code so that at least the syntax is correct even if it doesn't do what you want. That is half the battle for newcomers. Been there. Done that. Still do. Quote Link to comment https://forums.phpfreaks.com/topic/279350-return-id-number/#findComment-1436879 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.