Sorry will need on it unfortunately as tested it again and still not adding the info the db table and updating the other db table I did get bit lost in terms of what curly bracket closed the if check line and got bit lost following the other code
I have had another look again and trying to concentrate more on it and altered the coding and am slowly getting somewhere as I now get a success message shown on the page and checked the ticket_replies db table and it's got the reply message added in there but has not added in the ticket_id for some reason, it's added 0 in that column instead of the ticket id, for example the ticket I am working on ticket_id 66 Just checked the support_tickets db table and it's not updated the ticket_status column to PENDING SUPPORT The new code is below if (isset($_POST['submit'])) { // File upload configuration $targetDir = "support-ticket-images/"; $allowTypes = array('pdf','doc','docx','jpg','png','jpeg','gif'); $statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = ''; // Escape user inputs for security $reply_text = strip_tags($_POST['reply_text'], ENT_QUOTES); $username = htmlentities($_SESSION["user_name"], ENT_QUOTES); $user_id = htmlentities($_SESSION["user_id"], ENT_QUOTES); $fileNames = array_filter($_FILES['files']['name']); if(!empty($fileNames)){ foreach($_FILES['files']['name'] as $key=>$val){ // File upload path $fileName = basename($_FILES['files']['name'][$key]); $targetFilePath = $targetDir . $fileName; // Check whether file type is valid $fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION); if(in_array($fileType, $allowTypes)){ // Upload file to server if(move_uploaded_file($_FILES["files"]["tmp_name"][$key], $targetFilePath)){ // Image db insert sql $insertValuesSQL.="('".$fileName."', '$ticket_id', '".$username."','".$user_id."'),"; } } } } if( strlen($reply_text)>3 ){ $sql = "INSERT INTO ticket_replies (ticket_id, reply_text,user_type, user_id) VALUES ('$ticket_id','$reply_text','customer', '$user_id');"; $link->query($sql); if( strlen($insertValuesSQL)>3 ){ $sql= "INSERT INTO support_ticket_files (file_name, ticket_id, user_name, user_id) VALUES $insertValuesSQL"; $link->query($sql); } $sql="UPDATE support_tickets set ticket_status = 'PENDING SUPPORT' where ticket_id ='$ticket_id'"; $link->query($sql); } echo "Reply successfully submitted"; } else{ $errorMessage=" Enter a message "; } PHP: Is getting there hopefully just needs bit more tweaking to get it fully working, once it is, I'll then test the file upload as well just to make sure that works as well and then will focus on the new support ticket issue but think I can probably copy/paste some code from the file I am working on at the mo
Use this one: <?php if (isset($_POST['submit'])){ // File upload configuration $targetDir = "support-ticket-images/"; $allowTypes = array('pdf','doc','docx','jpg','png','jpeg','gif'); $statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = ''; // Escape user inputs for security $reply_text = strip_tags($_POST['reply_text'], ENT_QUOTES); $username = htmlentities($_SESSION["user_name"], ENT_QUOTES); $user_id = htmlentities($_SESSION["user_id"], ENT_QUOTES); $fileNames = array_filter($_FILES['files']['name']); if(!empty($fileNames)){ foreach($_FILES['files']['name'] as $key=>$val){ //File upload path $fileName = basename($_FILES['files']['name'][$key]); $targetFilePath = $targetDir . $fileName; // Check whether file type is valid $fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION); if(in_array($fileType, $allowTypes)){ // Upload file to server if(move_uploaded_file($_FILES["files"]["tmp_name"][$key], $targetFilePath)){ // Image db insert sql $insertValuesSQL.="('".$fileName."', '$ticket_id', '".$username."','".$user_id."'),"; }//move ends }//file type check ends }//foreach ends }//empty file check ends if(!empty($insertValuesSQL)){ $insertValuesSQL = trim($insertValuesSQL, ','); } //file checks done //now add comment to table if( strlen($reply_text)>3 ){ $sql = "INSERT INTO ticket_replies (ticket_id, reply_text,user_type, user_id) VALUES ('$ticket_id','$reply_text','customer', '$user_id');"; $link->query($sql); if( strlen($insertValuesSQL)>3 ){ $sql="INSERT INTO support_ticket_files (file_name, ticket_id, user_name, user_id) VALUES $insertValuesSQL "; $link->query($sql); } $sql="UPDATE support_tickets set ticket_status = 'PENDING SUPPORT' where ticket_id ='$ticket_id'"; $link->query($sql); }else{ echo "Error: Enter a message"; } //can display any error messages here }//submit
Just updated the code to the above and it's not adding the ticket_id still to the ticket_replies db table and not updating the support_tickets db table ticket_status column
Sorry yeah is a hidden input field just under the form tag as below <form action="" method="post" class="form-block ticketform" enctype="multipart/form-data"> <input type="hidden" name="ticket_id" value="<?php echo $_GET['ticket_id']; ?>"/> HTML: Do I still need to add the variable in using the code below? $ticket_id = htmlentities($_POST["ticket_id"], ENT_QUOTES); PHP:
Ian Haney Just noticed this error Change this line: $reply_text = strip_tags( $_POST['reply_text'], ENT_QUOTES ); to this: $reply_text = strip_tags( htmlentities( $_POST['reply_text'], ENT_QUOTES )); Then change all the below lines: $link->query($sql); to this: $link->query($sql) or die( $link->error ); See if an error is thrown. Also, how are you checking that nothing is going in DB? Are you seeing rows in phpmyadmin, or are you viewing replies on this script page itself?
If you are checking replies on this php script itself, then do one more modification. Add a header location just above the }else{ statement, just after the update query. header("location: your_script_name.php?ticket_id=$ticket_id"); }else{ echo "Error: Enter a message"; }
Yes, you do, otherwise your script won't have a value to put into the insert string. This is so if the user leaves their result open and they refresh the tab they don't insert the message over and over. I'd leave this for now but it's definitely something you want to do. There's a philosophy about separating working from layout and your code is very much intermingling. If your working code had feedback for the user it gets saved to the session and then displayed with the next page and removed from the session. Essentially the flow would be: display the ticket view page go to the update page save the new ticket reply & set a success/fail message redirect to the ticket view page & display any success/fail messages
I have amended the code and tested again and still the same unfortunately. I am actually checking in phpmyadmin and it's adding the row in the ticket_replies db table but still not adding the ticket_id to the column and not updating the ticket_status column in the support_tickets db table Also it's not throwing any error on the page itself
It's working now, I added in $ticket_id = htmlentities($_POST["ticket_id"], ENT_QUOTES); and is adding the ticket_id now to the relevant column and is updating the ticket_status column in the other db table to PENDING SUPPORT Wish me luck as will now try to include a file
Update: it has added the file all ok One quick question, is it possible to add the file upload onto the reply as well submitted by the user, I have attached a screenshot of where it would be good to have the filename of the uploaded file or files for the specific reply
It won't be possible to show the file near the reply itself, because you are not storing the ID of the ticket_reply table anywhere in the support_files_ticket table. You are only storing the ticket_id from the support_ticket table.
Ok, how easy is it to add the reply_id into the ticket_replies so can show the file_name for the reply itself, does it mean changing quite a lot of coding?
very easy and recommended. Use this thread as an example. You've created the ticket and we've all added bits of code. What if we couldn't tie those bits of code to a reply or view them in order? The same could happen with the photos, screenshots or whatever your users are attaching. Adding the column to the table is really easy. Adding it into your code will be a few lines at most. You need to get the reply_id back when it's inserted and then add that into insertValuesSQL, we'll use a placeholder and then swap it out when we know the value $insertValuesSQL .= "('{$fileName}', '{$ticket_id}','###', '{$username}','{$user_id}'),"; Code (php): $sql = "INSERT INTO ticket_replies (ticket_id, reply_text,user_type, user_id) VALUES ('$ticket_id','$reply_text','customer', '$user_id');"; $link->query($sql); /* GET THE ID FROM HERE */ $ticket_reply_id = ?????? if (strlen($insertValuesSQL) > 3) { $insertValuesSQL = str_replace('###', $ticket_reply_id, $insertValuesSQL); $sql = "INSERT INTO support_ticket_files (file_name, ticket_id, ticket_reply_id, user_name, user_id) VALUES {$insertValuesSQL} "; $link->query($sql); } Code (php):
I have added reply_id into the support_ticket_files db table and put it after the ticket_id column. Do I now copy the code you have put above Sarah?
Ok thank you, what do I put in where the question marks are on line $ticket_reply_id = ?????? Also do I leave the ### as they are on line $insertValuesSQL .= "('{$fileName}', '{$ticket_id}','###', '{$username}','{$user_id}'),";
Leave the hashes, we use a str_replace on them later. I don't know the mysqli command for getting the id of the new record so you need to work that one out. That code replaces the ???