-
Posts
3,404 -
Joined
-
Last visited
-
Days Won
55
Everything posted by Ch0cu3r
-
how do i do error handling for this php code?
Ch0cu3r replied to xaznbabyx's topic in PHP Coding Help
Use the comparison operator to see if the values are the same, Simple if/else logic // When you get the background and text color from the user // check if the chosen background and text colors are the same if($background_color == $text_color) { // colors are the same so output error message saying colors must not be the same // set page background to be white and text color to black } // colors are not the same else { // so set the page background and text color to the chosen values. } -
MySQL query to form fields based on selection of dropdown
Ch0cu3r replied to SF23103's topic in PHP Coding Help
Sorry header('Content-type', 'text/json'); should of been header('Content-Type: text/json');. Looks like I got nodejs setHeader function mixed up with PHP's header function there. You got the "Unable to perform this action at this time due to a server error" message because of a MySQL error. Looks like the error was because your actual column names differ to the ones you mentioned. For reference whenever you get a 500 Internal Server error check your servers error logs for the cause. -
how do i do error handling for this php code?
Ch0cu3r replied to xaznbabyx's topic in PHP Coding Help
What have you tried so far? I guess you are using the code given to you in your last topic? and did you not learn any thing about handling input errors in your other topic? -
You need to use array notation. foreach ($output['data']['500020315']['members'] as $item) { echo $item->role . '<br>'; }
-
Why do you have a separate forms for the file upload? The file upload field should be in the same <form> as your other fields, making sure to still apply enctype="multipart/form-data" attribute to the form. You will need to move the code that processes the upload into insert_ac.php
-
That worked for me using the demo code supplied in the download. I did have to do Ctrl+F5 after changing/applying filename: in the code though.
-
MySQL query to form fields based on selection of dropdown
Ch0cu3r replied to SF23103's topic in PHP Coding Help
If you do not want to form to reload when selecting the class name then you need to use AJAX. Example Code HTML Class Name: <select name="class_drop_down"> <?php ... your code for populating class names here ... </select> Class Number: <input type="text" name="class_number" value="" disabled="disabled" /> Class Date: <input type="text" name="class_date" value="" disabled="disabled" /> <!-- place this code at the bottom of your page before cloing </body></html> tags --> <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script> <script> // using jquery to apply an onchange event on the select element named class_drop_down $('select[name="class_drop_down"]').on('change', function() { // do an ajax request to get_class_details.php, passing selected the class name $.ajax({ url: 'get_class_details.php', method: 'POST', data: { class_name: $(this).val()}, dataType: 'json', // this function is called with the response from the request success: function(response) { // make sure a error was not returned if(response.error === undefined) { // set values for class number and date fields as follows $('input[name="class_number"]').attr('value', response.class_number); $('input[name="class_date"]').attr('value', response.class_date); } else { // demo purposes only, output error message as an alert alert(response.error); } }, }); }); </script> get_class_details.php <?php // set the content type to be JSON // returning the result of the AJAX request to be in JSON format header('Content-type', 'text/json'); include "/secret/path/to/login.php"; $db = new mysqli('localhost', $username, $password, $database); // Connect to DB using required login info if($db->connect_errno > 0){ die('Unable to connect to database [' . $db->connect_error . ']'); } // check class_name exists in POST if(isset($_POST['class_name'])) { // make sure it is not empty if(!empty(trim($_POST['class_name']))) { // use prepared to statement for get class number and data where the class the name matches $stmt = $db->prepare('SELECT class_number, class_date FROM ft_form_7 WHERE class_name = ?'); if($stmt) { // bind class name to the query $stmt->bind_param('s', $_POST['class_name']); // the class_number and class_date values returned by the query will be bound to these variables $stmt->bind_result($class_number, $class_date); // execute prepared query $stmt->execute(); // attempt to get first result if($stmt->fetch()) { // define our data array with the query result $data = array( 'class_number' => $class_number, 'class_date' => $class_date, ); } else { // no row returned, set error message stating class could not be found $data = array('error' => 'Class name "' . htmlentities($_POST['class_name'], ENT_QUOTES) . '" cannot be found'); } } // problem with the query else { // trigger error message for server admin containing mysql error message trigger_error('Unable to query ft_form_7 table: ' . $db->error); // output generic error message for user $data = array('error' => 'Unable to perform this action at this time due to a server error'); } } else { // class name is empty, set error message $data = array('error' => 'Class name cannot be empty'); } } else { // class name not set in POST $data = array('error' => 'Invalid Request'); } // output result in JSON echo json_encode($data); -
Can you show us the code for this and your database structure. Joining multiple tables should not be slow at all (no mater how many records you have) provided your are storing your data efficiently.
-
Try $info = array(); while ($row = $result->fetch_array(MYSQLI_ASSOC)) { if(!isset($info[$row['semester']])) { $info[$row['semester']] = array( 'semester' => $row['semester'], 'children' => array() ); } $info[$row['semester']]['children'][] = array( 'name' => $row['name'], 'value' => $row['value'] ); } $data = json_encode(array('id' => $row['sbuid'], 'children' => array_values($info))); echo $data;
-
Why are you manually defining that $menu array? And Where are all those $_SESSION variables being defined?
-
Try using filename: $("button").click(function(){ $("#table2excel").table2excel({ // exclude CSS class exclude: ".noExl", name: document.title, filename: "myFileName" }); });
-
If you are running the code from your website then the email is most likely being sent, but is most likely being flagged as spam in your inbox, so check your spam folder I already provided a link.
-
Where are you running this code form? localhost? If so then you need to configure PHP to use an SMTP server/mail client in order for emails to be sent. See the following page for configuration options available http://php.net/manual/en/mail.configuration.php If your SMTP server requires authentication (ie google mail, yahoo, outlook etc) before sending emails then you cannot use PHP's mail function. Instead you need to use a something like PHPMailer or Swiftmailer.
-
Explain what you mean by "isn't working"
-
As you did last time, define \t as the delimiter when calling fputcsv. fputcsv($fp, $fields, "\t");
-
You have a stray single quote in your query, this will cause an SQL error $result = mysqli_query($con,"SELECT * FROM amazon'"); By the way the code provided by Barand forces the file to be downloaded. If you want the file to be saved to the server. Then delete the header() lines and replace $fp = fopen('php://output', 'w'); to be $fp = fopen('amazon.csv', 'w');
-
Your host may not allow your mysql user to have FILES privileges. A work around is to have PHP create the csv file. Check out Barands post here using the sql2csv function.
-
1) Make sure the user you are using to connect to MySQL has FILES privileges 2) You need to specify the path for where you want the file be created. If you do not specify the path it will save the file in mysql's data directory (set by the datadir directive in mysql's my.ini config file). $dir = $_SERVER['DOCUMENT_ROOT']; mysqli_query($con,"SELECT * FROM amazon INTO OUTFILE '$dir/amazon.csv'"); 3) Set the necessary file permissions on the directory you are writing the csv file to. Otherwise mysql wont be able to create the file.
-
No. As I said you do not want to be creating a new table for each row returned by your query. Otherwise the columns in the table will always be misaligned, as per image 1. If you want to have your table be like image 2, then you need apply padding to the <th> and <td> tags in your css. th { padding: 10px; background-color: red; } td padding: 5px; background-color: grey } Live HTML example of having a new table for every row compared and without a new table.
-
Its is because you are outputting the <table> tag inside your foreach loops, this will mean it will create a new table for every result returned from your query. You do not want this to happen, all you want is the results to be output inside new table row( <tr></tr> tags). All you need to do is remove this line from inside your loops echo "<table style='border: solid 1px blue; background: gray'>";
-
So you are inserting a new row when the user logs in? Which results in duplicate rows over time. In that case you want to setup a unique key constraint, such as add the usern field as a Unique Key then use a INSERT ON DUPLICATE UPDATE query. Example INSERT INTO logs (usern, passwd, id, `date`, active, number) VALUES ('$username', '$password', $id, '$auth', NOW(), '$active', $number) ON DUPLICATE KEY UPDATE `date` = NOW(); The above query will attempt to insert a new row, if there is already a row with the same username then it will update that rows date value to the current date. If you want the existing row to be deleted and a new row to be inserted then use a REPLACE query Or if you do not want the row to be updated or deleted then use a INSERT IGNORE query
-
I have tested your function and I none of the images I used for re-sizing was rotated. How are you viewing the images?
-
Umm.. No you posted the code user_info() function. I asked for the code for createThumb() function
-
Its is because web browsers do not display whitespace characters such as newlines. What you need to is conver the newlines into a HTML break tags <br /> PHP has a function that can do this for you called nl2br. Use this when you echo your post. To format the text of your post you can either use bbcodes/markdown, or implement a WYSIWYG type editor, such as CKeditor
-
Is that the thumbnail images? Can you show us the code for the createThumb function?