Jump to content

Barand

Moderators
  • Posts

    24,350
  • Joined

  • Last visited

  • Days Won

    796

Everything posted by Barand

  1. Several things could be improved. 1 ) Do not use "SELECT * ". Specify the columns you need, in this case all you would need is count of matching records. 2 ) Having said that, searching for the username to see if exists before adding a new user is not the way to it. I don't know the structure of your users table but you should add a unique index on the username so it will throw an error if you try to add a second with same name. It is then only necessary to insert the new record try { insert new user } catch (exce[tion]) ( if exception is duplicate key error username error else re-throw error ) 3 ) Do not store passwords as plain text. Use password_hash() prior to storing and password_verify() when checking. When you connect using PDO, are you setting the emulate prepares attribute to false?
  2. Yeah, really sorry that my first reply wasn't what you wanted. But then again, the question you asked was nothing like what you wanted either and was a complete waste of time all round. And as I voulunteer my time here for free, I don't like having it wasted. If this wasn't what you wanted, learn how to ask better questions.
  3. I haven't a clue what $dizi is - it doesn't appear in your code. But if your ajax call is receiving a response like that in your code then the code below is an example of how to grab the data from it EG <?php if (isset($_GET['ajax'])) { // respond to the ajax request $resp = <<<EOF {"class":"POLL","time":"2010-04-05T21:27:54.84Z","active":1, "tpv":[{"class":"TPV","tag":"MID41","device":"/dev/ttyUSB0", "time":1270517264.240,"ept":0.005,"lat":40.035093060, "lon":-75.519748733,"alt":31.1,"track":99.4319, "speed":0.123,"mode":3}], "sky":[{"class":"SKY","tag":"MID41","device":"/dev/ttyUSB0", "time":"2010-04-05T21:27:44.84Z","hdop":9.20,"vdop":12.1, "satellites":[{"PRN":16,"el":55,"az":42,"ss":36,"used":true}, {"PRN":19,"el":25,"az":177,"ss":0,"used":false}, {"PRN":7,"el":13,"az":295,"ss":0,"used":false}, {"PRN":6,"el":56,"az":135,"ss":32,"used":true}, {"PRN":13,"el":47,"az":304,"ss":0,"used":false}, {"PRN":23,"el":66,"az":259,"ss":40,"used":true}, {"PRN":20,"el":7,"az":226,"ss":0,"used":false}, {"PRN":3,"el":52,"az":163,"ss":32,"used":true}, {"PRN":31,"el":16,"az":102,"ss":0,"used":false} ] } ] } EOF; exit($resp); // return the ajax response } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Sample</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> $().ready( function() { gondor() }) function gondor() { $.get( "" , // blank url - calls self {"ajax" : 1} , // data to pass to url function(resp) { $("#time").html(resp.tpv[0].time) $("#latitude").html(resp.tpv[0].lat) $("#longitude").html(resp.tpv[0].lon) $("#altitude").html(resp.tpv[0].alt) $("#hdop").html(resp.sky[0].hdop) $("#vdop").html(resp.sky[0].vdop) }, "JSON" ) } </script> <style type="text/css"> body { font-family: verdana,sans-serif; font-size: 12pt; padding: 20px 50px; } #output { width:60%; margin: 50px auto; border: 2px solid gray; padding: 30px; } .data {width:150px; display: inline-block; text-align: right; color: #369; padding: 8px; } label {width:150px; display: inline-block; text-align: left; padding: 8px; } </style> </head> <body> <div id="output"> <label>Time</label>:<div id="time" class="data"></div><br> <label>Longitude</label>:<div id="longitude" class="data"></div><br> <label>Latitude</label>:<div id="latitude" class="data"></div><br> <label>Altitude</label>:<div id="altitude" class="data"></div><br> <label>HDOP</label>:<div id="hdop" class="data"></div><br> <label>VDOP</label>:<div id="vdop" class="data"></div> </div> </body> </html>
  4. Try function unread_count( $id ) // pass id of receiver to the function { $stmt = $this->db->prepare("SELECT COUNT(*) as total FROM pm WHERE unread = 1 AND receiver_id = :id "); $stmt->execute( [ 'id' => $id ] ); return $stmt->fetchColumn(); }
  5. "$result->num_rows" is not PDO. The PDO equivalent is $stmt->rowCount(); You cannot mix mysqli calls with PDO calls. Manual - PDO (If you use SELECT COUNT(*) then only one row containing the count will be returned.)
  6. Here's an example <?php session_start(); if (isset($_GET['ajax'])) { // respond to the ajax request $val = $_SESSION['cumval'] ?? 0; ++$val; $_SESSION['cumval'] = $val; exit("$val <br>"); } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Sample</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> $().ready( function() { setInterval('gondor()', 1000) }) function gondor() { $.get( "" , // blank url - calls self {"ajax" : 1} , // data to pass to url function(resp) { $("#output").append(resp) }, "TEXT" ) } </script> <style type="text/css"> body { font-family: verdana,sans-serif; font-size: 12pt; padding: 20px 50px; } </style> </head> <body> <div id="output"> </div> </body> </html>
  7. And what, exactly, is the problem you are having?
  8. And other variations if ( date('l j') == 'Friday 13' ) if ( date('l d') == 'Friday 13' ) if ( date('D j') == 'Fri 13' ) if ( date('D d') == 'Fri 13' )
  9. Yes, so long as there is no record for another customer containing car A | 2018-08-11 (There is a tutorial on resource booking in my signature)
  10. Easiest way... +--------------+ +-------------+ | Car | | customer | +--------------+ +-------------+ | car_id (PK) |--+ +--| cust_id (PK)| | reg_no | | | | name | | make | | | +-------------+ | model | | | +--------------+ | | | | | +-------------------+ | | | booking | | | +-------------------+ | | | booking_id (PK) | | +--<| car_id (UQ) | | | hire_date (UQ) | | | cust_id |>-+ +-------------------+ In the booking table (car_id, hire_date) is defined as a unique key and a record is written for each day that the car is hired. Because the key is unique, booking it to another customer on one of those dates will throw an error and the transaction should be cancelled.
  11. My table is in addition to your table containing the colors (if you read the query there is a JOIN between your table and mine) SELECT f.name , COALESCE(t.color, 0) as color FROM fruit f -- extra table "fruit" defining fruits required LEFT JOIN table t ON f.name = t.fruit -- your existing table "table" containing the color The moral is - to get information out of a database it has to be in there
  12. Isn't that like addressing a meeting and asking "Hands up anyone who isn't here?" To do it with a query only you would need a table with four rows fruit table --+------------- id| name --+------------- 1 | grape 2 | apple 3 | banana 4 | pomegranate then SELECT f.name , COALESCE(t.color, 0) as color FROM fruit f LEFT JOIN table t ON f.name = t.fruit Alternatively you can do it in your code with an array with fruit name as the key. Either way it needs to know what fruits to expect. Psychic mysql is a few versions away yet.
  13. I don't know if it's your only problem but the fetch_array() call after the query is effectively throwing away the first row. You don't output anything until you read the second row in the while() statement at the start of the loop.
  14. Try SELECT username , COUNT(*) as topics_started FROM ( SELECT user_id, username -- find username in record matching first post in each thread FROM xf_post JOIN ( SELECT thread_id -- subquery to find time of first post in each thread , MIN(timestamp) as timestamp FROM xf_post GROUP BY thread_id ) firstpost USING (thread_id, timestamp) ) firstuser GROUP BY user_id ORDER BY topics_started DESC;
  15. That's what I would expect. Give them a link to download the PDF file/chapters (perhaps zipped) If I paid for expect I would expect access any time. Have a "Your Library" page where they can go to and view or download (as they want) stuff that the purchased.
  16. if (date('j') == 13 && date('w')== 5) { echo "You should have stayed in bed today" ; } See date() function
  17. The above is a generic model. In your case, with only three categories and no hierarchy it can be simplified. Keep the attribute, category and attribute tables as you need to know which attributes to define for each product. CATEGORY CATEGORY_ATTRIBUTE ATTRIBUTE +-------------+-------------+ +-------------+--------------+ +--------------+-----------+ | category_id | cat_name | | category_id | attribute_id | | attribute_id | attr_name | +-------------+-------------+ +-------------+--------------+ +--------------+-----------+ | 1 | Subscrption | | 1 | 2 | | 2 | Type | | 2 | Book | | 1 | 3 | | 3 | Duration | | 3 | T-shirt | | 2 | 4 | | 4 | Title | +-------------+-------------+ | 2 | 5 | | 5 | Author | | 3 | 6 | | 6 | Size | | 3 | 7 | | 7 | Color | | 3 | 8 | | 8 | Style | +-------------+--------------+ +--------------+-----------+ And then your product table using option #2 (JSON attributes column) +------------+---------------------------------+-------------+------------------------------------------------------------------------------+ | product_id | prod_name | category_id | attributes | +------------+---------------------------------+-------------+------------------------------------------------------------------------------+ | 1 | Standard Membership (12 months) | 1 | {"Type": "S", "Duration": 12} | | 2 | Premium Membership (3 months) | 1 | {"Type": "P", "Duration": 3} | | 3 | Dystopian novel | 2 | {"Title": "Brave New World", "Author": "Aldus Huxley"} | | 4 | Harry Potter Book | 2 | {"Title": "Harry Potter and the Philosophers Stone", "Author": "JK Rowling"} | | 5 | T-Shirt | 3 | {"Size": "M", "Color": "Red", "Style": "V-neck"} | | 6 | T-Shirt | 3 | {"Size": "L", "Color": "White", "Style": "V-neck"} | | 7 | T-Shirt | 3 | {"Size": "L", "Color": "Red", "Style": "V-neck"} | | 8 | T-Shirt | 3 | {"Size": "L", "Color": "Black", "Style": "crew-neck"} | | 9 | Harry Potter Book | 2 | {"Title": "Harry Potter and the Goblet of Fire", "Author": "JK Rowling"} | +------------+---------------------------------+-------------+------------------------------------------------------------------------------+ As your only common attribute is price, have separate price table +------------------+------------+-------+------------+-------------+ | product_price_id | product_id | price | valid_from | valid_until | +------------------+------------+-------+------------+-------------+ | 1 | 1 | 86.10 | 2000-01-01 | 9999-12-31 | | 2 | 2 | 50.72 | 2000-01-01 | 9999-12-31 | | 3 | 3 | 95.31 | 2000-01-01 | 9999-12-31 | | 4 | 4 | 24.38 | 2000-01-01 | 9999-12-31 | | 5 | 5 | 35.99 | 2000-01-01 | 9999-12-31 | | 6 | 6 | 6.80 | 2000-01-01 | 9999-12-31 | | 7 | 7 | 26.03 | 2000-01-01 | 9999-12-31 | | 8 | 8 | 9.77 | 2000-01-01 | 9999-12-31 | | 9 | 9 | 70.75 | 2000-01-01 | 9999-12-31 | +------------------+------------+-------+------------+-------------+ As an example query, list all T-Shirts size "L" SELECT product_id , prod_name , attributes->>'$.Size' as size , attributes->>'$.Color' as color , attributes->>'$.Style' as style , price FROM product_j p JOIN product_price pp USING (product_id) WHERE attributes->>'$.Size' = 'L' AND category_id = 3 AND CURDATE() BETWEEN valid_from AND valid_until; +------------+-----------+------+-------+-----------+-------+ | product_id | prod_name | size | color | style | price | +------------+-----------+------+-------+-----------+-------+ | 6 | T-Shirt | L | White | v-neck | 6.80 | | 7 | T-Shirt | L | Red | v-neck | 26.03 | | 8 | T-Shirt | L | Black | crew-neck | 9.77 | +------------+-----------+------+-------+-----------+-------+
  18. If I re-create your error <?php $myObject->has(); ?> then I get this Note the line number in the error message. Your error reporting seems be ignoring that bit of information so you need fix that or search your code for occurences of "has(". If your luckyy there will be only one, if unlucky there could be dozens. You then need to find out which one has the null object calling it. You then find where that object is being created and see what went wrong.
  19. Somewhere in your code you have something like $myObject->has() but $myObject contains null and is not a valid object
  20. If you have PHP code in a file, rename it as .php. Any HTML in the file will work exactly the same as in a .html file.
  21. One way <?php $fields = ['p1', 'p2', 'p3', 'p4', 'p5']; $valid_nums = range(1,10); $output = ''; if (isset($_GET['p1'])) { foreach ($fields as $f) { if (!in_array($_GET[$f], $valid_nums)) { $clr = 'Not in range'; $cls = ''; } else { if ($_GET[$f]%2 ==0) { $clr = 'Black'; $cls = 'class="even"'; } else { $clr = 'Red'; $cls = 'class="odd"'; } } $output .= sprintf("%s <span %s>%2d %s</span><br>", strtoupper($f), $cls, $_GET[$f], $clr); } } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Sample</title> <style type="text/css"> body { font-family: verdana,sans-serif; font-size: 12pt; padding: 20px 50px; } .even { color: black; font-weight: 600;} .odd { color: red; font-weight: 600; } </style> </head> <body> <?=$output?> <hr> <form> P1 <input type="text" name="p1" size="5"><br> P2 <input type="text" name="p2" size="5"><br> P3 <input type="text" name="p3" size="5"><br> P4 <input type="text" name="p4" size="5"><br> P5 <input type="text" name="p5" size="5"><br><br> <input type="submit" name="btnSub" value="Submit"> </form> </body> </html> Sample output:
  22. Apparently a space character, or one of the HTML spacing options (&nbsp; &ensp; &emsp;) isn't good enough.
  23. An empty string is not, and AFAIK never has been, a valid integer value mysql> CREATE TABLE test99 (id int, val varchar(20) ); Query OK, 0 rows affected (0.32 sec) mysql> INSERT INTO test99 VALUES ('',''); ERROR 1366 (HY000): Incorrect integer value: '' for column 'id' at row 1
×
×
  • 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.