Jump to content

alan_k

Members
  • Posts

    45
  • Joined

  • Last visited

Everything posted by alan_k

  1. Yes you are right. Never do this on a production site. I am just learning JS & php and did this as an exercise following "The Book of JavaScript, 2nd Edition: A Practical Guide to Interactive Web Pages". I was trying to add on to what author had left out which was a way to clear the todo list's completed items.
  2. Sorry for the delay in responding. I just want to thank every one for input and post how I solved issue. I will have to revisit using the $_POST array with AJAX cause I was never able to pass a variable thru it. Javascript: // Clear Done Items Functionfunction clear_done_items(){var xmlhttp;var temp = document.getElementById("list_section");var temp_list_name = temp.innerHTML;var str_len = temp_list_name.length;var list_name = temp_list_name.slice(17,str_len);var user_name = getNameFromCookie();if (user_name == list_name) { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }xmlhttp.open("POST","todolist/clear_done_items.php",true);xmlhttp.send();} else { alert("Warning: You Must be Logged in as this User, " + list_name + ", to Delete the Completed Items List"); }} and php: <!DOCTYPE html><html><head><meta content="text/html; charset=utf-8" http-equiv="Content-Type"><title>Clear Done Items</title></head><body><?phpini_set('display_errors',1);error_reporting(E_ALL);$xml_cookie = $_COOKIE['user'];$xml_file_name = $xml_cookie . ".xml"; /* Here we will use the The DOMDocument class functions to delete the text nodes.*/ //Format XML to save indented tree rather than one line$domxml = new DOMDocument('1.0');if ($domxml->load($xml_file_name) === TRUE) {$domxml->preserveWhiteSpace = false;$domxml->formatOutput = true;// Start Delete process of nodeecho "<br>";$xpath = new DOMXPath($domxml);// Here we use the The DOMXPath class function evaluate to do the heavy lifting.foreach ($xpath->evaluate('//doneitems/item') as $node) { $node->parentNode->removeChild($node);}//Save XML to file - remove this and following line if save not desired$domxml->save($xml_file_name);echo "<strong>XML Document updated.</strong>"; } else {echo " <strong>Error in loading XML file:</strong>"; echo "<br>"; print_r(error_get_last()); } ?></body></html> so basically I'm using a cookie to get the user_name variable to php to process from the main page.
  3. Here is function: function displayHomeInformation(user_name) { document.getElementById("loginArea").innerHTML = "<h3>Welcome, " + user_name + ". <br>" + "<a href='#' onClick='logout(); return false'>logout</a> </h3>"; displayLegalLists(user_name);} It just displays some login information for user. But forget about that I cant even get the POST array to my php page. I'm lost as to why this happening. I've verified you dont need a form to submit and create the POST array, yes? These 3 lines should create and send the POST array to my php file: xmlhttp.open("POST","todolist/clear_done_items.php",true); xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlhttp.send(user_name); And yet the array is empty??? Is my understanding wrong? What is responsible for creating and sending the POST array? And what is it I'm doing wrong. So to be clear step 1 I get user_name from cookie and put it in a variable step-2 I issue the xmlhttp commands mentioned above that will create the POST array and send my user_name variable in the array to my php file step -3 The php file looks for the user_name in the POST array and assigns it to php variable and then I can work on the appropriate XML file based on the user_name. Thanks for your time.
  4. That's what I thought by reading up on ajax, that all I needed was the xmlhttp.send. Any ideas then on why the _POST array is empty when I send it to my php file? I even tried GET and that didnt work. I'm really stumped by what I'm doing wrong here. Any input much appreciated...
  5. I'm sorry it does or it doesnt have to be submitted in form? I know i'm not getting to php file because print_r($POST) is printing out array ().
  6. I changed line to xmlhttp.open("POST","todolist/clear_done_items.php",true); xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlhttp.send('user_name='+ user_name); but get same exact error. $_POST array is still empty. Just to be clear I dont need the ".onreadystatechange" event handler if all I want is to pass the user_name to the php script and dont need anything back form the script?
  7. I am trying to pass a variable from a javascript function to my php file. All I do is extract the user_name from a cookie in javascript and then use AJAX to $_POST the variable to my php file for processing. I do not use a form to submit the POST variable which I have done in the past.The problem is my $_POST array is not getting passed to the php file. I've looked here and looked at several other suggestions but none work. Do you have to submit the $_POST variable via an html form? I dont think so but maybe I'm wrong. Here is the code:Javascript - function clearDoneItems() { var user_name = getNameFromCookie(); if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } alert(user_name); xmlhttp.open("POST","todolist/clear_done_items.php",true); xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlhttp.send(user_name); displayHomeInformation(user_name); } PHP - <!DOCTYPE html> <html> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <title>Clear Done Items</title> </head> <body> <?php ini_set('display_errors',1); error_reporting(E_ALL); print_r($_POST); $xml_name=$_POST['user_name']; $xml_file = $xml_name . ".xml"; echo $xml_file; /* Here we will use the The DOMDocument class functions to delete the text nodes. Format XML to save indented tree rather than one line */ $domxml = new DOMDocument('1.0'); if ($domxml->load('$xml_file') === TRUE) { $domxml->preserveWhiteSpace = false; $domxml->formatOutput = true; // Start Delete process of node echo "<br>"; $xpath = new DOMXPath($domxml); // Here we use the The DOMXPath class function evaluate to do the heavy lifting. foreach ($xpath->evaluate('//doneitems/item') as $node) { $node->parentNode->removeChild($node); } //Save XML to file - remove this and following line if save not desired $domxml->save('alan.xml'); echo "<strong>XML Document updated.</strong>"; } else { echo " <strong>Error in loading XML file:</strong>"; echo "<br>"; print_r(error_get_last()); } ?> </body> </html> >Errors on php page:Notice: Undefined index: user_name in /var/www/bb/todolist/clear_done_items.php on line 160Warning: DOMDocument::load(): I/O warning : failed to load external entity "/var/www/bb/todolist/$xml_file" in /var/www/bb/todolist/clear_done_items.php on line 24 I thought from reading up on AJAX the POST variable is set from the three commands xmlhttp.open,.setRequestHeader and .send. Dont these take the place of the POST action in an html form?
  8. Just wanted to share the code in case some other newbie ever needs it. This stores the $id variable then calls the modal box to delete the record. Part of my trouble was I had to read up on scopes. Declaring id as var id was making it a local variable, only accessible within that function. Removing the var part makes the id variable default to a Global variable and thus accessible outside the scope of the function. Anyhow here it is and please feel free to weigh in to improve the solution. JS: <!--JavaScript for popup alert to Delete a row using Bootstrap modal --> <script type="text/javascript"> $(document).ready(function(){ $(document).on('click', '.delete-row', function(e){ e.preventDefault(); id = $(this).attr('data-id'); // get id from td element and store it in Global variable $id. }); $(document).on('click', '#del-row', function(e){ window.location = 'printer_delete.php?id=' + id; // call printer_delete.php with $id value to delete row. }); }); </script>
  9. Thanks I finally got it to work as follows: js: <!--JavaScript for popup alert to Delete arow using Bootstrap modal--> <script type="text/javascript"> $(document).ready(function(){ $(document).on('click', '.delete-row', function(e){ e.preventDefault(); var id = $(this).attr('data-id'); // get id from td element and store it in $id. window.location = 'printer_delete.php?id=' + id; // call printer_delete.php with $id value to delete row. }); }); </script> PHP: echo "<td><a class='one glyphicon glyphicon-trash delete-row' data-toggle='modal' data-placement='bottom' title='Delete Printer Record' href='#' data-target='#myModal' aria-hidden='true' data-id='" . $row['id'] . "'></a></td>"; Now if you still have any patience left, that deletes the row fine the only thing is I would like the Bootstrap modal delete confirmation button to do the actual delete rather then the trashcan icon in the table. If you have any idea on how to proceed on that : Bootstrap code: <!-- Modal Code --><div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button> <h4 class="modal-title" id="myModalLabel">Delete Row Confirmation</h4> </div> <div class="modal-body"> Are You Sure You Want To Delete This Row? </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <button type="button" id="del-row" class="btn btn-primary">Delete Row</button> </div> </div> </div></div> Thanks again for your patience.
  10. I'm sorry I'm getting a little punch drunk with this one. The original code segment is <!--JavaScript for popup alert to Delete arow using Bootstrap modal <script type="text/javascript"> $(document).ready(function(){ $(document).on('click', '#del-row', function(e){ e.preventDefault(); var id = $("#delete-row").attr('data-id'); // get id from td element and store it in $id. window.location = 'printer_delete.php?id=' + id; // call printer_delete.php with $id value to delete row. }); }); </script> --> And the poster who I got this from advised that the data-id value would have the current id from the row : echo "<td><a class='one glyphicon glyphicon-trash delete-row' id='delete-row' data-toggle='modal' data-placement='bottom' title='Delete Printer Record' href='#' data-target='#myModal' aria-hidden='true' data-id='" . $row['id'] . "'></a></td>"; We now know that is wrong. Is there a correct way to assign the id var the current clicked id value so I can assign it and use it in the window.location command? Or perhaps you have a more elegant way? Thanks.
  11. OK when I did it like this: <script type="text/javascript"> $(document).ready(function(){ $( "#del-row" ).click(function() { $( this).remove();});}); </script> That made the Delete Row on the modal disappear:( When I did this : <script type="text/javascript"> $(document).ready(function(){ $( "#del-row" ).click(function() { $( delete-row).remove();});}); </script> Modal box does nothing when I click on delete row button. I'm confused to because I dont want to delete the row on this page but I want to get the $row[id} value so I can call the "window.location = 'printer_delete.php?id=' + id;" line and send it. Here is a link to pastebin with the code for the page: http://pastebin.com/pFR5itswmaybe this will help to see what I'm trying to accomplish.Thanks for yopur patience...
  12. I tell you I tried like a dozen different examples and none worked. If someone could help me out here and show me the jquery I need to get this to work I would be extremely grateful.Thanx...
  13. I've been modifying my site and the script is supposed to get the id value stored in $row[id] field and then call delete_printer.php?id. This logic works fine for me modifying a record yet when doing a delete I get only the first row id for all rows on that page I try to delete. I've used this same line many times with no trouble and yet now it is broken and I cant see why. JS: <script type="text/javascript">$(document).ready(function(){$(document).on('click', '#del-row', function(e){e.preventDefault();var id = $("#delete-row").attr('data-id'); // get id from td element and store it in $id.window.location = 'printer_delete.php?id=' + id; // call printer_delete.php with $id value to delete row.});});</script> PHP: echo "<td><a class='one glyphicon glyphicon-edit' aria-hidden='true' data-toggle='tooltip' data-placement='bottom' title='Click Here to Edit' href='printer_update.php?id=" . $row['id'] . "'></a></td>";echo "<td><a class='one glyphicon glyphicon-trash delete-row' id='delete-row' data-toggle='modal' data-placement='bottom' title='Delete Printer Record' href='#' data-target='#myModal' aria-hidden='true' data-id='" . $row['id'] . "'></a></td>"; The first PHP line to modify a record works fine. This is the first time I've used the PHP in conjunction with the Javascript so I'm thinking in there lies the problem but being relatively new to JS I'm in need of some help. Any input would be welcome.
  14. This was the trouble: Black_Toner_Cart_Num = :Black_Toner_Cart_Num Table was Blk_Toner_Cart_Num. Is there anyway to find these MySQL error messages like you can with PHP?
  15. I have a page that adds printer info into an Inventory tracking site. It passes form values to insert_printer.php via _$POST. I have used the following code before to insert a row with no problems and yet now it is not working. I have verified the php with a validator and that is correct. I put in a a print_r ($_POST) and the values are being passed to the insert page ok. I have looked at this thing for hours and nothing is popping out. Perhaps a new set of eyes will see it right away: $stmt = $link->prepare("INSERT INTO Printer_Inventory SET Printer_Name = :Printer_Name, Physical_Loc = :Physical_Loc, Static_IP_Addr = :Static_IP_Addr,Printer_Model = :Printer_Model, Black_Toner_Cart_Num = :Black_Toner_Cart_Num, Blk_Toner_Changed = :Blk_Toner_Changed,Cyan_Toner_Cart_Num = :Cyan_Toner_Cart_Num, Cyan_Toner_Changed = :Cyan_Toner_Changed,Mgnt_Toner_Cart_Num = :Mgnt_Toner_Cart_Num, Mgnt_Toner_Changed = :Mgnt_Toner_Changed,Yllw_Toner_Cart_Num = :Yllw_Toner_Cart_Num, Yllw_Toner_Changed = :Yllw_Toner_Changed");$stmt->execute(array(':Printer_Name' => $Printer_Name,':Physical_Loc' => $Physical_Loc,':Static_IP_Addr' => $Static_IP_Addr,':Printer_Model' => $Printer_Model,':Black_Toner_Cart_Num' => $Black_Toner_Cart_Num,':Blk_Toner_Changed' => $Blk_Toner_Changed,':Cyan_Toner_Cart_Num' => $Cyan_Toner_Cart_Num,':Cyan_Toner_Changed' => $Cyan_Toner_Changed,':Mgnt_Toner_Cart_Num' => $Mgnt_Toner_Cart_Num,':Mgnt_Toner_Changed' => $Mgnt_Toner_Changed,':Yllw_Toner_Cart_Num' => $Yllw_Toner_Cart_Num,':Yllw_Toner_Changed' => $Yllw_Toner_Changed)); This works ok manually from phpMyAdmin: INSERT INTO Printer_Inventory (`Printer_Name`,`Physical_Loc`,`Static_IP_Addr`,`Printer_Model`) VALUES ('HP','SHS','172.16.xxx.xxx','HP 3102'); Get an error message: I am correct in assuming I dont have to account for the primary key id field? I never had to before. I imported a .csv file to the database ok. Attached is the SQL structure. Printer_Inventory.txt
  16. Follow Up. I stand corrected the jquery function does style it the same way, and thanks for the heads up I had to add class=delete-row. I would be curious though if you have any experience calling the modal plugin via js and how it is done.
  17. The only thing is then will I have the styling the same? The Edit option is right before it and uses the tooltip from Bootstrap. I was wondering, I tried activating the modal thru javascript($('#myModal').modal(show) but it did not work, that is why I fell back on the attribute data-target way of activating the modal for the confirmation box. Do you have any experience using the js way and is there something there not mentioning in the Bootstrap documentation to make it work?
  18. I need to assign the data attribute data-toggle used in Bootstrap two values like: echo "<td><a class='one glyphicon glyphicon-trash' id='delete-row' data-toggle='modal tooltip' data-placement='bottom' title='Delete Record' href='#' data-target='#myModal' aria-hidden='true' data-id='" . $row['id'] . "'></a></td>"; cause this element has a tooltip and is also used to trigger a modal when clicked. However I cant get this to work. Does anyone know if this can be done and how because I see no documentation and I've tried several different scenarios all no good.
  19. I have a form that wont stay aligned when I shrink the page. The div2 will move over to the left and intrude on div1 and the button positions become all jumbled. I have been reading up on css and thought the way to go was making a wrapper div then two child divs for my input types but it doesnt seem to work. I have tried all sorts of things I have found all to no avail. Ideally I would like the elements to all shrink proportionally and stay alaigned as the window shrinks. Absent that I would at least like the elements to just disappear off screen as it shrinks with scroll bars. Any help is musch appreciated. HTML: <form action="updated_tt.php" method="post"><input type="hidden" name="ud_id" value="<?php echo $id; ?>" /><div id="wrap"> <div id="div1"> Trouble Report/Request:<br/> <textarea class="tt_fld5" name="ud_problem" maxlength="300" rows="3" wrap=HARD ><?php echo $problem; ?></textarea> <br/> </div> <div id="div2"> Action Taken: <br/> <textarea class="tt_fld5" name="ud_act_tkn" maxlength="300" cols="40" rows="3" wrap=HARD ><?php echo $act_tkn; ?></textarea> </div></div><br/><br/><?php/* Here we create an array of the drop down list choices, then go thru the array one by one and check for the "selected" attribute. If the value retrieved from MySQL equals the ddown_options array element then construct <option> line with the "selected" value, else construct a normal <option> line. Note We have to Make this a form here.*/ $option_to_preselect = $tkt_status;$ddown_options = array( 'Open', 'Referred', 'Closed',);echo '<span class=tt_fld>Ticket Status: ' . '</span>' . '<br />';$arrlength=count($ddown_options);print '<select name="ud_ticket_status" id="input_select">';for ($x=0;$x<$arrlength;$x++){ if ($ddown_options[$x] == $option_to_preselect) { print ' <option value="' . $ddown_options[$x] . '"' . ' selected="selected">' . $ddown_options[$x] . '</option>'; } else { print ' <option value="' . $ddown_options[$x] . '">' . $ddown_options[$x] . '</option>'; }}print '</select>';echo '<br />';echo '<br />';/* For Assigned Tech...*/echo '<span class=tt_fld>Assigned To: ' . '</span>' . '<br />';$option_to_preselect = $assgnd_tech;$ddown_options = array( 'Unassigned', '***', '***', '***', '***',);$arrlength=count($ddown_options);print '<select name="ud_assgnd_tech" id="input_select">';for ($x=0;$x<$arrlength;$x++){ if ($ddown_options[$x] == $option_to_preselect) { print ' <option value="' . $ddown_options[$x] . '"' . ' selected="selected">' . $ddown_options[$x] . '</option>'; } else { print ' <option value="' . $ddown_options[$x] . '">' . $ddown_options[$x] . '</option>'; }}print '</select>';?><br /> <center><button type="submit" class="btn btn-primary btn-lg" onclick="updated.php">Update Trouble Ticket</button></center><br /><!-- Indicates a dangerous or potentially negative action --><center><button type="button" class="btn btn-danger" onclick="Del_Function()">Delete Trouble Ticket</button></center></form> CSS: #wrap { overflow: hidden; padding: 2px 0 0 0; width: 94%; margin: 25 auto;}#div1 { font-weight: bold; font: x-large serif; font-weight: bold; float: left; width: 47%; margin-top: 10px; }#div2 { font: large serif; float: right; width: 47%; margin-top: 10px; font-weight: bold; font-size: x-large;}
  20. Thanks for your help. What was happening was the TT# field wasnt getting updated. After RTFM on bindValues I got it to work. For some reason bindParam didn't work.
  21. I'm trying this by following what I searched online but not working: $updt=$link->prepare("UPDATE TT_Form SET TT_NUM = :TT_Num WHERE LName = ?");$updt->execute(array($LName)); Also read further and tried this: $updt=$link->prepare("UPDATE TT_Form SET TT_NUM = :TT_Num WHERE LName = :LName");$updt-> bindParam(':TT_Num', $TT_Num, PDO::PARAM_INT);$updt->bindParam(':LName', $Lname, PDO::PARAM_STR, 15);$updt->execute(); Still no good. And all records have TT# of zero... Update 3: Here's the code that worked. please weigh in if something should be revised: $updt=$link->prepare("UPDATE TT_Form SET TT_NUM = :TT_Num WHERE LName = :LName");$updt-> bindValue(':TT_Num', $TT_Num, PDO::PARAM_INT);$updt->bindValue(':LName', $LName, PDO::PARAM_STR);$updt->execute(); I'm just having a hard time with these positional vs. named parameters. Anybody have a link to a lucid discussion on the topic? FYI here is what solved the problem using PDO: <?phpini_set('display_errors',1);error_reporting(E_ALL);include 'dbinfo.inc.php';$Date_Entered=$_POST['Date_val'];$FName=$_POST['FName_val'];$LName=$_POST['LName_val'];$Dept=$_POST['q4_department'];$Rch_Num=$_POST['q15_reach'];$Rm_Number=$_POST['q5_roomNumber'];$E_Mail=$_POST['q6_email6'];$Prob_Cat=$_POST['q11_problemCatregory'];$Rm_Avail=implode(", ",$_POST['q12_pleaseSpecify12']);$Problem=$_POST['q8_explainProblem'];$Tkt_Status=$_POST['Tkt_Status_val'];$Assgnd_Tech="Unassigned";$TT_Num=0;//Clean input to make sure it is formatted with a leading Capital letter.$FName=ucfirst($FName);$LName=ucfirst($LName);try { $link=new PDO($dsn, $username,$password); }catch (PDOException $e) { $error_message=$e->getMessage(); echo "<h1>Resource Unavailable. Please Contact the System Administrator</h1>"; }$stmt = $link->prepare("INSERT INTO TT_Form SET Date_Entered = :Date_Entered, FName = :FName, LName = :LName, Dept =:Dept , Rch_Num = :Rch_Num, Rm_Number = :Rm_Number, E_Mail = :E_Mail, Prob_Cat = :Prob_Cat, Rm_Avail = :Rm_Avail, Problem = :Problem, Tkt_Status = :Tkt_Status, Assgnd_Tech = :Assgnd_Tech, TT_Num = :TT_Num");$stmt->execute(array( ':Date_Entered' => $Date_Entered, ':FName' => $FName, ':LName' => $LName, ':Dept' => $Dept, ':Rch_Num' => $Rch_Num, ':Rm_Number' => $Rm_Number, ':E_Mail' => $E_Mail, ':Prob_Cat' => $Prob_Cat, ':Rm_Avail' => $Rm_Avail, ':Problem' => $Problem, ':Tkt_Status' => $Tkt_Status, ':Assgnd_Tech' => $Assgnd_Tech, ':TT_Num' => $TT_Num ));//We use the insertId function to get the last row inserted id and add 100.$TT_Num = $link->lastInsertId();// Simple error check to see if row got added from above INSERT statement.if ($TT_Num == 0) { echo "<h2>Error in Adding Trouble Ticket. Please Contact the System Administrator.</h2>"; exit; }else { $TT_Num = $TT_Num + 100; } $updt=$link->prepare("UPDATE TT_Form SET TT_NUM = :TT_Num WHERE LName = :LName");$updt->bindValue(':TT_Num', $TT_Num, PDO::PARAM_INT);$updt->bindValue(':LName', $LName, PDO::PARAM_STR);$updt->execute();echo '<br /n>';echo '<br /n>';echo '<h2>Your Trouble Ticket Number Is: ' . $TT_Num .'</h2>'; ?>
  22. ok one problem solved. That "$stmt->execute();" wasn't needed. That's what you get for copying and pasting without understanding it is your doing and why. Still have the second trouble which is why I wanted to use prepared statements all along. I cant add a record with apostrophes and get the next tt number in the series. I get a zero on anything I add. Update: In experimenting this only happens if there is an apostrophe in the Last Name Field. So I assume trouble is with line: $updt=$link->prepare("UPDATE TT_Form SET WHERE LName = $LName");$updt->execute(array(':TT_Num' => $TT_Num));
  23. Appreciate your input and am on the road but still have two problems. 1. When I insert a record, two are created in mysql 2. if I insert a name with an apostrophe I get two plus the record has a Trouble Ticket number of zero. I keep on looking over the code but dont see why this happening. If you have the inclination I would appreciate any input as to what is going on.Here is my code so far: <?phpini_set('display_errors',1);error_reporting(E_ALL);include 'dbinfo.inc.php';$Date_Entered=$_POST['Date_val'];$FName=$_POST['FName_val'];$LName=$_POST['LName_val'];$Dept=$_POST['q4_department'];$Rch_Num=$_POST['q15_reach'];$Rm_Number=$_POST['q5_roomNumber'];$E_Mail=$_POST['q6_email6'];$Prob_Cat=$_POST['q11_problemCatregory'];$Rm_Avail=implode(", ",$_POST['q12_pleaseSpecify12']);$Problem=$_POST['q8_explainProblem'];$Tkt_Status=$_POST['Tkt_Status_val'];$Assgnd_Tech="Unassigned";$TT_Num=0;//Clean input to make sure it is formatted with a leading Capital letter.$FName=ucfirst($FName);$LName=ucfirst($LName);try { $link=new PDO($dsn, $username,$password); }catch (PDOException $e) { $error_message=$e->getMessage(); echo "<h1>Resource Unavailable. Please Contact the System Administrator</h1>"; }$stmt = $link->prepare("INSERT INTO TT_Form SET Date_Entered = :Date_Entered, FName = :FName, LName = :LName, Dept =:Dept , Rch_Num = :Rch_Num, Rm_Number = :Rm_Number, E_Mail = :E_Mail, Prob_Cat = :Prob_Cat, Rm_Avail = :Rm_Avail, Problem = :Problem, Tkt_Status = :Tkt_Status, Assgnd_Tech = :Assgnd_Tech, TT_Num = :TT_Num");$stmt->execute(array( ':Date_Entered' => $Date_Entered, ':FName' => $FName, ':LName' => $LName, ':Dept' => $Dept, ':Rch_Num' => $Rch_Num, ':Rm_Number' => $Rm_Number, ':E_Mail' => $E_Mail, ':Prob_Cat' => $Prob_Cat, ':Rm_Avail' => $Rm_Avail, ':Problem' => $Problem, ':Tkt_Status' => $Tkt_Status, ':Assgnd_Tech' => $Assgnd_Tech, ':TT_Num' => $TT_Num ));$stmt->execute();//We use the insertId function to get the last row inserted id and add 100.$TT_Num = $link->lastInsertId();// Simple error check to see if row got added from above INSERT statement.if ($TT_Num == 0) { echo "<h2>Error in Adding Trouble Ticket. Please Contact the System Administrator.</h2>"; exit; }else { $TT_Num = $TT_Num + 100; } $updt=$link->prepare("UPDATE TT_Form SET TT_Num = :TT_Num Where LName = '$LName'");$updt->execute(array(':TT_Num' => $TT_Num));echo '<br /n>';echo '<br /n>';echo '<h2>Your Trouble Ticket Number Is: ' . $TT_Num .'</h2>'; ?>
  24. Forgive me but I just want to understand this. I'm going to do: $stmt = $link->prepare("INSERT INTO TT_Form SET Date_Entered = :Date_Entered, FName = :FName, LName = :LName, Dept =:Dept , Rch_Num = :Rch_Num, Rm_Number = :Rm_Number, E_Mail = :E_Mail, Prob_Cat = :Prob_Cat, Rm_Avail = :Rm_Avail, Problem = :Problem, Tkt_Status = :Tkt_Status, Assgnd_Tech = :Assgnd_Tech, TT_Num = :TT_Num");$stmt->execute(array( ':Date_Entered' => $Date_Entered, ':FName' => $FName, ':LName' => $LName, ':Dept' => $Dept, ':Rch_Num' => $Rch_Num, ':Rm_Number' => $Rm_Number, ':E_Mail' => $E_Mail, ':Prob_Cat' => $Prob_Cat, ':Rm_Avail' => $Rm_Avail, ':Problem' => $Problem, ':Tkt_Status' => $Tkt_Status, ':Assgnd_Tech' => $Assgnd_Tech, ':TT_Num' => $TT_Num )); Then Delete all the $query statements $query = "INSERT INTO TT_Form VALUES('', '$Date_Entered', '$FName', '$LName','$Dept','$Rch_Num', '$Rm_Number', '$E_Mail', '$Prob_Cat', '$Rm_Avail', '$Problem', '', '$Tkt_Status', '$Assgnd_Tech', '$TT_Num')"; and this which already commented out: /*$query = "INSERT INTO TT_Form VALUES('', '$Date_Entered', '$FName', '$LName','$Dept','$Rch_Num', '$Rm_Number', '$E_Mail', '$Prob_Cat', '$Rm_Avail', '$Problem', '', '$Tkt_Status', '$Assgnd_Tech', '$TT_Num')";$result = $link->query($query);*/ and then how am I inserting into the Db. Thru a stmt->execute()? And what to do with the $result = $link->query($query); Sorry for the questions but I am just having a hard time getting my head around prepared statements w/PDO. Thanks for the patience...
  25. I went with this. It works. If anybody has a solution that works more elegantly please weigh in. I guess PDO doesnt handle apostrophes on its own? <?phpinclude 'dbinfo.inc.php';$Date_Entered=$_POST['Date_val'];$FName=$_POST['FName_val'];$LName=$_POST['LName_val'];$Dept=$_POST['q4_department'];$Rch_Num=$_POST['q15_reach'];$Rm_Number=$_POST['q5_roomNumber'];$E_Mail=$_POST['q6_email6'];$Prob_Cat=$_POST['q11_problemCatregory'];$Rm_Avail=implode(", ",$_POST['q12_pleaseSpecify12']);$Problem=$_POST['q8_explainProblem'];$Tkt_Status=$_POST['Tkt_Status_val'];$Assgnd_Tech="Unassigned";$TT_Num=0;//Clean input to make sure it is formatted with a leading Capital letter.$FName=ucfirst($FName);$LName=ucfirst($LName);$Eqpmnt_Brwd=ucfirst($Eqpmnt_Brwd);//Check For Apostrophes$FName = addslashes($FName);$LName = addslashes($LName);try { $link=new PDO($dsn, $username,$password); }catch (PDOException $e) { $error_message=$e->getMessage(); echo "<h1>Resource Unavailable. Please Contact the System Administrator</h1>"; }$query = "INSERT INTO TT_Form VALUES('', '$Date_Entered', '$FName', '$LName','$Dept','$Rch_Num', '$Rm_Number', '$E_Mail', '$Prob_Cat', '$Rm_Avail', '$Problem', '', '$Tkt_Status', '$Assgnd_Tech', '$TT_Num')";$result = $link->query($query);//We use the insertId function to get the last row inserted id and add 100.$insertId = $link->lastInsertId();echo $insertId;// Simple error check to see if row got added from above INSERT statement.if ($insertId == 0) { echo "<h2>Error in Adding Trouble Ticket. Please Contact the System Administrator.</h2>"; exit; }else { $insertId = $insertId + 100; } $count=$link->prepare("UPDATE TT_Form SET TT_Num = id + 100 Where LName = '$LName'");$count->execute();echo '<br /n>';echo '<br /n>';echo '<h2>Your Trouble Ticket Number Is: ' . $insertId .'</h2>'; ?>
×
×
  • Create New...