Jump to content

All Activity

This stream auto-updates

  1. Today
  2. Yesterday
  3. Please use code tags <>. Basically you need to make up your mind as to what this script is intended to do: Return the data for an image, so that it can be referenced as the source in an image tag. Return Some html The problem you have right now is that you are setting the header, as if you wanted to return image data header ('Content-Type: image/png'); Then you return some html, which includes an img tag and an embedded base64 image. echo '<img src="data:image;base64, ' .$im. '" style="width:5in;height:5in;">'; So, if you want to return the data and reference the script in an <img src="..."> tag, then you would continue to use the header (as long as your image is actually going to be a .png) and just return the image data. Often this is what people want to do, when creating an image on the fly in the server. In your case, you started with some base64 data, that is perhaps an image. If so, then all your existing code would need is this // header ('Content-Type: image/png'); --NO! You are returning HTML not image data $b64 = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="; echo '<img src="data:image/png;base64, ' . $b64 . '" style="width:5in;height:5in;">'; Amongst the several confusing mistakes you are making currently, you read data into a GD OBJECT. Then you try and return that object in a place where you need base64 encoded data. If you are going to use a function, you need to actually take 30 seconds and look up the function and at minimum: Check the number and types of the parameters Check what the function returns I'm going to assume here that there is some reason you want to read image data into GD. In this case, you commented out all the code that you would need to actually have this function. At minimum it would be more like this: // header ('Content-Type: image/png'); --NO! You are returning HTML not image data $b64 = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="; $bin = base64_decode($b64); // Load GD resource from binary data $im = imageCreateFromString($bin); if (!$im) { die('Base64 value is not a valid image'); } echo '<img src="data:image/png;base64, ' . base64_encode(imagepng($im)) . '" style="width:5in;height:5in;">'; This is unsophisticated code, because it has the baked in assumption that your original source is the base64 encoded version of a .png image.
  4. If the field is a date then it should be a date. Don't make it a string. But what I was trying to point out was that you were asking MySQL to interpret the timestamp 2022-01-22T20:52:00.000Z according to the format %c-%e-%Y %T, or in other words M-D-YYYY HH:MM:SS. That's not going to work.
  5. Thanks reqinix. I did try that, as well as several other formats. I am not sure why it wasn't taking it. See my last, I have it figured out.
  6. Look at the URL in your address bar. See the part at the beginning that says file://? That means you are trying to view the file like a regular file. You need to view it as a webpage. As in through XAMPP with a URL that starts with http://.
  7. Finally! I switched one field to timestamp and generate on insert. I also switched the lastrain date to a string since it comes in as a string. It is inserting every time the page is hit. So getting closer.
  8. Look at the query. STR_TO_DATE('2022-01-24T20:52:00.000Z' , '%c-%e-%Y %T') What will that try to do?
  9. Hello everyone, good night. I'm making a database for ecommerce called "ecommerce". But when I was connecting the database (msqli_connect.php) with the add_artist.php file, I get errors. My question is: How can I fix them? I greatly appreciate your help. Ps.: The source code and the image are attached. add_artist.php <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Add an Artist</title> <meta charset="UTF-8"> <meta name="description" content="Home page"> <meta name="keywords" content="ecommerce"> <meta name="author" content="José Moreira"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <?php # Script 19.1 - add_artist.php // This page allows the administrator to add an artist. if ($_SERVER['REQUEST_METHOD'] = = 'POST') { // Handle the form. // Validate the first and middle names (neither required): $fn = (!empty($_POST['first_name'])) ? trim($_POST['first_name']) : NULL; $mn = (!empty($_POST['middle_name'])) ? trim($_POST['middle_name']) : NULL; // Check for a last_name... if (!empty($_POST['last_name'])) { $ln = trim($_POST['last_name']); // Add the artist to the database: require ('../../mysqli_connect. php'); $q = 'INSERT INTO artists (first_ name, middle_name, last_name) VALUES (?, ?, ?)'; $stmt = mysqli_prepare($dbc, $q); mysqli_stmt_bind_param($stmt, 'sss', $fn, $mn, $ln); mysqli_stmt_execute($stmt); // Check the results.... if (mysqli_stmt_affected_ rows($stmt) = = 1) { echo '<p>The artist has been added.</p>'; $_POST = array( ); } else { // Error! $error = 'The new artist could not be added to the database!'; } // Close this prepared statement: mysqli_stmt_close($stmt); mysqli_close($dbc); // Close the database connection. } else { // No last name value. $error = 'Please enter the artist\'s name!'; } } // End of the submission IF. // Check for an error and print it: if (isset($error)) { echo '<h1>Error!</h1> <p style="font-weight: bold; color: #C00">' . $error . ' Please try again.</p>'; } // Display the form... ?> <h1>Add a Print</h1> <form action="add_artist.php" method="post"> <fieldset><legend>Fill out the form to add an artist:</legend> <p><b>First Name:</b> <input type="text" name="first_name" size="10" maxlength="20" value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p> <p><b>Middle Name:</b> <input type="text" name="middle_name" size="10" maxlength="20" value="<?php if (isset($_POST['middle_name'])) echo $_POST['middle_name']; ?>" /></p> <p><b>Last Name:</b> <input type="text" name="last_name" size="10" maxlength="40" value="<?php if (isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /></p> </fieldset> <div align="center"><input type="submit" name="submit" value="Submit" /></div> </form> </body> </html> msqli_connect.php <?php # Script 9.2 - mysqli_connect.php // This file contains the database access information. // This file also establishes a connection to MySQL, // selects the database, and sets the encoding. // Set the database access information as constants: DEFINE ('DB_USER', 'username'); DEFINE ('DB_PASSWORD', 'Quencyjones79'); DEFINE ('DB_HOST', 'localhost'); DEFINE ('DB_NAME', 'ecommerce'); // Make the connection: $dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error( ) ); // Set the encoding... mysqli_set_charset($dbc, 'utf8');
  10. Error: INSERT INTO weather_data (datetime, temp, feelslike, stationbarometer, sealevelbarometer, dewpoint, humidity, winddir, windspeed, gust, maxdailygust, hourlyrainrate, dailyrain, weeklyrain, monthlyrain, totalrain, lastrain, solar, battout) VALUES ( STR_TO_DATE('2022-01-24T20:52:00.000Z' , '%c-%e-%Y %T'), 49.1, 49.1, 29.959, 29.135, 47.16, 93, 274, 0.7, 1.1, 8.1, 0, 0.181, 0.181, 0.701, 49.043, STR_TO_DATE('2022-01-24T19:22:00.000Z', '%c-%e-%Y %T'), 57.62, 1 ) Incorrect datetime value: '2022-01-24T20:52:00.000Z' for function str_to_date WTF is up with dates in mysql? I haven't worked in it since version 5.* but I never had the problems I am having trying to insert. Any help would be greatly appreciated.
  11. Looks good. Well Done! Update: Except for the check on the function's incoming object. You check if it is a dir yet your mainstream logic doesn't call rrmdir if it isnt a dir.
  12. <?php function rrmdir($dir) { if (is_dir($dir)) { $objects = scandir($dir); foreach ($objects as $object) { if ($object != "." && $object != "..") { if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object); } } reset($objects); rmdir($dir); } } $filesToKeep = array( 'tokenset.php', 'index.php', 'done.php', 'count.php', 'tokenmaster', 'start' ); $dirList = glob('*'); foreach ($dirList as $file) { if (! in_array($file, $filesToKeep)) { if (is_dir($file)) { rrmdir($file); } else { unlink($file); }//END IF }//END IF }//END FOREACH LOOP ?> Thought I might show the solution that was used....
  13. At the same time look into using PDO instead of MYSQLI. Much easier to use. Here is a brief example: $pdo = (connection statement); // do this ONCE before beginning db operations $q = 'select col1, col2, col3 from mytable where col1 = :arg1'; $qst = $pdo->prepare($q); // define the parms $parms = array( ':arg1'=>$val1 ); if (!$qst->execute($parms)) { (handle an error condition) } else { while($row = $qst->fetch()) { (handle each row of the results) } } There is very good info on setting up the connection logic in the manual as well as all of the available functions but what I have showed you is enough to do most exercises.
  14. Doh! Thank you and to all others who responded. I will look into parameterized queries.
  15. No it doesn't. Referencing field names that aren't in the table produces that error.
  16. Quoting both datetime fields produces this error. Unknown column 'tempf' in 'field list'
  17. Looks like you have a second datetime later on in the query (lastRain). You need to quote both.
  18. Error: INSERT INTO weather_data (datetime, tempf, feelslike, stationbarometer, sealevelbarometer, dewpoint, humidity, winddir, windspeed, gust, maxdailygust, hourlyrainrate, dailyrain, weeklyrain, monthlyrain, totalrain, lastrain, solar, battout) VALUES ( '1643040660', 48, 48, 30.053, 29.23, 45.79, 92, 299, 1.1, 1.1, 8.1, 0, 0.161, 0.161, 0.681, 49.024, 2022-01-24T15:34:00.000Z, 43.8, 1 ) 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 ':34:00.000Z, 43.8, 1 )' at line 22 This is the error converting string to datetime with strtotime.
  19. Error: INSERT INTO weather_data (datetime, tempf, feelslike, stationbarometer, sealevelbarometer, dewpoint, humidity, winddir, windspeed, gust, maxdailygust, hourlyrainrate, dailyrain, weeklyrain, monthlyrain, totalrain, lastrain, solar, battout) VALUES ( '2022-01-24T16:08:00.000Z', 48, 48, 30.056, 29.232, 45.79, 92, 331, 1.8, 2.2, 8.1, 0, 0.161, 0.161, 0.681, 49.024, 2022-01-24T15:34:00.000Z, 47.22, 1 ) 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 ':34:00.000Z, 47.22, 1 )' at line 22 This is what I get putting quotes around the DateTime.
  20. (Being a Character Representations of their actual Data Type) Datetime literals must be enclosed in single quotes, in exactly the same way as you do for Character literals. The error message is definitive. Error: INSERT INTO weather_data ... VALUES ( 2022-01-24T15:40:00.000Z, 47.7, 47.7, 30.048, 29.224, 45.49, 92, 314, 1.3, 2.2, 8.1, 0, 0.161, 0.161, 0.681, 49.024, 2022-01-24T15:34:00.000Z, 29.48, 1 ) \_________/ 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 ':40:00.000Z, 47.7, 47.7, 30.048, 29.224, 45.49, 92, 314, ' at line 6 That said, you should not be embedding variables directly into SQL - you are leaving yourself wide open to SQL Injection Attacked. Use a Parameterised Query (Prepared Statement) instead. Obligatory XKCD Reference - Little Bobby Tables. Regards. Phill W.
  21. I don't see the attempt to convert string to date. The only uses of strtotime have been commented out so I don't see at all how you even get a value for it in your query which is another question.
  22. A date time value is a string and therefore needs to be in quotes VALUES ( '$_DateTime', $_tempf, ... Better still, use prepared queries to protect from SQL injection attacks. Don't put variables in the SQL query string.
  23. Any non-Indian Users that favour keyboard navigation are still going to be very frustrated by this design. As soon as they try to move "down" the list with the arrow keys, the Javascript code will submit the page, including the first option ("country=india"). This reloads the page and, sadly, your code doesn't cater for the list "remembering" which option was chosen previously and just resets itself to the first, "--select--" option. Pressing the down key again just goes round the same loop over and over again (and hits your PHP backend each and every time). Do you really need such real-time handling of a change in country? Regards, Phill W.
  24. For the life of me I can not figure out the syntax error. I was thinking it was related to DateTime field. The data comes over as a string from the api, but converting to time doesn't work. The field in the database is DateTime. Any suggestions would be appreciated. The data from the api for reference. array(1) { [0]=> array(3) { ["macAddress"]=> string(17) "FA:F5:C2:98:6F:29" ["lastData"]=> array(27) { ["dateutc"]=> int(1642622880000) ["tempinf"]=> float(77.9) ["humidityin"]=> int(38) ["baromrelin"]=> float(29.956) ["baromabsin"]=> float(29.132) ["tempf"]=> float(77.2) ["battout"]=> int(1) ["humidity"]=> int(47) ["winddir"]=> int(247) ["windspeedmph"]=> float(5.6) ["windgustmph"]=> float(8.1) ["maxdailygust"]=> float(15.9) ["hourlyrainin"]=> int(0) ["eventrainin"]=> int(0) ["dailyrainin"]=> int(0) ["weeklyrainin"]=> int(0) ["monthlyrainin"]=> float(0.52) ["totalrainin"]=> float(48.862) ["solarradiation"]=> float(196.47) ["uv"]=> int(1) ["feelsLike"]=> float(76.83) ["dewPoint"]=> float(55.39) ["feelsLikein"]=> float(77.2) ["dewPointin"]=> float(50.2) ["lastRain"]=> string(24) "2022-01-12T02:50:00.000Z" ["tz"]=> string(15) "America/Chicago" ["date"]=> string(24) "2022-01-19T20:08:00.000Z" } ["info"]=> array(2) { ["name"]=> string(18) "My Weather Station" ["coords"]=> array(5) { ["coords"]=> array(2) { ["lon"]=> float(-197.65635809999999) ["lat"]=> float(38.6587316) } ["address"]=> string(44) "100 Main Street, Anytown, FL 08226, USA" ["location"]=> string(10) "Anytown" ["elevation"]=> float(214.7066497802734) ["geo"]=> array(2) { ["type"]=> string(5) "Point" ["coordinates"]=> array(2) { [0]=> float(-97.65635809999999) [1]=> float(30.6587316) } } } } } } I take the data from the array and assign it to variables. Notice the unsuccessful attempts to convert string to DateTime. <?php $_DateTime = $data[0]['lastData']['date']; //$_OldDateTime = $data[0]['lastData']['date']; //$_dateTime = strtotime($_OldDateTime); $_tempf = $data[0]['lastData']['tempf']; $_feelsLike = $data[0]['lastData']['feelsLike']; $_stationbarometer = $data[0]['lastData']['baromrelin']; $_sealevelbarometer = $data[0]['lastData']['baromabsin']; $_dewpoint = $data[0]['lastData']['dewPoint']; $_humidity = $data[0]['lastData']['humidity']; $_winddir = $data[0]['lastData']['winddir']; $_windspeed = $data[0]['lastData']['windspeedmph']; $_gust = $data[0]['lastData']['windgustmph']; $_maxdailygust = $data[0]['lastData']['maxdailygust']; $_hourlyrainrate = $data[0]['lastData']['hourlyrainin']; $_dailyrain = $data[0]['lastData']['dailyrainin']; $_weeklyrain = $data[0]['lastData']['weeklyrainin']; $_monthlyrain = $data[0]['lastData']['monthlyrainin']; $_totalrain = $data[0]['lastData']['totalrainin']; $_lastRain = $data[0]['lastData']['lastRain']; //$_OldlastRain = $data[0]['lastData']['lastRain']; //$_lastRain = strtotime($_OldlastRain); $_solar = $data[0]['lastData']['solarradiation']; $_battout = $data[0]['lastData']['battout']; // this is where we insert into the database $sql = "INSERT INTO weather_data (datetime, tempf, feelslike, stationbarometer, sealevelbarometer, dewpoint, humidity, winddir, windspeed, gust, maxdailygust, hourlyrainrate, dailyrain, weeklyrain, monthlyrain, totalrain, lastrain, solar, battout) VALUES ( $_DateTime, $_tempf, $_feelsLike, $_stationbarometer, $_sealevelbarometer, $_dewpoint, $_humidity, $_winddir, $_windspeed, $_gust, $_maxdailygust, $_hourlyrainrate, $_dailyrain, $_weeklyrain, $_monthlyrain, $_totalrain, $_lastRain, $_solar, $_battout )"; //mysqli_query($conn, $sql); if (mysqli_query($conn, $sql)) { echo "Success!"; //json_encode(array("statusCode"=>200)); } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); } mysqli_close($conn); ?> Error message: Error: INSERT INTO weather_data (datetime, tempf, feelslike, stationbarometer, sealevelbarometer, dewpoint, humidity, winddir, windspeed, gust, maxdailygust, hourlyrainrate, dailyrain, weeklyrain, monthlyrain, totalrain, lastrain, solar, battout) VALUES ( 2022-01-24T15:40:00.000Z, 47.7, 47.7, 30.048, 29.224, 45.49, 92, 314, 1.3, 2.2, 8.1, 0, 0.161, 0.161, 0.681, 49.024, 2022-01-24T15:34:00.000Z, 29.48, 1 ) 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 ':40:00.000Z, 47.7, 47.7, 30.048, 29.224, 45.49, 92, 314, ' at line 6
  25. Programming is not like cooking, where you can throw a bunch of things into a pot and get a delicious gumbo. Programming is like painting: the canvas may vary and what you're trying to draw will be different each day, but you need to work with the paints in a certain way, and if you try to throw them together then all you're going to be able to paint is "Brown Rock In Puddle of Mud". If you want red then you use red, and if you want yellow then you use yellow, and if you want some shade of orange then you mix red and yellow together in very precise, calculated, and controlled proportions. You've got a lot of things going on here and only a small fraction of them are going to be appropriate, let alone useful, to your end goal. It's complicated enough that I'm not even entirely sure what your end goal is supposed to be (other than "you want a red dot"). So let's start at the beginning - or perhaps a little bit on this side of it. You have a database, but let's pretend you already got what you need from it and now have that $b64 variable. In as technical terms as you know, what do you need to do with it? Do you need to output an <img> into your HTML that will show this image? Or do you already have the <img> and you need a script that you can reference to show the image? And I'll skip ahead a bit and ask: in the general case, are these images going to be small and simple, or potentially large and detailed? Judging by the fact that you're talking about JPEGs that are 5"x5", I'm thinking the latter. We'll come back to that later.
  26. I have tried several things to get this working. I am getting a black screen with a white dot. Not sure what I'm doing wrong. Apparently it should be a red dot. I want to get this working first as I have BASE64 to display from a table in the database.By teh way I am fairly new to PHP so please go easy! <?php // Define the Base64 value you need to save as an image header ('Content-Type: image/png'); $b64 = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="; // Obtain the original content (usually binary data) $bin = base64_decode($b64); // Load GD resource from binary data $im = imageCreateFromString($bin); //echo $im; // Make sure that the GD library was able to load the image // This is important, because you should not miss corrupted or unsupported images if (!$im) { die('Base64 value is not a valid image'); } // Specify the location where you want to save the image //; echo '<img src="data:image;base64, ' .$im. ' " style="width:5in;height:5in;">'; //echo '<img src="data:image/jpeg;base64,data:image/jpeg;base64,$b64>")'; //echo '<img src= ' .$bin. '/>'; //print("<img src= $im 'height: 200px; width: 350px;'>"); //$img_file = '/img/filename.png'; // Save the GD resource as PNG in the best possible quality (no compression) // This will strip any metadata or invalid contents (including, the PHP backdoor) // To block any possible exploits, consider increasing the compression level //imagepng($im, $img_file, 0); ?>
  27. Last week
  28. Don't get your very last comment. Inputs or textarea - the alignment is up to you in either case. You are using tables so alignment should not be an issue.
  1. Load more activity
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.