Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 01/11/2020 in all areas

  1. i'm going with the code never copies the (unnecessary) $cart_items variable back to the session variable, so the code starts over each time it gets executed. just use $_SESSION["cart_items"] everywhere and forget about the $cart_items variable.
    1 point
  2. In your pagination loop you set all the links to the same page number! Should be $data = array( 'page' => $current_page, <-- CHANGED 'country' => $cleaned_current_page_country );
    1 point
  3. session_start will automatically determine if your starting a new session or resuming one that already exists, but this is not what your error is referring to. The error you're seeing relates to calling session_start multiple times within the same request. The function is only intended to be called once at the start of your request, which is not the same as at the start of every *.php file you have. If you want to have your separate pages that people request and not re-design your application to use a common front-end script or some framework then what you should do is have a common include file that handles your session (and other stuff as needed) and include that in each of your main entry pages using require_once. So your entry points should be coded something like: <?php require_once './includes/common.php'; //whatever else your script does And your common include would just be <?php session_start(); //whatever other common code you want This common include could contain things like your session management code, database connection code, function definitions, etc. Note: there's also a php.ini setting called session.auto_start. If you've enabled that then you don't need to manually call session_start().
    1 point
  4. I have just noticed you don't use the $page_url in those pagination links. It looks like you intended to but then just used the query string $the_query = http_build_query($data); $page_url .= '?' . $the_query; echo '<li><a href=" '.$the_query.' ">'.$current_page.'</a></li> '; As you go to the same page that should be sufficient but I think you will need the preceding "?" Try $the_query = http_build_query($data); $the_query = '?' . $the_query; echo "<li><a href=\"$the_query\">$current_page</a></li>";
    1 point
  5. the most immediate problem is you are not using the correct $_POST field and table column in the WHERE clause in the SELECT query (edit: i realized while writing this that the SELECT query you have shown is part of the update process, to retrieve a specific row of data to populate the form fields with, not part of a name search.) if you are entering the name (or partial name) of a game to search for, wouldn't you be searching the table column holding the game's name? why are you trying to match the id column? next, don't write code like this. there's a bunch of problems that have resulted in a wall of code that's both insecure and has created a 'cannot see the forest for the trees' problem (which is perhaps why you are using the wrong field/column in the WHERE clause.) a laundry list of issues - don't create a bunch of discrete variables for each different form you write code for. this is just a waste of typing time. instead, operated on the form data as a set, by keeping the data as an array, and operating on the elements in the array. by using exceptions for database statement errors, any connection, query, prepare, and execute error will transfer control to the exception handler. therefore, any discrete error handling logic in your code won't ever be executed and should be removed. related to #2, the only exception handling try/catch block you should have in your code are for those errors that are recoverable, that your code can do something about, such as dealing with the inserting/updating of duplicate or out of range user submitted data. all other database statement errors are non-recoverable and there's no good reason for your code to catch these exceptions. just let php catch and handle these exceptions. by using exceptions for errors and letting php catch them, php will use its error related settings to control what happens with the actual error information (database statement errors will 'automatically' get displayed or logged the same as php errors.) 'function getPosts()' - don't do this. you want code to be easy to write and debug and be readable by everyone. by intentionally using numerical indexes, you have made more work while writing this code and made more work for anyone trying to read/maintain the code. also, once you write and test the code for a function, you should not find yourself regularly editing the code. functions should not contain application specific code that you must change each time you do something new. external data can be anything and cannot be trusted. you must validate all external data before using it. you should use a prepared query when supplying external/unknown data to the sql query statement. items #6 and #7 will help avoid the current error, because you would not attempt to run a query if an expected input is empty and a numerical input that can be empty/null won't produce a query error. a name or partial name search can match more than one row (the same game name for more than one platform.) you would loop over the result from such a query and display as many rows of data that the query matched. to update/delete the data for a specific row, you would produce an edit link and a delete form for each row that is displayed. the edit link would contain the id of the row. when you click the edit link, the code would query for an fetch the data matching that row and populate the form field values. the current SELECT query seems to be for this part of the process, not for a name search. you need one more SELECT query.
    1 point
  6. One more attempt. Here's a script which puts the hints I gave you into practice. As you can see, it splits the ordered items into separate order depending on the SKU group SAMPLE: Code (read, understand what's going on and learn. Refer to php.net manual if you don't know what something does.) (Not an elseif()..elseif() to be found!) <?php const HOST = 'localhost'; # const USERNAME = '????'; # const PASSWORD = '????'; # const DATABASE = '????'; # These lines would # function pdoConnect($dbname=DATABASE) # normally be in { # $db = new PDO("mysql:host=".HOST.";dbname=$dbname;charset=utf8",USERNAME,PASSWORD); # an included file $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; # } # $pdo = pdoConnect(); // // CREATE TEST DATA USED BY THIS EXAMPLE // $pdo->exec("CREATE TABLE IF NOT EXISTS aveeva ( id int not null auto_increment primary key, order_no int, sku int, product varchar(50), qty int ) "); $pdo->exec("REPLACE INTO aveeva (id, order_no, sku, product, qty) VALUES (1,1020345, 12345, 'Pair of left-footed socks', 2), (2,1020345, 35547, 'Square Hula hoop', 1), (3,1020345, 12346, 'Pair of right-footed socks', 2), (4,1020345, 62347, 'Pair of three-legged tights', 5), (5,1020345, 45501, 'String vest', 1), (6,1020345, 45501, 'Thermal long johns (red)', 2), (7,1020345, 22105, 'Staffordshire pot dog', 2), (8,1020345, 38962, '250 Kg dumbell set', 1), (9,1020345, 23176, 'Ming vase', 1), (10,1020345, 23194, 'Porcelain elephant', 1), (11,1020345, 38547, '0.5 metre yoga mat', 1) "); // DUMMY CLASS TO PROVIDE THE METHODS YOU USED class Magee { private $fromName; private $body; private $subject; private $type; private $sendTo; public function __construct() { } public function setFromName($str) { $this->fromName = $str; } public function setBody($str) { $this->body = $str; } public function setSubject($str) { $this->subject = $str; } public function setType($str) { $this->type = $str; } public function setToEmail($str) { $this->sendTo = $str; } public function send() { // // Instead of sending emails // we just build content // for demonstration output // $out = "<fieldset><legend>TO: {$this->sendTo}</legend> <label>From</label>{$this->fromName}<br> <label>Subject</label>{$this->subject}<br> {$this->body}<br> "; $out .= "</fieldset>"; return $out; } } #Magee // define email addresses for the sku groups const DEFAULT_ADDY = 'other@gmail.com'; $addys = [ 2 => 'abc@gmail.com', 3 => 'xyz@gmail.com', 4 => 'qwe@gmail.com' ]; $res = $pdo->query("SELECT sku , product , qty , order_no FROM aveeva ORDER BY order_no, sku "); $orders = []; // // Split the items for each order // into groups depending the first // digit of their SKU // foreach ($res as $r) { $ono = array_pop($r); $sku1 = substr($r['sku'], 0, 1); if (!isset($addys[$sku1])) { $sku1 = 0; } $orders[$ono][$sku1][] = $r; } // // Build the email bodies from the array data // $emailsSent = ''; foreach ($orders as $ono => $odata) { foreach ($odata as $sku1 => $sdata) { $message = "<table border='1'> <caption>Order No: $ono</caption> <tr><th>SKU</th><th>Product</th><th>Quantity</th></tr>\n"; foreach ($sdata as $item) { $message .= "<tr><td>" . join("</td><td>", $item) . "</td></tr>\n"; } $message .= "</table>\n"; $emailsSent .= sendMailbasedOnSku($message, $addys, $sku1); } } function sendMailbasedOnSku($message, $addys, $sku1) { $emailTemplate = new Magee; $emailTemplate->setFromName('GIRI Test mail'); $emailTemplate->setBody($message); $emailTemplate->setSubject("Custom Email from observer"); $emailTemplate->setType('html'); // GET THE APPROPRIATE EMAIL ADDRESS $sendTo = $addys[$sku1] ?? DEFAULT_ADDY; // USE IT $emailTemplate->setToEmail($sendTo); return $emailTemplate->send(); } ?> <html> <head> <title>Example</title> <style type='text/css'> body { font-family: clibri, sans-serif; font-size: 11pt;} fieldset { width: 70%; margin: 16px auto; padding: 16px;} legend { background-color: black; color: white; padding: 4px;} label { display: inline-block; width: 120px; font-weight: 600;} table { width: 50%; margin: 16px auto; border-collapse: collapse;} th { background-color: black; color: white; padding: 8px;} td { padding: 4px 8px;} </style> </head> <body> <?=$emailsSent?> </body> </html>
    1 point
  7. Your problems are mainly because you are trying to write code before you have learnt to read. In my first post I said you needed 2 things. The first you have totally disregarded despite my spoonfeeding you the required code. The second addresses the problem you are now having and has also been ignored. Good luck and goodbye.
    1 point
  8. @mac_gyver gave the correct way to do it - http_build_query() EG $url = 'http://localhost/board/italian.php'; $data = [ 'page' => 2, 'country' => 'italian', 'comment' => 'this is a (simple) comment' ]; $qstr = http_build_query($data); $url .= '?' . $qstr; echo $url; //--> http://localhost/board/italian.php?page=2&country=italian&comment=this+is+a+%28simple%29+comment Note the encoding of the query string
    1 point
This leaderboard is set to New York/GMT-04:00
×
×
  • 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.