-
Posts
24,566 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
The SQL stays the same for PDO as it was for mysqli. Only the PHP code changes. However I recommend you use explict JOIN .. ON syntax and not the implicit FROM a, b WHERE synatx. If $db is a PDO connection, then ... $q = "SELECT SUM(quantity) AS total FROM orders INNER JOIN order_details ON orders.order_id = order_details.order_id WHERE equip_ID = ? AND start_date > ? "; $stmt = $db->prepare($q); $stmt->execute( [ $item['id'] , $start_date ] ); // pass the values here, not in the query $total = $stmt->fetchColumn(); Your use of $item['id'] suggests that you might be running this query when you are looping through the results of another query. If so, don't. Use one query with JOINs.
-
Column names should not be in quotes. [edit...] While I'm here, don't put variables into your query strings, use prepared statements, and switch to PDO - it's far superior to mysqli.
-
The purpose of a column defined as TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP is to record the datetime a record was added to the table. If that is its purpose you shouldn't be inserting data into that column in your insert query. If its purpose is other than that, such as recording a tournament date, then it should be redefined (probably as DATETIME or DATE).
-
If you want latest first, reverse the array $counter_name = 'counter.txt'; file_put_contents($counter_name, date('Y-m-d H:i:s')."\n", FILE_APPEND); // creates file if it doesn't exist // appends if it does exist $k = 0; $clicks = file($counter_name, FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES); $clicks = array_reverse($clicks); foreach ($clicks as $dt) { ++$k; echo "$k : $dt<br>"; } giving 1 : 2021-03-02 13:27:25 2 : 2021-03-02 13:26:23 3 : 2021-03-02 13:24:19 4 : 2021-03-01 18:28:04 5 : 2021-03-01 18:25:28 6 : 2021-03-01 18:25:23 7 : 2021-03-01 18:25:17
-
Append the current datetime each time instead of the count. The number of records will give you the count. $counter_name = 'counter.txt'; file_put_contents($counter_name, date('Y-m-d H:i:s')."\n", FILE_APPEND); $k = 0; foreach (file($counter_name, FILE_IGNORE_NEW_LINES) as $dt) { ++$k; echo "$k : $dt<br>"; }
-
Here's an example using a cut-down version of your product table ... +--------------+--------------+-------+-----------------------------+-------------------------------+------+------+ | product_code | product_name | price | img1 | img2 | img3 | img4 | +--------------+--------------+-------+-----------------------------+-------------------------------+------+------+ | A001 | Widget | 10.99 | images/products/file1.png | images/products/file2.jpg | NULL | NULL | | B002 | Gizmo | 3.49 | images/products/file3.jpg | NULL | NULL | NULL | | C003 | Wotsit | 56.25 | images/products/file4.jpg | images/products/file5.png | NULL | NULL | | D444 | Gizmo Mk II | 2.25 | images/products/file6.png | images/products/file7.jpg | NULL | NULL | +--------------+--------------+-------+-----------------------------+-------------------------------+------+------+ ... and a form which submits the images as an array. <?php $errors = []; if ($_SERVER['REQUEST_METHOD'] == 'POST') { echo '<pre>', print_r($_POST, 1), '</pre>'; echo '<pre>', print_r($_FILES, 1), '</pre>'; $post = array_map('trim', $_POST); foreach ($post as $fn=>$v) { if ($v == '') { $errors[] = "$fn cannot be empty"; } } foreach ($_FILES['img']['error'] as $k => $v) { if ($v != 0 && $v != 4) { $errors[] = "Error uploading image #$k"; } } foreach ($_FILES['img']['type'] as $k => $v) { if (!in_array($v, ['image/png', 'image/jpeg', 'image/gif'] ) && $_FILES['img']['error'][$k] != 4 ) { $errors[] = "Image #$k is not a valid image type"; } } if (!$errors) { $target_dir = "images/products"; $uploads = [ 'img1' => null, 'img2' => null, 'img3' => null, 'img4' => null ]; foreach ($_FILES['img']['name'] as $k => $filename) { if ($filename) { if (move_uploaded_file($_FILES['img']['tmp_name'][$k], $target_dir . '/' . $filename)) { $uploads["img$k"] = $target_dir . '/' . $filename; } } } $post = array_merge($post, $uploads); $stmt = $conn->prepare("INSERT INTO product (product_code, product_name, price, img1, img2, img3, img4) VALUES (?, ?, ?, ?, ?, ?, ?) "); $stmt->bind_param('ssdssss', ...array_values($post)); $stmt->execute(); header("Location: #"); exit; } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Example</title> </head> <body> <?php if ($errors) { echo "<div style='color:red'>" . join('<br>', $errors) . "</div>\n"; } ?> <form method="POST" enctype="multipart/form-data"> Product Code<br> <input type="text" name="product_code"> <br> Product Name<br> <input type="text" name="product_name"> <br> Price<br> <input type="text" name="price"> <br> <br> <fieldset> <legend>Product Images</legend> <input type="file" name="img[1]"> <input type="file" name="img[2]"> <input type="file" name="img[3]"> <input type="file" name="img[4]"> </fieldset> <br> <br> <input type="submit" value="Submit"> </form> </body> </html>
-
Your coding should be validate product data if (valid) write product record foreach uploaded image validate image if (valid) write image record endif endforeach endif Are you going to tell me that isn't easier?
-
Secondly, do not store multiple images names in the product record. Store them in a separate table, 1 row for each image uploaded. Then your problem with NULL values goes away - you don't insert a record if there is no image. I.E. normalize your data. +----------------+ +---------------------+ | product | | product_image | +----------------+ +---------------------+ | product_code |-------+ | product_image_id | | product_name | +--------<| product_code | | | category | | image | | filter | +---------------------+ | description | | specification | | price | +----------------+ Thirdly, use prepared statements instead of putting datavalues directly into the queries.
-
Upload your files as an array then use a loop to process them. That way you process them all with a single block of code intead of giving us 4 sets of code to wade through (TLDR). https://www.php.net/manual/en/features.file-upload.multiple.php
-
You need to show us what you've tried so far before we help you with your homework.
-
See
- 1 reply
-
- 1
-
Is there a way to convert Bitcoin to Satoshie and vice versa?
Barand replied to imgrooot's topic in PHP Coding Help
@Cor4Flez Let sleeping posts lie. This one is two years old. -
@Hendz I recommend you use PHP's password_hash() and password_verify() functions.
-
This should get you started (if you haven't already) ... $array = array ( 'data' => array ( '0' => array ( 'latitude' => 22.934566, 'longitude' => 79.08728, 'type' => 'county', 'distance' => 44.328, 'name' => 'Narsinghpur', 'number' => '', 'postal_code' => '', 'street' => '', 'confidence' => 0.5, 'region' => 'Madhya Pradesh', 'region_code' => 'MP', 'county' => 'Narsinghpur', 'locality' => '', 'administrative_area' => '', 'neighbourhood' => '', 'country' => 'India', 'country_code' => 'IND', 'continent' => 'Asia', 'label' => 'Narsinghpur, India' ) ) ); foreach ($array['data'] as $record) { // insert $record }
-
0 out of 10 for formatting. What have you tried so far? What was the outcome?
-
Alternaltive method... mysql> SELECT * FROM users; +--------+--------------+-----------+----------+ | userId | fullname | firstname | lastname | +--------+--------------+-----------+----------+ | 1 | Peter Dowt | NULL | NULL | | 2 | Laura Norder | NULL | NULL | | 3 | Tom DiCanari | NULL | NULL | | 4 | Scott Chegg | NULL | NULL | +--------+--------------+-----------+----------+ mysql> UPDATE users -> SET firstname = SUBSTRING_INDEX(fullname, ' ', 1), -> lastname = SUBSTRING_INDEX(fullname, ' ', -1); mysql> SELECT * FROM users; +--------+--------------+-----------+----------+ | userId | fullname | firstname | lastname | +--------+--------------+-----------+----------+ | 1 | Peter Dowt | Peter | Dowt | | 2 | Laura Norder | Laura | Norder | | 3 | Tom DiCanari | Tom | DiCanari | | 4 | Scott Chegg | Scott | Chegg | +--------+--------------+-----------+----------+
-
The missing quotes around $password aren't helping either, but when you do it correctly, using a prepared statement, then that problem will disappear.
-
You will actually need both the id and the name The label/for attributes use the id, POST variables use the name.
-
I gave you a one line PHP solution in my first post Your code works for me. It could be a conection problem. Check for mysql errors using mysqli_report() (EG this is my mysqli connection code)... mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); $db = mysqli_connect(HOST,USERNAME,PASSWORD,$database); $db->set_charset('utf8');
-
Do you charge per line of code, or are you of the school of programming that thinks "Why use one line of code when you can do it with several?" Anyway, here's a mysqli version $pop = 54321; $result = $donnees->query("SELECT DAYOFYEAR(CURDATE()) as dayno"); $row = $result->fetch_assoc(); echo round($pop / $row['dayno']); //--> 1006
-
The main point is the query. Use DAYOFYEAR to get the number of days from start of year, it's easier and doesn't produce a zero value. (and you can use that same query just as well with MySqli) Yours will fail (div by 0) if you run it on the 1st Jan. Also note the use of SQL is overkill when you can do what you want with a simple one liner in PHP.