nevsi79 Posted March 14, 2008 Share Posted March 14, 2008 I inserted some data in mySQL table as follows: $add_claim_details_sql = "INSERT INTO claim_details (ins_ref_no) VALUES ('$your_ref_no')"; $add_claim_details_res = mysql_query($add_claim_details_sql) or die ("Could not edit item details:" . mysql_error()); so far so good - it works, great stuff, but ... ??? I would like to give some feedback to the user, ie, I want to supply the user who's submitted the form with a ref. no. The ref. no is the id from claim_details. (the id is set to be a primary key auto-increment). so I was hoping to achieve this by writing the following script: $query = "SELECT id FROM claim_details WHERE id=".$_GET['id']; $result = mysql_query($query)or die("Could not get your ref. no.:" . mysql_error()); echo $result; Obviously it does not work as I get the following error: Could not get your ref. no.:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 I do not how to identify the id - which will be the reference number unique to the just submitted form. I hope my question makes sense and someone will help. Thanks! Quote Link to comment Share on other sites More sharing options...
Stooney Posted March 14, 2008 Share Posted March 14, 2008 $query = "SELECT id FROM claim_details WHERE id='$_GET[id]'"; EDIT: For more security, do this instead: $query = "SELECT id FROM claim_details WHERE id='".mysql_real_escape_string($_GET['id'])."'"; Quote Link to comment Share on other sites More sharing options...
AdRock Posted March 14, 2008 Share Posted March 14, 2008 Have you tried $query = "SELECT id FROM claim_details WHERE id='".$_GET['id']'"; Quote Link to comment Share on other sites More sharing options...
dotBz Posted March 14, 2008 Share Posted March 14, 2008 another is $query = "SELECT id FROM claim_details WHERE id='{$_GET['id']}';"; or there's probably an error in your sql query. try "echo $query", that could help Quote Link to comment Share on other sites More sharing options...
nevsi79 Posted March 14, 2008 Author Share Posted March 14, 2008 It didn't work guys as I don't get the ref. no (i.e. the id unique to the just submitted data), instead I get: Resource id #4 Here is all my code: $add_claim_details_sql = "INSERT INTO claim_details (ins_ref_no) VALUES ('$your_ref_no')"; $add_claim_details_res = mysql_query($add_claim_details_sql) or die ("Could not edit item details:" . mysql_error()); echo $add_claim_details_res; echo "</br>"; echo "thank you, your form has been submitted and the your data was stored in our db"; echo "</br>"; $query = "SELECT id FROM claim_details WHERE id='".mysql_real_escape_string($_GET['id'])."'"; $result = mysql_query($query)or die("Could not get your ref. no.:" . mysql_error()); echo "your ref no is: $result"; When I run it, I get the following every time: 1 thank you, your form has been submitted and the your data was stored in our db your ref no is:Resource id #4 Where 1 is what I have put in the form for ins_ref_no. However the resource id is not the desired output, it should have been 34 (or whatever is- which is id corresponding to the data that have been inserted with the $add_claim_details_sql. Quote Link to comment Share on other sites More sharing options...
trq Posted March 15, 2008 Share Posted March 15, 2008 You can't simply echo the result of mysql_query(), it returns a result resource. You need to pass this result resource to mysql_fetch_assoc() to actually get your results. Quote Link to comment Share on other sites More sharing options...
nevsi79 Posted March 15, 2008 Author Share Posted March 15, 2008 thank you thorpe, - as I said I am new to PHP so I added the following 2 lines to my script: $row = mysql_fetch_array($result); echo $row; but it did not return the id, it returned nothing! What am I doing wrong? Quote Link to comment Share on other sites More sharing options...
nevsi79 Posted March 15, 2008 Author Share Posted March 15, 2008 thank you thorpe, Embarrassed - as I said I am new to PHP Embarrassed so I added the following 2 lines to my script: Code: $row = mysql_fetch_array($result); echo $row; but it did not return the id, it returned nothing! What am I doing wrong? Quote Link to comment Share on other sites More sharing options...
nevsi79 Posted March 15, 2008 Author Share Posted March 15, 2008 I think I am not targeting the id properly, is $_GET['id'] the right variable ???? I tried the following: $num_rows = mysql_num_rows($result); echo $num_rows; and it returned 0????? Quote Link to comment Share on other sites More sharing options...
trq Posted March 15, 2008 Share Posted March 15, 2008 is $_GET['id'] the right variable We would have no idea... are you passing the id via the url? eg; http://yourhost.com/yourpage.php?id=2 Quote Link to comment Share on other sites More sharing options...
Stooney Posted March 15, 2008 Share Posted March 15, 2008 EDIT: I read your post wrong sorry. Quote Link to comment Share on other sites More sharing options...
nevsi79 Posted March 15, 2008 Author Share Posted March 15, 2008 sorry I do not understand your question This is all my code: session_start(); include('conn.php');//Connection to databse //Random Number $rand = rand(1000, 2000); $_SESSION['guid'] = $rand; $guid = $_SESSION['guid']; echo "SESSION ID is <b>".$guid."</b><br/><br/>"; $your_ref_no = htmlspecialchars($_POST['tfa_YourRefNo']); //check to see if the form has been submitted if(isset($_POST['tfa_submitAction'])){ //add to claim_details table $add_claim_details_sql = "INSERT INTO claim_details (ins_ref_no) VALUES ('$your_ref_no')"; $add_claim_details_res = mysql_query($add_claim_details_sql) or die ("Could not edit item details:" . mysql_error()); echo $add_claim_details_res; echo "</br>"; echo "thank you, your form has been submitted and the your data was stored in our db"; echo "</br>"; $query = "SELECT id FROM claim_details WHERE id='".mysql_real_escape_string($_GET['id'])."'"; $result = mysql_query($query)or die("Could not get your ref. no.:" . mysql_error()); //$row = mysql_fetch_array($result); $num_rows = mysql_num_rows($result); echo $num_rows; My form is in another file called: testform.html The testform.html has an action that equals the url to the above php page and the submit button of the testform.html has a name=''tfa_submitAction''. Quote Link to comment Share on other sites More sharing options...
nevsi79 Posted March 15, 2008 Author Share Posted March 15, 2008 change $row = mysql_fetch_array($result); echo $row; to $row = mysql_fetch_array($result); echo $row[0]; It's not called mysql_fetch_array for nothing. it returned blank Quote Link to comment Share on other sites More sharing options...
Stooney Posted March 15, 2008 Share Posted March 15, 2008 Post your form Quote Link to comment Share on other sites More sharing options...
nevsi79 Posted March 15, 2008 Author Share Posted March 15, 2008 This is the form, but so far I have only output <h3>Claim Details</h3>. Hopefully you will not be confused with the rest of it. <!-- FORM: HEAD SECTION --> <link href="http://app.formassembly.com/wForms/3.0/css/black/wforms.css" rel="stylesheet" type="text/css"></link> <link href="http://app.formassembly.com/wForms/3.0/css/wforms-jsonly.css" rel="alternate stylesheet" title="This stylesheet activated by javascript" type="text/css"></link> <link href="http://app.formassembly.com/wForms/3.0/css/wforms-layout.css" rel="stylesheet" type="text/css"></link> <link href="file:///nevenaaleksieva/Desktop/testform.css" rel="stylesheet" type="text/css" /> <!--[if IE 7]> <link href="http://app.formassembly.com/wForms/3.0/css/wforms-layout-ie7.css" rel="stylesheet" type="text/css"></link> <![endif]--> <script type="text/javascript" src="http://app.formassembly.com/wForms/3.0/js/wforms.js"></script> <script type="text/javascript" src="http://app.formassembly.com/wForms/3.0/js/wforms_custom_validation.js"></script> <script type="text/javascript" src="http://app.formassembly.com/js/populate_form.js"></script> <!-- FORM: BODY SECTION --> <div class="wForm"> <div class="codesection" id="code-"></div> <form method="post" action="http://www.geeks-fix.co.uk/testform/output.php" id="id2489978" class="labelsAbove hintsTooltip" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="10485760"><div class="wfPage " id="wfPgIndex-1"> <h3>Claim Details:</h3> <div class="oneField"> <label for="tfa_YourRefNo" class="preField">Your Ref. No:<span class="reqMark">*</span></label> <input type="text" id="tfa_YourRefNo" name="tfa_YourRefNo" value="" size="40" class="required"><br> <span class="errMsg" id="tfa_YourRefNo-E"></span> </div> </div> <div class="wfPage " id="wfPgIndex-2"> <h3>Policy Holder Info</h3> <fieldset id="tfa_FullName" class=""> <legend>Full Name</legend> <div class="oneField"> <label for="tfa_Title" class="preField">Title</label> <input type="text" id="tfa_Title" name="tfa_Title" value="" size="8" class=""><br> <span class="errMsg" id="tfa_Title-E"></span> </div> <div class="oneField"> <label for="tfa_FirstName" class="preField">First Name</label> <input type="text" id="tfa_FirstName" name="tfa_FirstName" value="" size="50" class=""><br> <span class="errMsg" id="tfa_FirstName-E"></span> </div> <div class="oneField"> <label for="tfa_Initials" class="preField">Initials</label> <input type="text" id="tfa_Initials" name="tfa_Initials" value="" size="" class=""><br> <span class="errMsg" id="Initials-E"></span> </div> <div class="oneField"> <label for="tfa_FamilyName" class="preField">Family Name<span class="reqMark">*</span></label> <input type="text" id="tfa_FamilyName" name="tfa_FamilyName" value="" size="50" class="required"> <div class="field-hint-inactive" id="tfa_FamilyName-H"> <span>if there is more than one person on the policy, please separate the family names with comma.</span> </div> <br><span class="errMsg" id="tfa_FamilyName-E"></span> </div> </fieldset> <fieldset id="tfa_Address" class=""> <legend>Address</legend> <div class="oneField"> <label for="tfa_StreetAddress" class="preField">Street Address<span class="reqMark">*</span></label> <input type="text" id="tfa_StreetAddress" name="tfa_StreetAddress" value="" size="50" class="required"><br> <span class="errMsg" id="tfa_StreetAddress-E"></span> </div> <div class="oneField"> <label for="tfa_Addresscontinued" class="preField">Address continued</label> <input type="text" id="tfa_Addresscontinued" name="tfa_Addresscontinued" value="" size="50" class=""><br> <span class="errMsg" id="tfa_Addresscontinued-E"></span> </div> <div class="oneField"> <label for="tfa_City" class="preField">City</label> <input type="text" id="tfa_City" name="tfa_City" value="" size="" class=""><br> <span class="errMsg" id="tfa_City1-E"></span> </div> <div class="oneField"> <label for="tfa_Postcode" class="preField">Postcode<span class="reqMark">*</span></label> <input type="text" id="tfa_Postcode" name="tfa_Postcode" value="" size="8" class="required"> <br><span class="errMsg" id="tfa_Postcode-E"></span> </div> </fieldset> <fieldset id="tfa_Telephone" class="required"> <legend>Telephone:</legend> <div class="oneField"> <label for="tfa_Home" class="preField">Home:</label> <input type="text" id="tfa_Home" name="tfa_Home" value="" size="40" class="validate-integer"> <br><span class="errMsg" id="tfa_Home-E"></span> </div> <div class="oneField"> <label for="tfa_Mobile" class="preField">Mobile:</label> <input type="text" id="tfa_Mobile" name="tfa_Mobile" value="" size="40" class="validate-integer"> <br><span class="errMsg" id="tfa_Mobile-E"></span> </div> <div class="oneField"> <label for="tfa_Work" class="preField">Work:</label> <input type="text" id="tfa_Work" name="tfa_Work" value="" size="40" class="validate-integer"><br> <span class="errMsg" id="tfa_Work-E"></span> </div> </fieldset> <div class="oneField"> <label for="tfa_emailaddress" class="preField">email address</label> <input type="text" id="tfa_emailaddress" name="tfa_emailaddress" value="" size="40" class="validate-email"> <br> <span class="errMsg" id="tfa_emailaddress-E"></span> </div> </div> <div class="wfPage " id="wfPgIndex-3"> <h3>Item Claimed Details:</h3> <fieldset id="tfa_Item" class="repeat"> <legend>Item </legend> <div class="oneField"> <label for="tfa_ItemName" class="preField">Item Name<span class="reqMark">*</span></label> <input type="text" id="tfa_ItemName" name="tfa_ItemName" value="" size="40" class="required"><br> <span class="errMsg" id="tfa_ItemName-E"></span> </div> <div class="oneField"> <label for="tfa_ItemMake" class="preField">Item Make</label> <input type="text" id="tfa_ItemMake" name="tfa_ItemMake" value="" size="40" class=""> <br><span class="errMsg" id="tfa_ItemMake-E"></span> </div> <div class="oneField"> <label for="tfa_ItemDetails" class="preField">Item Details</label> <input type="text" id="tfa_ItemDetails" name="tfa_ItemDetails" value="" size="40" class=""><br> <span class="errMsg" id="tfa_ItemDetails-E"></span> </div> <div class="oneField"> <label for="tfa_ItemPrice" class="preField">Item Price</label> <input type="text" id="tfa_ItemPrice" name="tfa_ItemPrice" value="" size="40" class=""><br> <span class="errMsg" id="tfa_ItemPrice-E"></span> </div> <span class="duplicateSpan"><a id="tfa_Item-wfDL" class="duplicateLink" href="#">add another item</a> </span> </fieldset> <div class="oneField"> <label for="tfa_Attachadditional" class="preField">Attach additional info</label> <input type="file" id="tfa_Attachadditional" name="tfa_Attachadditional" value="" size="40" class=""><br> <span class="errMsg" id="tfa_Attachadditional-E"></span> </div> </div> <div class="actions"> <input type="submit" class="primaryAction" id="submit-" name="tfa_submitAction" value="SUBMIT"> <input type="button" class="secondaryAction" onclick="history.go(-1)" value="CANCEL"> <input type="hidden" value="12461" name="tfa_dbFormId" id="tfa_dbFormId"> <input type="hidden" value="" name="tfa_dbResponseId" id="tfa_dbResponseId"> <input type="hidden" value="f7d9626afc92956c65cb904d2a6abcb7" name="tfa_dbControl" id="tfa_dbControl"> </div> </form> </div> Quote Link to comment Share on other sites More sharing options...
Stooney Posted March 15, 2008 Share Posted March 15, 2008 Try changing if(isset($_POST['tfa_submitAction'])){ to if(isset($_POST['tfa_YourRefNo'])){ Rather than checking for the submit, check for a variable. I find it more reliable. Quote Link to comment Share on other sites More sharing options...
nevsi79 Posted March 15, 2008 Author Share Posted March 15, 2008 Try changing if(isset($_POST['tfa_submitAction'])){ to if(isset($_POST['tfa_YourRefNo'])){ Rather than checking for the submit, check for a variable. I find it more reliable. thank you for your post but still the same Quote Link to comment 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.