-
Posts
24,572 -
Joined
-
Last visited
-
Days Won
824
Everything posted by Barand
-
When something is "unexpected" the cause is usually whatever came before it. But you haven't shown us that.
-
Welcome - enjoy your swim.
-
Yes, don't use select * ... Also, there is no need to close a connection at the end of a page - php does this automatically for you.
-
the id is unique, so you can't insert a second id of 26. Omit the id column from the query then the rest of the columns can be inserted (unless any are defined UNIQUE in which case you need to omit those too) $sql = "INSERT INTO $table (company_name) SELECT company_name FROM $table WHERE id = 26"; You can call a lastInsertId() function to get the id of the new record.
-
Php and Laravel prevent duplicate entries help please
Barand replied to PNewCode's topic in PHP Coding Help
The basic code is /* Current user data +----+----------+--------------+ | id | username | fullname | +----+----------+--------------+ | 1 | lucy | Lucy Lastik | | 2 | hugh | Hugh Jass | | 3 | tom | Tom DiCanari | +----+----------+--------------+ */ try { $stmt = $pdo->prepare("INSERT INTO user (username, fullname) VALUES (?, ?) "); $stmt->execute( [ 'lucy', 'Lucy Smith' ] ); // attempt duplicate insert } catch (PDOException $e) { if ($stmt->errorInfo()[1] == 1062) { // duplicate key error code $_SESSION['errors']['username'] = 'username already exists'; // error message to display to user when form redisplayed } else throw $e; // let php handle the error } Just convert it to Laravelese. -
Php and Laravel prevent duplicate entries help please
Barand replied to PNewCode's topic in PHP Coding Help
The best way is to define the username as a unique key in your user table. That way you can't add another with the same name (an exception with be thrown if you attempt to add a second). Then check for duplicate key errors when adding users in you code. -
I cannot comment as I have absolutely no idea what your data looks like Then do that... $dt = new DateTime($match['matchDateTime']);
-
php update mysql database with form/checkbox
Barand replied to tonypoli783's topic in PHP Coding Help
Telling us that some thing isn't working tells us nothing. What should happen that isn't? What shouldn't happen that is? Your topic title mentions a database update. Where and what is that code? NOTE - use the code button (<>) when posting code. -
A. echo '<pre>' . print_r($match, true) . '</pre>'; B. $dt = new DateTime('2024-10-06T09:19:53.660Z'); echo $dt->format('D, d.m - h:i'); // Sun, 06.10 - 09:19
-
Contact the printer manufacturer, they may aleady have android software and cable available for this purpose.
-
how to calculate a certain number of times a fraction?
Barand replied to NikitosKnyaz's topic in PHP Coding Help
I'm guessing the assignment deadline has passed, so for the sake of others reading the thread, here's one solution... <?php $a = 1; $b = 2; $c = []; $d = []; $N = 8; $tdata1 = $tdata2 = ""; $vals = []; for ($i=1, $a=1, $b=2; $i<=$N; $i++, $a+=2, $b+=2) { $c[] = $a; $d[] = $b; $exp = '<u>' . join('.', $c) . "</u><br>" . join('.', $d); $tdata1 .= "<td>$exp</td>"; $val = number_format(array_product($c) / array_product($d), 5) ; $tdata2 .= "<td>$val</td>"; } ?> <table border='1'> <tr><th>Fraction</th> <?=$tdata1?> </tr> <tr><th>Decimal</th> <?=$tdata2?> </tr> </table> -
<!DOCTYPE html> <html lang="en"> <head> <title>4 Queens</title> <meta charset="utf-8"> <style type='text/css'> table { border-collapse: collapse; } td { width: 40px; height: 40px; text-align: center; font-size: 16pt; } tr:nth-child(odd) td:nth-child(even), tr:nth-child(even) td:nth-child(odd) { background-color: lightgreen; } </style> </head> <body> <?php echo '<table border="1">'; for ($i = 1; $i <= 4; $i++) { echo "<tr>"; for ($j = 1; $j <= 4; $j++) { echo '<td>' . pow($i,2) + pow($j,2) . '</td>'; } echo '</tr>'; } echo "</table>\n"; ?> </body> </html> Dear Supreme Being, Can you put aside your arrogance for a moment and assume that we are 5 year olds (not a wall) and explain the theory behind your Pythagorean solution to the problem and why it "works" explain how the above square then tells us where the four queens should be positioned? I've found the 5s but there are no "opposite 10s", only 20s opposite the 5s, so the description is somewhat vague.. Having found the 5s, 10s, 20s and 25s (why those values in particular?), I now have 8 squares. Given that these are the candidates, the code then needs to find which 4 of these 8 fit the bill.
-
INSERT INTO user (fname,sex) values (inname, NULLIF(insex, '') );
-
In your php.ini file, ensure "display_startup_errors" is also set to On
-
Why the Freckle didn't you post the code that actually used to get the results you are complaining about. Once I got the data loaded, your query wouldn't even run without corrections to column names. Anyway - the answer to your question... They are in the wrong order because you order by your generated qNo column. I'd give up on that method. If you are using MariaDB, you can ORDER BY NATURAL_SORT_KEY(Q_id) If MySQL (which doesn't have that function), use FetchAll() to get an array of your results then natsort($results) use a custom sort which does a strnatcmp() on the Q_id column $res = $pdo->query(" ... "); $result = $res->FetchAll(); usort($results, fn($a,$b)=>strnatcmp($a['Q_id'], $b['Q_id'])); (Using sort($results) would have sorted using the values of the first column in each row - I assumed natsort() would do the same (silly me) )
-
FYI - it helps if the column defined as the primary key exists in the table! Same goes for the unique key in user table. Plus other syntax errors - I was trying to help but I gave up.
-
Your main problem is that mysql_*** functions have been deprecated for years and now no longer exist (unless you are using a really ancient version of PHP, in which case you should upgrade) You need to use mysqli or PDO (PDO is definitely recommended).
-
how to calculate a certain number of times a fraction?
Barand replied to NikitosKnyaz's topic in PHP Coding Help
I gained four inches just by giving up rugby, squash and badminton. -
how to calculate a certain number of times a fraction?
Barand replied to NikitosKnyaz's topic in PHP Coding Help
If it's your homework assignment, yes. If it isn't, what have you tried so far? -
how to calculate a certain number of times a fraction?
Barand replied to NikitosKnyaz's topic in PHP Coding Help
-
If you want to add another key to the results array add it to the query, and use @mac_gyver method.
-
... or ... $res = $pdo->query("SELECT Lvl , Sub FROM shadd_1 ORDER BY Lvl DESC, Sub "); $results = $res->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_COLUMN); foreach ($results as $lvl => $arr) { echo "$lvl <UL>"; foreach ($arr as $sub) { echo "<li>$sub</li>"; } echo "</ul>"; } FYI - the $results array (slightly simpler than the $data array in above post) looks like this.. Array ( [ULE] => Array ( [0] => BASIC lug [1] => BASIC lug ) [ACE] => Array ( [0] => ger ) )
-
And what are you wanting to do with that table? BTW, which values belong to which columns (do you have a problem with your keyboard's spacebar? If so you could have used the "table" button in the toolbar) What code have you tried so far?