Jump to content

Barand

Moderators
  • Posts

    24,604
  • Joined

  • Last visited

  • Days Won

    830

Everything posted by Barand

  1. Just bad typing then?
  2. The person_id should be unique in person_tbl. I showed you in my earlier reply how the persons_web_tbl should be.
  3. But for future reference, as benanamen said, don't waste time checking first. Ensure that person_id is either the primary key or is defined as a UNIQUE key. Then it is impossible to insert a duplicate and will throw an error/exception. Insert the new record and check for a duplicate key error. If there is one, output error message or "found", otherwise it worked and job done. Example CREATE TABLE `user_demo` ( `userid` int(11) NOT NULL AUTO_INCREMENT, `fname` varchar(20) DEFAULT NULL, `lname` varchar(20) DEFAULT NULL, `username` varchar(20) DEFAULT NULL, PRIMARY KEY (`userid`), UNIQUE KEY `username` (`username`) ) DATA: +--------+-------+----------+----------+ | userid | fname | lname | username | +--------+-------+----------+----------+ | 1 | Laura | Norder | norderl | | 2 | Tom | DiCanari | tomd | | 3 | Harry | Potter | harryp | +--------+-------+----------+----------+ Attempting to insert another user with same username gives a 1062 Duplicate key error mysql> INSERT INTO user_demo (fname, lname, username) -> VALUES ('Harry', 'Palmer', 'harryp'); ERROR 1062 (23000): Duplicate entry 'harryp' for key 'username' The processing would be try { insert new record } catch (exception e) { if error number = 1062 echo "Duplicate!" end if }
  4. That is not the way to do it. DB tables are not spreadsheets
  5. In that case your "persons_web_table" needs to allow multiple person_id" EG person_id | platform -----------+------------ 1 | Facebook 1 | Twitter 1 | Instgram 2 | Facebook 2 | Twitter 3 | Twitter
  6. Your method creates a connection each time you run a query. That method creates a single connection for each object and uses it for all the queries in the object. Better still would be to create a single connection for the page and pass that one connection to each new object when it is created. At the top of each page, have require 'db_inc.php'; // <--- CONTAINS ----- # const HOST = "localhost"; # const USER = "root"; # const PWD = ""; # const DBNAME = "cms"; # # function pdoconnect() # { # $dsn = 'mysql:host=' . HOST . ';dbname=' . DBNAME; # $pdo = new PDO ($dsn, USER, PWD); # $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); # $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); # $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); # return $pdo; # } # $connection = pdoconnect(); // create connection then your code becomes <?php class dbh { protected $conn; public function __construct($connection) { $this->conn = $connection; } } class PostsData extends dbh{ public function getPosts() { $sql = "SELECT * FROM posts_tbl"; $stmt = $this->conn->prepare($sql); $stmt->execute(); $result = $stmt->fetchAll((PDO::FETCH_OBJ)); return $result; } public function addPost($filter_author, $filter_title, $filter_txt) { $sql = "INSERT INTO posts_tbl (post_author, post_title, post_txt) VALUES (?, ?, ?)"; $stmt = $this->conn->prepare($sql); $stmt->execute([$filter_author, $filter_title, $filter_txt]); } } $post = new PostsData($connection); // pass connection to new object $posts = $post->getPosts(); foreach ($posts as $post) { echo $post->post_title . '<br>'; } ?>
  7. You are still connecting in each of your functions instead of just once. Try <?php class dbh{ private $host = "localhost"; private $user = "root"; private $pwd = ""; private $dbname = "cms"; protected $conn; // make available to subclass public function __construct() { $this->conn = $this->connect(); } private function connect() { $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname; $pdo = new PDO ($dsn, $this->user, $this->pwd); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); return $pdo; } } class PostsData extends dbh{ public function getPosts() { $sql = "SELECT * FROM posts_tbl"; $stmt = $this->conn->prepare($sql); $stmt->execute(); $result = $stmt->fetchAll((PDO::FETCH_OBJ)); return $result; } public function addPost($filter_author, $filter_title, $filter_txt) { $sql = "INSERT INTO posts_tbl (post_author, post_title, post_txt) VALUES (?, ?, ?)"; $stmt = $this->conn->prepare($sql); $stmt->execute([$filter_author, $filter_title, $filter_txt]); } } $post = new PostsData(); $posts = $post->getPosts(); foreach ($posts as $post) { echo $post->post_title . '<br>'; } ?>
  8. Normalize your data (as @requinix has just said while I was still typing) +------------------+ +------------------+ +------------------+ +------------------+ | gigassigned | | user | | user_platform | | platform | +------------------+ +------------------+ +------------------+ +------------------+ | userid |>----------------| id |-----------------<| userid | +------| platformid | | gigid | | selfdescription | | platformid |>-----+ | name | | answer | | usertype | | url | +------------------+ | dateadded | | username | | rangelow | +------------------+ | firstname | | rangehigh | | surname | +------------------+ | status | | datecreated | | town | +------------------+
  9. I agree with ginerjm - add "low" and "high" columns. (You can run an update query using the same functions I have used below to populate them) But, given what you have currently SELECT * FROM simona6; +----+-------+-------------+ | id | name | followers | +----+-------+-------------+ | 1 | Curly | 2500-5000 | | 2 | Larry | 5000-10000 | | 3 | Mo | 2500-5000 | | 4 | Peter | 5000-10000 | | 5 | Paul | 10000-15000 | | 6 | Mary | 15000-20000 | +----+-------+-------------+ SELECT name , substring_index(followers, '-', 1) as low , substring_index(followers, '-', -1) as high FROM simona6 WHERE 3000 BETWEEN substring_index(followers, '-', 1) AND substring_index(followers, '-', -1); +-------+------+------+ | name | low | high | +-------+------+------+ | Curly | 2500 | 5000 | | Mo | 2500 | 5000 | +-------+------+------+
  10. Do not create a connection every time you want to execute a query. connecting probably takes longer than the query so you more than double the execution time you will soon run out of connections certain functions (such as lastInsert() ) are valid only in the current connection Create the connection (once per script unless more than one server is used), store in $this->connection, or similar, and use that each time for the function calls.
  11. There are 5 parts to an ajax call generating the call (which you have shown us) The headers sent to the server the processing on the server the response returned the processing of the response (also shown) All of those need to be correct.
  12. Have you checked the calls' headers and responses in the network tab of your browser's developer tools?
  13. … or their correct salutation is Dr, or some other title (eg Prof.) that is not gender-related
  14. I rarely use sets so I keep forgetting about the FIND_IN_SET() function
  15. If there are are 10 values in your array then you need 10 placeholders i.e. NOT IN (?,?,?,?,?,?,?,?,?,?) then $stmt->execute( $yourarray );
  16. That line clears the data each time through the loop. Put it before the foreach() loop.
  17. Here's an example // // set default values // $senderid = $_GET['sender'] ?? '0'; $recipid = $_GET['recipient'] ?? '0'; $page = $_GET['page'] ?? 1; if (isset($_GET['btnSub'])) $page = 1; // new search was requested $total_recs = 0; $search_results = ''; if (isset($_GET['page'])) { // was form submitted? // // build query WHERE clause // $whereclause = ''; $where = []; $params = []; if ($senderid != 0) { $where[] = "(sender_id = ?)"; $params[] = $senderid; } if ($recipid != 0) { $where[] = "(recipient_id = ?)"; $params[] = $recipid; } if ($where) { $whereclause = "WHERE " . join(' AND ', $where); } // get total records from the search $count = $db->prepare("SELECT COUNT(*) FROM notes n INNER JOIN user s ON n.sender_id = s.user_id INNER JOIN user r ON n.recipient_id = r.user_id $whereclause"); $count->execute($params); $total_recs = $count->fetchColumn(); // now get the search results if (intval($page) == 0) $page = 1; $offset = ($page - 1) * PERPAGE; $limit = PERPAGE; $stmt = $db->prepare("SELECT n.id , s.username as sender , r.username as recipient , n.message , date_format(time_sent, '%a %b %D %l:%i %p') as sent FROM notes n INNER JOIN user s ON n.sender_id = s.user_id INNER JOIN user r ON n.recipient_id = r.user_id $whereclause ORDER BY time_sent LIMIT $offset, $limit "); $stmt->execute($params); foreach ($stmt as $rec) { $search_results .= " <div class='result'> <div class='fromto'> <div class='label'>From:</div>". esc($rec['sender']) . "<br> <div class='label'>To:</div>". esc($rec['recipient']) . "<br><br> <div class='label'>Sent:</div>". esc($rec['sent']) . "<br> </div> <div class='msg'>". esc($rec['message']) . "</div> <div style='clear:both'></div> </div>\n"; } }
  18. NOTE TO READERS: The above statement applies only to those using PDO. If you are using mysqli then prepared statements are a completely different ballpark to those using query()
  19. Have you checked the error log?
  20. Yes, but what have you tried so far?
  21. HTML knows nothing about php variables. If it's in the HTML section (as this is) it needs embedding inside php tags Either <a href='test.php?id=<?php echo $uri; ?>'>link to page2</a> or use short tags <a href='test.php?id=<?=$uri?>'>link to page2</a>
  22. In the first post the href was in the middle of a double-quoted php string I.E. echo "<tr> <td> <a href='Details/21/index.php?id=$id'>$id</a> </td> </tr>"; We have no context for this latest post - is it in the HTML portion or PHP portion of your code?
  23. Don't rely on table position (last four). Add a "dropdown" flag (1 = used in dropdown, 0 = not)
  24. I've just looked at the data in your sql dump files. Did I say the design was terrible? I apologise for the gross understatement and for referring to it as a database As I hope I demonstrated in my earlier posts, if you get the data right the processing becomes a whole lot easier. Nothing in the other tables matches those column names in your prices table, so all of my solutions go out of the window. There might have been some hope if the first column in prices mapped to id #1 and the 2nd column to id #2 but even that isn't the case. I'll check if dropping "Price" suffix from the column names can save the day. I'll have a look at your code now, but first I need a stiff drink, or several - a large gin and tonic should help.
  25. The solution should work for those products in the product table whose name matches a column name in the price table. Basically you just need +----+-------+-------+ | id | name | price | +----+-------+-------+ | 1 | prod1 | 10.00 | | 2 | prod2 | 20.00 | | 3 | prod3 | 30.00 | +----+-------+-------+ You can Use the id/name columns to get your product dropdown. Use id/price columns to create your prices array. I don't know what SS Req, MS Req etc are, or how they relate to products, so I can't comment on those No, they are not required. The array elements are your variables If you want to display the price for product 2 then you don't need $ESSPrice, just echo $prices[ $prods[2] ]
×
×
  • 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.