-
Posts
24,602 -
Joined
-
Last visited
-
Days Won
830
Everything posted by Barand
-
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.
-
You can do with a single INSERT SELECT statement. INSERT INTO Images (name, person_id, filename, description, medium) SELECT name , table_id , image_name , description , medium FROM tbl_person_data; Doing it with a prepared statement, as you are, requires either execute with a numerically indexed array and ? as placeholders, or execute with an associative array and named placeholders matching the array keys
-
Constraint on Child-table based on value in Parent-table
Barand replied to SaranacLake's topic in MySQL Help
If you are so error-prone, use your form which maintains those answer_group/answer tables to validate. If you create a group for 5, only show 5 answer rows. If editing and you accidentally have 6 answers, display the sixth in say, red, to flag the error. -
Group Timestamps in array into unique instances and count them via php
Barand replied to Thomas_L's topic in PHP Coding Help
try foreach ($array as $k => $d) { if ($k > 0) { if (strtotime($d) > strtotime($array[$k-1])+6) { $new[] = "-------------------"; } } $new[] = $d; } $new = Array ( [0] => 2021-02-10 09:04:48 [1] => 2021-02-10 09:04:54 [2] => 2021-02-10 09:05:00 [3] => 2021-02-10 09:05:06 [4] => 2021-02-10 09:05:12 [5] => 2021-02-10 09:05:18 [6] => ------------------- [7] => 2021-02-10 09:06:18 [8] => 2021-02-10 09:06:24 ) [edit...] Alternative solution... $new = []; $newkey = 0; foreach ($array as $k => $d) { if ($k > 0) { if (strtotime($d) > strtotime($array[$k-1])+6) { $newkey++; } } $new[$newkey][] = $d; } gives $new = Array ( [0] => Array ( [0] => 2021-02-10 09:04:48 [1] => 2021-02-10 09:04:54 [2] => 2021-02-10 09:05:00 [3] => 2021-02-10 09:05:06 [4] => 2021-02-10 09:05:12 [5] => 2021-02-10 09:05:18 ) [1] => Array ( [0] => 2021-02-10 09:06:18 [1] => 2021-02-10 09:06:24 ) ) -
Adding elements from one multidimensional array to another
Barand replied to kenoli's topic in PHP Coding Help
In PHP, string indexes for arrays should be quoted. I am talking about the HTML in your form. You are naming the input field as "['title']" instead of "[title]" -
Adding elements from one multidimensional array to another
Barand replied to kenoli's topic in PHP Coding Help
name="1['title']" ^ ^ remove those single quotes in all inout names -
Constraint on Child-table based on value in Parent-table
Barand replied to SaranacLake's topic in MySQL Help
I wouldn't bother. It's not as though your users will be creating these "answer" records. It's completely under your control and testing will show any "accidents". And what if it really is a 5-answer group but you accidentally enter "4" or "6" in the group table? It's equivalent to storing a derived total instead of getting the total from the data. Let the number of answers dictate the group size. -
Adding elements from one multidimensional array to another
Barand replied to kenoli's topic in PHP Coding Help
print_r() doesn't normally put quotes around keys, just [..]. EG Array ( [fname] => joe [lname] => bloggs ) If you have them, your form must be adding them to the input field names. Show your form code. -
Adding elements from one multidimensional array to another
Barand replied to kenoli's topic in PHP Coding Help
Are you sure it isn't referring to line 64 in a different file? It's not like the error reports to quote an empty line number. -
Define customer/product as UNIQUE... CREATE TABLE `cart` ( `cart_id` int(11) NOT NULL AUTO_INCREMENT, `customerID` int(11) DEFAULT NULL, `productID` varchar(5) DEFAULT NULL, `quantity` int(11) DEFAULT NULL, PRIMARY KEY (`cart_id`), UNIQUE KEY `UNQ_cart_product` (`customerID`,`productID`) ) BEFORE +---------+------------+-----------+----------+ | cart_id | customerID | productID | quantity | +---------+------------+-----------+----------+ | 1 | 1 | ABC12 | 3 | | 2 | 1 | DEF23 | 5 | +---------+------------+-----------+----------+ UPDATE INSERT INTO cart (customerID, productID, quantity) VALUES (1, 'ABC12', 2), (1, 'DEF23', 5), (1, 'GHI34', 10) ON DUPLICATE KEY UPDATE quantity = quantity + VALUES(quantity); AFTER +---------+------------+-----------+----------+ | cart_id | customerID | productID | quantity | +---------+------------+-----------+----------+ | 1 | 1 | ABC12 | 5 | | 2 | 1 | DEF23 | 10 | | 3 | 1 | GHI34 | 10 | +---------+------------+-----------+----------+
- 1 reply
-
- 1
-
-
Connecting to database showing error many time?
Barand replied to williamnoah's topic in PHP Coding Help
This the content of my include file ("db_inc.php") <?php define("HOST",'localhost'); define("USERNAME",'????'); define("PASSWORD",'????'); define("DATABASE", 'test'); // default database name function pdoConnect($dbname=DATABASE) { $db = new PDO("mysql:host=".HOST.";dbname=$dbname;charset=utf8",USERNAME,PASSWORD); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); return $db; } function myConnect($dbname=DATABASE) { mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); $db = mysqli_connect(HOST,USERNAME,PASSWORD,$dbname); $db->set_charset('utf8'); return $db; } To use include 'db_inc.php'; $conn = pdoConnect(); // pdo connection to default database or, for example, to connect a different database using mysqli $conn = myConnect('sakila'); I recommend using PDO. -
TIP: If you are creating home-grown charts, plotting the values is the easy bit. 95% of the coding effort will be in the drawing of chart area, plot area, axes, axis labels, scaling, titles etc. You can sidestep this with a simple table with horizontal bars. EG CODE EXAMPLE... <?php $values = [ 'Strongly Disagree' => 7, 'Disagree' => 10, 'Neither' => 12, 'Agree' => 25, 'Strongly Agree' => 41 ]; function valueChart(&$values) { $out = "<table class='chartTable'> <tr><th>Response</th> <th>Total</th> <th>Percent</th> </tr> "; $totalVal = array_sum($values); foreach ($values as $resp => $n) { $out .= "<tr><td>$resp</td> <td class='ra'>$n</td> <td>" . bar($n / $totalVal * 100) . "</td></tr>\n"; } $out .= "</table\n"; return $out; } function bar($val=0) { $a = '#3399ff'; $b = '#e6f2ff'; $c = '#0066cc'; $bg = '#eee'; $width = 300; $height = 25; $svg = <<<SVG <svg width='$width' height='$height' viewBox='0 0 $width $height'>"; <defs> <linearGradient id="bargrad" x1="0" y1="0" x2="0" y2="1"> <stop offset="0%" style="stop-color:$a"/> <stop offset="25%" style="stop-color:$b"/> <stop offset="100%" style="stop-color:$c"/> </linearGradient> </defs> <rect x='0' y='0' width='$width' height='$height' style='fill:$bg' stroke='#999'/> SVG; $w = $val/100 * $width; $svg .= "<rect x='0' y='0' width='$w' height='$height' style='fill:url(#bargrad)' />"; $svg .= "</svg>\n"; return $svg; } ?> <!DOCTYPE html> <html lang="en"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Chart Example</title> <head> <style type='text/css'> .chartTable { font-family: arial, sans-serif; font-size: 11pt; } th { padding: 4px 16px ; } td { padding: 0 16px; } .ra { text-align: right; } </style> </head> <body> <?=valueChart($values)?> </body> </html> Hope this helps.
-
Adding elements from one multidimensional array to another
Barand replied to kenoli's topic in PHP Coding Help
Have you got PHP error reporting turned on and have you set your PDO options to report exceptions? -
Adding elements from one multidimensional array to another
Barand replied to kenoli's topic in PHP Coding Help
Why don't you RTFM to see what $$ does? try $stmt = $conn->prepare("INSERT INTO mytable (title, medium, person_id, name) VALUES (:title, :medium, :person_id, :name) "); foreach ($titles as $title_elements) { if ($title_elements['title'] != '') { $stmt->execute($title_elements); } }