Jump to content

Barand

Moderators
  • Posts

    23,362
  • Joined

  • Last visited

  • Days Won

    693

Everything posted by Barand

  1. Are you sure that is your actual code?
  2. Slightly different approach then. Store the category query results in an array. $stmt = $pdo->query("SELECT categoryID , categoryName FROM tbl_categories WHERE categoryActive = 1 ORDER BY categoryID "); $cat_options = []; /// store cat data in array foreach ($statement as $row) { $cat_options[$row['categoryID']] = $row['categoryName']; } // main loop here while ($row = $stmt->fetch()) { echo "<select name='categoryID' class='select200px' required> <option value=''>Select...</option>"; foreach ($cat_options as $id => $cat) { $sel = $row['categoryID'] == $id ? 'selected' : ''; echo "<option $sel value='$id'>$cat</option>"; } echo "</select>"; }
  3. Like this $stmt = $pdo->query("SELECT categoryID , categoryName FROM tbl_categories WHERE categoryActive = 1 ORDER BY categoryID "); $cat_options = ''; foreach ($statement as $row) { $cat_options .= "<option value='{$row['categoryID']}'>{$row['categoryName']}</option>"; } // main loop here while (..) { echo "<select name='categoryID' class='select200px' required> <option value="">Select...</option> $cat_options </select>"; }
  4. No one is suggesting you hard code them, just that they should not be created inside the loop. Create them as you do now, but before the loop, and store the options to use inside the loop.
  5. Create the option lists before the loop $options = "<option value='1'>One</option> <option value='2'>Two</option> <option value='3'>Three</option>"; while ... { ... ... echo "<select ... > $options </select>"; ... }
  6. You create a statement object $stmt and loop through the results. While you are looping through these results you create another statement object $stmt thus overwriting the one you are currently processing. Running any queries inside a loop is a bad idea to start with, this takes it to another level.
  7. Not with my method, which sorts them numerically. Original order +-----------------------------------------------------------+ | title | +-----------------------------------------------------------+ | The Bionic Woman" (1976) {Jaime's Shield: Part 2 (#2.11)} | | The Bionic Woman" (1976) {Jaime's Mother (#1.8)} | | The Bionic Woman" (1976) {Rancho Outcast (#2.5)} | +-----------------------------------------------------------+ Sorted (...ORDER BY SUBSTRING_INDEX(title, '#', -1)+0) +-----------------------------------------------------------+ | title | +-----------------------------------------------------------+ | The Bionic Woman" (1976) {Jaime's Mother (#1.8)} | | The Bionic Woman" (1976) {Jaime's Shield: Part 2 (#2.11)} | | The Bionic Woman" (1976) {Rancho Outcast (#2.5)} | +-----------------------------------------------------------+
  8. The obvious question is "Why not store the #x.xx in a separate column?". But given where you are... SELECT title FROM movie; +-----------------------------------------------------------+ | title | +-----------------------------------------------------------+ | "The Bionic Woman" (1976) {Jaime's Shield: Part 2 (#2.11)} | | "The Bionic Woman" (1976) {Jaime's Mother (#1.8)} | | "The Bionic Woman" (1976) {Rancho Outcast (#3.21)} | +-----------------------------------------------------------+ SELECT title FROM movie ORDER BY SUBSTRING_INDEX(title, '#', -1)+0; +-----------------------------------------------------------+ | title | +-----------------------------------------------------------+ | "The Bionic Woman" (1976) {Jaime's Mother (#1.8)} | | "The Bionic Woman" (1976) {Jaime's Shield: Part 2 (#2.11)} | | "The Bionic Woman" (1976) {Rancho Outcast (#3.21)} | +-----------------------------------------------------------+
  9. A prepared query is the best way to accomplish that. MYSQLI $inputname = "O'Brien"; $res = $mysqli->prepare("SELECT whatever from table1 where lastname = ? "; $res->bind->param("s", $inputname); $res->execute(); PDO $inputname = "O'Brien";; $res = $pdo->prepare("SELECT whatever from table1 where lastname = ? "; $res->execute( [$inputname] );
  10. If you intend calculating a total of the voucher codes or using them in some other arithmetic operation then keep it as a number (BIGINT) but if it's just a code, then it's a string.
  11. Looks like you are trying to store a 19 digit number in an INT field with a maximum value of 2147483647. Your voucher code is actually a string that consists of numeric characters (like a phone number) so store it as one, say VARCHAR(20);
  12. This example ensures every student has a unique matriculation number CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `firstName` varchar(50) NOT NULL, `lastName` varchar(50) NOT NULL, `matricNo` varchar(50) NOT NULL, `DOB` date NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `idx_student_matricNo` (`matricNo`) ) ;
  13. Describe "not working". That on its own tells us nothing.
  14. There's an example here, courtesy of @Psycho
  15. Don't check if a user exisits by first running a query. Instead, you should put a UNIQUE constraint on the columns and trap duplicate key errors when they occur on inserting a record. Why a mix of mysqli and PDO. Just stick with PDO. Don't create a new connection for each query as you do in you signup_push function - that will really slow you down. Create the connection once and pass it in the function arguments. INSERT, UPDATE and DELETE queries do not create result sets. You are inserting then using fetchAll() to count non-existant results. PDO::row_count() will tell you how many were affected by the query.
  16. Why duplicate all the code when the only thing that needs to change is the name of the database in the query?
  17. Use array keys instead of array_search $results = array_keys(array_column($data, 'oname'), 'Border.aao');
  18. Example of form and writing its form data to two tables in different databases <?php $con = <your db connection code> ; if ($_SERVER['REQUEST_METHOD']=='POST') { $stmt1 = $con->prepare("INSERT INTO database1.mytable(col1, col2, col3) VALUES (?,?,?)"); // insert into table i database1 $stmt2 = $con->prepare("INSERT INTO database2.mytable(col1, col2, col3) VALUES (?,?,?)"); // insert into table i database2 $stmt1->execute([ $_POST['val1'],$_POST['val2'],$_POST['val3'] ]); $stmt2->execute([ $_POST['val1'],$_POST['val2'],$_POST['val3'] ]); } ?> <form method='post'> Value 1 <input name='val1'> Value 2 <input name='val2'> Value 3 <input name='val3'> <input type='submit>' </form>
  19. OK. But, again, why do you need two pages? Are both databases on the same server? (Even if they aren't it just means you need two connections)
  20. My query didn't involve any external variables as the date+7 is calculated in the query. I therefore used query() instead of prepare() - there is no execute() required with query(). Incidentally, your code just compares against now, now 7 days from now,
  21. Try $stmt = $pdo->query("SELECT * FROM care_plan_review WHERE reminder_date > CURDATE() + INTERVAL 7 DAY");
  22. Don't use a number. Each character in a number can be 1 of 10 choices. If you use a string of mixed uppercase, lowercase, numbers and puctuation each character can be 1 of 90 (approx) characters.
  23. The first reply in this thread does it without array iterator - read it beyond the first sentence and look at the output.
×
×
  • 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.