Jump to content
Larry Ullman's Book Forums

Max

Members
  • Posts

    112
  • Joined

  • Last visited

Everything posted by Max

  1. Thanks, Larry - I think I have my head round it now - that one apostrophe entered through a textarea caused me a lot of chaos. Keep up the great work.
  2. Following your book, I have now added mysqli_real_escape_string() to the php which actually works for everything except `` and ´´ which it doesn't escape, but I will take the risk of those never being used by the people who are uploading data. They are not the standard apostrophes but Hex 60. To get to the root of the problem, what I did was to comment out the steps whereby I uploaded the data to MySQL and instead did an 'echo' instead: e.g. $update = "UPDATE blah SET Name = '$name', Address = '$address', Description = '$desc' WHERE Ref = $ref LIMIT 1"; echo $update; I then ran the script and copy/pasted the result from the screen into the phpMyAdmin 'SQL' service and found that it failed. On investigation, it was the apostrophe causing th problem. I hope that this helps others.
  3. In addition, I have been caught out with uploading text to MySQL from a textarea - when people use an apostrophe - as in "I've" - then the MySQL sees that apostrophe as the end of a data set as in - UPDATE blah SET Comments = 'blah blah it's blah blah' you find that MySQL has thrown it all out because it has seen the " ' " as a termination character. The problem is that there are so many apostrophes on a keyboard - ' , ` , ´ , " etc - so I have included a str_replace routine to turn these into spaces as \' doesn't seem to work in all circumstances.
  4. Hi Larry Comments genuinely meant - and if I am mad enough to scramble my brains by learning something new I will always look for your books on that subject. Regarding the character encoding, I am referring to a textarea such as this one. Head is set to: <meta charset="utf-8"> and the MySQL "utf8_unicode_ci". On my keyboard are characters such as an "enye" - the n with a tilde on top. Also, it would be nice for people to have a way of writing m2 as for 'square metres'. When I put the 'enye' into MySQL using phpMyAdmin it works and reads correctly, but when read by html in my web page it comes out as '�'. I guess the obvious thing for me to do is use the same software as is used on this forum which looks like "ipsEditor" (Invision Power). For example, you have X2 and X2 on the control panel. To keep it simple, I guess that I could put instructions under the textarea field saying "To insert superscript 2 type ' ² '" as most people don't have superscript 2 on their keyboard anyway. Here's a quick and dirty program I wrote to test it: <!doctype html> <html lang="en"> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta charset="utf-8"> <!--[if lt IE 9]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> </head> <body> <?php echo '<h2>'; echo 'This is the raw data: Ñ'; echo '<br /><br /><br />'; echo 'This is using HTML entities: Ñ'; echo '<br /><br /><br />'; $ent = "Ñ"; echo 'This is using htmlentities: ' . htmlentities($ent); echo '</h2></body></html>'; If I run this on the server, I get the following: This is the raw data: � This is using HTML entities: Ñ This is using htmlentities: Best regards Max
  5. Hi again Larry, Character encoding....Hmmmm If I write HTML, and want to include an ampersand -> & - or a euro sign -> € or alternatively € I type exactly those characters. The question is when one is using php to write text to mySQL. For example, if you have a web page which invites a customer to upload text to the database. If they use a keyboard that has unusual characters such as a half, which I would type in HTML as ½ it is not saved as such even though the DB has UTF-8 encoding. So...what happens when the text is retrieved from the DM and sent to the browser is that you get the ?inside a black diamond symbol. I have read your book a milion times (as well as your excellent javascript tome) and just can't get my head round it. I suppose I could write a load of turgid php such as 'str_replace....' for each character we are likely to come across. Any advice would be much appreciated. Interestingly, the Daily Mail website forum has the same problem, which doesn't make me feel any better, to be honest!! Thanks again for all your time spent on us dunces! P.S. Another question - how would you offer the client the option to put into text, say, a superscript 2 in the sense of metres squared - or in the USA - feet squared when there is no key for that? Sorry for the spelling in the title!!
  6. Hi Abigail Thanks for that. My provider doesn't offer e-mail help - you have to phone them and I was hoping to avoid the embarrassment of admitting that I'd done something stoopid !! The web site will be back tomorrow (did before when I screwed up). I would be nice to be able to kill it through php some way. I have tried most things - logging out, uploading (FTP) a safe version of the program. Regards Max
  7. Larry, what would we do without you??? How about something like this??: //We'll call the variable that contains the text '$my_text' // html elements are not all the same size, eg <h1>, </strong>, <small> // Do a 'while...'....then.... //Find the first opening tag (<) $first = strpos("<") //Find first closing tag (>) $second = strpos('>',$my_text) $length_of_string = $second - $first; //Remove the offending element substr_replace($my_text, "", $first, $length_of_string) //end while Unfortunately, this removes all of the HTML formatting. I have found this: html_entity_decode() Function which I'm going to have a play with to see how well it works - I'll report back. Regards Max
  8. Hi Larry Thanks for that, but the php seems to be running in the server - I still get hundreds of e-mails per minute with the same error (the error is just a notice, not related to the problem. Eventually the server blocks the website for a day and just gives a 'Website not available' message. I guess they're waiting for the server to cool down after whizzing round in circles . The worst of it is that I feel so bally FOOLISH!!
  9. Here's a general question... I suspect most of us have done it at some time - I certainly have - uploading a program by mistake to a server, that has an infinite loop in it (usually 'while(){....}') which means that the program whizzes round a million times sending hundreds of error messages to your e-mail address. QUESTION: Eventually the server will kill the program automatically, but is there any way (in php or otherwise server-side) to kill it? I suppose one could add a line in the loop such as $x+; if (x$>'1000'){die()} to save this embarrassment! "To err is human, but it takes a computer to REALLY $&** things up!!"
  10. I wonder if anyone can help with this little puzzle. I am creating a listing of properties which includes a photo and a description in html. The description is drawn down from MySQL but may be 2,000 plus characters long. As I only want to display the first, say, 300 characters and the <a> link (you know the sort of thing: Wonderful property, hot and cold running water, great views....see more.... ) The code is:.... if($prop_live == "1") { echo '<tr style ="background-color: #FFFF99; border-collapse: separate; border-spacing: 12px 12px;"><td>' . $prop_name . '<br /><a href = "images/properties/' . $prop_ref . 'jpg" target = "_blank"><img src = "images/properties/' . $prop_ref . 'jpg" width = "160" alt = "Image missing - sorry." /></a></td><td style = "text-align: left; vertical-align: top; padding: 20px; ">' . substr($prop_desc, 0, 300) . ' ...more information....<br /><br />' . $elec2 . '<br />' . $water2 . '</td><td style = "text-align: right; vertical-align: top; padding: 20px;">' . $prop_price . '</td></tr>'; } (Note: I haven't added the <a href> bit yet around ...more information...) All is well and good until we are unlucky enough to encounter an html control such as <sup> or á and cut it in half. We then have incomplete html code which messes up the <td> or worse. I have scratched my head over this and been through all of the php text handling functions trying to cut the length of the string e.g. 303 or 298 characters or whatever, where one encounters a space. The alternative would be to convert the html code <sup>2</sup> before 'echo'ing it. I guess striptags would work but leave us with a non-superscript 2. Thanks Max
  11. That's great, Larry - will reduce server load. I am a minimalist so love to simplify my software.
  12. You can highlight the screen text and paste it here e.g.: Back to PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (4th Edition) · Next Unread Topic → Reply to this topic My Media
  13. Hi all, and especially Larry, I have encountered an interesting issue when uploading images using a reference from recently uploaded MySQL data. What I am trying to do is, in one page, send data regarding a property (real estate in US) and an image. I want the image to have the same reference number as the property. This is the schematic:... 1) Input html and upload image -> POST to php. 2) INSERT property data -> MySQL 3) SELECT property reference (Primary key) generated by MySQL. 4) Use the property reference + ".jpg" as filename -> the image to a subdirectory on server. The issue is that it seems that MySQL can take a bit too long to do the SELECT query which then throws up a '$image_no undefined variable and the image is saved as '.jpg' instead of e.g. 234.jpg. I think that I have solved it thus (my comments in red): Section of whole program: Check if user has sent html POST and uploaded an image (other conditionals not shown here for conciseness): if(isset($_FILES['upload'])) { ....then insert the data into MySQL: $q = "INSERT INTO db655736144.Properties (Property_name, Owner_name, Owner_phone_number, Owner_email, Description, Electricity, Water, Price, Commission, TCC_Comments) VALUES ('$pty', '$owner', '$phone', '$owner_email', '$desc', '$elec', '$water', '$price', '$comm', '$TCC')"; $r =@mysqli_query ($dbc, $q); //Run the query - could put this in a while loop as well? $allowed = array ('image/pjpeg', 'image/jpeg', 'image/JPG', 'image/jpg'); if(in_array($_FILES['upload']['type'], $allowed)) //Check for correct file type { $prop_id = "SELECT Property_Ref FROM Properties WHERE Property_name = '$pty'"; //Get property reference number $prop_id_action = @mysqli_query ($dbc, $prop_id); //Do query //$Property_ref is the array containing property ref no generated by MySQL while ($Property_ref = @mysqli_fetch_array($prop_id_action)) { $image_no = $Property_ref['Property_Ref']; // $image_no is the property ref no generated by MySQL. This while loop should only run once as Property_Ref is Primary key. } If MySQL hasn't done its bit yet, then you are left with $image_no equalling NULL and an error, AND an image called .jpg on the server. Also the program has to open a new subdirectory with the name Property Ref (e.g. Main/Properties/Adds/123 for additional images to be uploaded to later. This won't happen. This is the bit --------------------------------------------------------------------------------------------------- I have added a while loop that seems to work here:..... while (!$image_no) // If the image number from the query = NULL (!$image_no) { while ($Property_ref = @mysqli_fetch_array($prop_id_action)) // Repeat the query until we have a value for $image_no { $prop_id_action = @mysqli_query ($dbc, $prop_id); //Do query $image_no = $Property_ref['Property_Ref']; // $image_no is the property ref no generated by MySQL } } -------------------------------------------------------------------------------------------------- $_FILES['upload']['name'] = $image_no . '.jpg'; //Add file extension This is just to check that there isn't a subdirectory with the same name (shouldn't be)... if (move_uploaded_file($_FILES['upload']['tmp_name'], "images/properties/{$_FILES['upload']['name']}")) { if(is_dir('images/properties/more_images/' . $image_no)) { echo "<h2>There was a system problem - please call Max.</h2>"; echo '<p style = "font-weight: bold; font-size: 120%"><a href = "admin.php">Return to menu</a></p>'; die(); } No directory exists so we can contine by creating it: else { mkdir('images/properties/more_images/' . $image_no); Some HTML to return to menu etc:... echo '<p style = "font-weight: bold; font-size: 120%">File uploaded....<a href = "add_images.php?image_no=' . $image_no . '">Add more images</a>...or...<a href = "admin.php">Return to menu</a>....or just continue.....</p>'; } ....cont } else { echo "There was a system problem - please call Max."; die(); } } else { echo'<p class="emph">Please upload a .jpg image</p>'; } } I'd be very interesed in your comments regarding this 'fix'. Regards Max
  14. Sounds like the perfect solution - you could then do housekeeping once a month to delete any files hanging around kicking their heels. I'll have a look at the code and re-write it. Regards Max
  15. Hi Larry and everyone I have an academic type question so won't bore you with too much code. I am using the filenames generated by my camera (e.g. P9100399.JPG), uploading them, and then adding comments for a website later when I have time. I know you don't like doing this but hey! When uploading an image file, it is nice to check that it hasn't been uploaded to the same file previously. - Select file - Check if it exists on PC / size etc. etc. - Check if there is a matching filename using "if (file_exists(strtolower($_FILES['upload']['name'])))" - If exists, then "File exists on system. Do you want to overwrite?<a href upload_file2.php...etc>YES</a>" - Use SESSIONs to send $_FILES['upload']['temp_file] to upload_file2.php - Overwrite file with new image using SESSIONs to grab the temp filename. - Save filename to MySQL database with other data such as where taken, comments (to be updated later whenever), using the filename minus the file extension as the index, so it is therefore unique. The problem is that whether using $_POST or <a href> you seem to lose the temp file off of the server - I guess PHP erases it when you call recursively or to another program. What it means (when I'm programming anyway) is that if you want to overwrite the file, you need to re-start the process again (ignoring the 'file_exists' part) thus requiring the user to re-select the image. It works fine but I just feel that it is a bit clunky. Maybe Javascript would be a solution?
  16. P.S. Does the same with Microsofe Edge (of reason??), Chrome and Opera.
  17. PHP Version 7.0.14 System Linux info 3.0 #1337 SMP Tue Jan 01 00:00:00 CEST 2000 all GNU/Linux Build Date Dec 13 2016 07:47:37 BROWSER: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36stdClass Object ( [browser_name_regex] => ~^mozilla/5\.0 \(.*windows nt 10\.0.*\) applewebkit/.* \(khtml.* like gecko\) chrome/55\..*safari/.*$~ [browser_name_pattern] => Mozilla/5.0 (*Windows NT 10.0*) AppleWebKit/* (KHTML* like Gecko) Chrome/55.*Safari/* [parent] => Chrome 55.0 [platform] => Win10 [comment] => Chrome 55.0 [browser] => Chrome [version] => 55.0 [device_type] => Desktop [ismobiledevice] => [istablet] => ) Dear all Firstly, great to see your photo on the website, Larry (it's been some while since I've been on the forum!) - I'm still using your great book PHP6 and MYSQL5 which is still incredibly helpful!! Here is an issue that I haven't seen before - the POST method changing a full stop (period in US parlance) into an underscore: This is a little quick program I've done for demonstration purposes:... <?php if(isset($_POST)) { foreach($_POST AS $key => $value) { echo 'ID is:....:' . $key . '..........Comment is:..:' . $value . '<br /><br />'; } } echo' <form action="test_post.php" method = "post" enctype="multipart/form-data">'; $a = "test_the_post_1.jpg"; $b = "test_the_post_2.jpg"; echo ' <textarea name = "' . $a . '" rows = "5" cols = "100">Comment here 1</textarea><br /><br /> <textarea name = "' . $b . '" rows = "5" cols = "100">Comment here 2</textarea><br /><br /> <input type = "submit" name = "submit" value = "UPDATE"> </form>'; ?> When I run test_post.php I get the following: ID is:....:test_the_post_1_jpg..........Comment is:..:Comment here 1 ID is:....:test_the_post_2_jpg..........Comment is:..:Comment here 2 ID is:....:submit..........Comment is:..:UPDATE You will notice that the post "test_the_post_1.jpg" has become test_the_post_1_jpg. I have resolved this easily with str_replace() but don't really understand why it is occurring. Keep up the great work, Larry.
  18. I use SciTe which is free to download. You can do what you like with tabs etc and it won't upset your server. Shows all matching brackets, curly brackets etc.
  19. Hi Jack You can change that line of code to read (in the case of JPGs only): $allowed=array('image/pjpeg','image/jpeg','image/JPG');
  20. Hi I just wanted to inform you all of a problem that I had with mail(). I was using mail() under 1and1.com server with the "From:" address being the one that the enquirer put in the form e.g. fredbloggs@hotmail.com which works just dandy. However, I used the exact same code (with obvious adjustments) in a site under one.com and no mail came through. I talked online to a very helpful person and it transpired that with one.com your mail has to come from under the site e.g. From: info@myonedotcomwebsite.org. Not a big problem once you know about it except that the recipient then has to copy/paste the enquirer's e-mail address from the text instead of just hitting "REPLY". Hope this helps someone out of a fix sometime.
  21. Hi HartleySan No, am very happy with your help - just thought the info would be useful to surfers who use JS but not php. Gradients are very interesting - I have been looking at http://www.tutorialspoint.com/html5/canvas_create_gradients.htm but suggests that not all browsers are capable of this. For those who didn't see it under php6 and mysql5, here is the code for bar charts: <html> <head> <!--Load the AJAX API--> <script type="text/javascript" src="https://www.google.c...sapi"></script> <script type="text/javascript"> // Load the Visualization API and the piechart package. google.load('visualization', '1.0', {'packages':['corechart']}); // Set a callback to run when the Google Visualization API is loaded. google.setonloadCallback(drawChart); // Callback that creates and populates a data table, // instantiates the pie chart, passes in the data and // draws it. function drawChart() { // Create the data table. var data = new google.visualization.DataTable(); data.addColumn('string', 'Topping'); data.addColumn('number', 'Slices'); data.addRows([ ['Mushrooms', 3], ['Onions', 1], ['Olives', 1], ['Zucchini', 1], ['Pepperoni', 2] ]); // Set chart options var options = {'title':'How Much Pizza I Ate Last Night', 'width':400, 'height':300}; // Instantiate and draw our chart, passing in some options. var chart = new google.visualization.BarChart(document.getElementById('chart_div')); chart.draw(data, options); } </script> </head> <body> <!--Div that will hold the pie chart--> <div id="chart_div"></div> </body> </html>
  22. I have brought this topic over from php6 and mysql5 as it seems more appropriate to JS than php........... The question was:....... I was looking at an Iberdrola electricity invoice and working out how I would code the php to produce one, which was relatively straightforward, except at one point they put a bar chart to show the previous year's consumption....which got me wondering - is there any facility in php to produce something similar? This would be easy enough: x........x x........x...x x....x...x...x x....x...x...x x....x...x...x ________ J...M..M..J A...A..A..U N...R..Y..L BUT WOULD LIKE TO SOMETHING A BIT MORE FLASHY! ....and HartleySan replied thus:.... With newer browsers, you can very easily create gradients to somewhat spice up your graphs. As a more practical solution, you may want to consider a library like Google Chart Tools: https://developers.google.com/chart/ I believe they use the canvas element, so an HTML5-compatible browser is required. Edit: Apparently the charts fall back to VML graphics for non-HTML5 browsers, so maybe they work in all browsers. ------------------------------------ I reckon that package is very useful to developers.
  23. Hi HartleySan Yes - works well now, don't know where I went wrong - just copied and pasted it. I like the bar charts as well:.... <html> <head> <!--Load the AJAX API--> <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript"> // Load the Visualization API and the piechart package. google.load('visualization', '1.0', {'packages':['corechart']}); // Set a callback to run when the Google Visualization API is loaded. google.setonloadCallback(drawChart); // Callback that creates and populates a data table, // instantiates the pie chart, passes in the data and // draws it. function drawChart() { // Create the data table. var data = new google.visualization.DataTable(); data.addColumn('string', 'Topping'); data.addColumn('number', 'Slices'); data.addRows([ ['Mushrooms', 3], ['Onions', 1], ['Olives', 1], ['Zucchini', 1], ['Pepperoni', 2] ]); // Set chart options var options = {'title':'How Much Pizza I Ate Last Night', 'width':400, 'height':300}; // Instantiate and draw our chart, passing in some options. var chart = new google.visualization.BarChart(document.getElementById('chart_div')); chart.draw(data, options); } </script> </head> <body> <!--Div that will hold the pie chart--> <div id="chart_div"></div> </body> </html>
×
×
  • Create New...