Jump to content

Barand

Moderators
  • Posts

    24,423
  • Joined

  • Last visited

  • Days Won

    806

Posts posted by Barand

  1. 1 hour ago, chhorn said:

    At least there are PHP-tags missing.

    "Unexpected end of file" errors occur when the php parser expects something but cannot find it. Usual causes are an opening { without a corresponding closing } or quots at the start of a string and then no closing quotes.

    You'll have to find it yourself as it's impossible from just the couple of isolated snippets that we can see.

  2. if I were you I'd use a newline (\n) to separate the data instead of "@@@".

    if(isset($_POST['textdata']))
    {
        file_put_contents('data.txt', $_POST['textdata']."\n", FILE_APPEND);
    }

    Then, if you data.txt file contains, say

    TESTA
    TESTB
    TESTC
    TESTD

    ... you can read it back and separate the items into an array using file();

    $data = file('data.txt');

    giving

    $data = Array
    (
        [0] => TESTA
    
        [1] => TESTB
    
        [2] => TESTC
    
        [3] => TESTD
    )

     

    • Like 1
  3. +----------------+                                                          +----------------+
    | Make sure to   |---+                                             +------->| (e.g. Courier) |
    +----------------+   |                                             |        +----------------+
                         |                                             |             |
                         |  +----------+                               |             |
                         +->|  use a   |---+                           |             |        +----------------+
                            +----------+   |                           |             +------->| and use spaces |
                                           |                           |                      +----------------+
                                           |    +----------------+     |                                |    
                                           +--->| monospace font |-----+                                |     
                                                +----------------+                                      |
                                                                                 +----------+           |         
                                                                                 | not tabs |<----------+         
                                                                                 +----------+                                 
                                                                                       |
            +--------------------------------------------------------------------------+
            |
            V                                                                    
    +---------------+
    | It also helps |
    +---------------+
            |        
            |        
            |                         +-------------------+                       +-------------------+
            +------------------------>| if you sometimes  |---------------------->|   switch between  |
                                      +-------------------+                       +-------------------+
                                                                                            |
                                                                                            |
                                                                          +-----------------+-----------------+
                                                                          |                                   |
                                                                          |                                   |
                                                                +-------------------+                +-------------------+
                                                                |     overtype      |                |      insert       |
                                                                +-------------------+                +-------------------+
                                                                          |                                   |
                                                                          |                                   |
                                                                          |            +----------+           |
                                                                          +----------=>|  modes   |<----------+
                                                                                       +----------+

     

    • Like 2
    • Haha 1
    • Great Answer 1
  4. The query has inbuilt syntax errors. Your WHERE clause will always begin with "WHERE AND … "

    IMO a cleaner way to include conditions only if there is a value is

    $min_price = 10;
    $max_price = 50;
    $featured  = 1;
    $binds = [];
    
    $where = [];
    $whereclause = '';
    
    if ($min_price > 0) {
        $where[] = "min_price >= ?";
        $binds[] = $min_price;
    }
    if ($max_price > 0) {
        $where = "max_price <= ?";
        $binds[] = $max_price;
    }
    if (in_array($featured, [0,1])) {
        $where[] = "featured = ?";
        $binds[] = $featured ;
    }
    
    if ($where) $whereclause = 'WHERE ' . join(' AND ', $where);
    
    $find_records = $db->prepare(" SELECT * 
                                   FROM projects
                                   $whereclause
                                   ");
    $find_records->execute($binds);
    $result_records = $find_records->fetchAll(PDO::FETCH_ASSOC);

     

  5. If you do it the second way (no placeholders), there is no point in preparing it; just use $db->query().

    CAVEAT: If $vars originated from an external source ($_GET, $_POST, $_COOKIE etc) then you are injection-prone and, as you are not even escaping the values your queries could fail.

    EG

    $username = "O'Reilly";
    
    $res = $db->query("SELECT password FROM user WHERE username = '$username' ")      // fails with syntax error and open to injection

    If in doubt, prepare();

     

    1 minute ago, imgrooot said:

    But if you see my min and max price, i use equals to and less/more than operators to compare.  But your array does not show that.

    Your bindings do not either, the query does. The array is just a more convenient way of binding.

  6. Your WHERE clause will then be like this...

     WHERE id = N AND duplicate = 'False'

     

    6 minutes ago, Nigel12 said:

    I'm using VB.NET

    You have my sympathy.

     

    Also those "Answer_x" columns should ne normalized into a separate table; separate row for each answer.

  7. 30 minutes ago, tryingphp said:

    ("DELETE FROM users WHERE user_id = CURRENT_DATE+3 ");

    The thing about programming is that it requires some thought. Why would as user_id be equal to a date value?

    Why don't you do some reading about how to use SQL instead of taking the "infinite monkeys with typewriters" approach in the hope you eventually come up with a right answer?

    On 11/16/2019 at 7:37 PM, Barand said:

    Instead of deleting the record, update the record setting the expiry date to CURRENT_DATE+3 days

  8. On 11/16/2019 at 7:37 PM, Barand said:

    One way would be to add an "expiry_date" (default NULL) column to your user table.

    Instead of deleting the record, update the record setting the expiry date to CURRENT_DATE+3 days.

    Run a job every day that does a "DELETE FROM user WHERE expiry_date < CURRENT_DATE

    This time, read what I said.

  9. You have this comment...

    1 hour ago, kiko12122 said:

    // We don't need to write to the file, so just open for reading

    but you don't get around to actually opening the file - the comment won't do it for you. Therefore in the next line $fp has not been defined.

    1 hour ago, kiko12122 said:

     $file_data = fread( $fp, 8 * 'KB_IN_BYTES' ); 

    Further, you have put 'KB_IN_BYTES' inside quotes thus making it a string value (which has a numeric value of 0).

    So I guess the problem is in trying to read 0 bytes from a file that doesn't exist.

    And what is the comment about being "good citizens"? You don't close it either.

    (Has KB_IN_BYTES been defined as constant anywhere?)

  10. Try this

    // Attempt delete query execution
    $stmt = $dbc->prepare("DELETE FROM users WHERE user_id = ? ");   // prepare query with placeholder (?) for id value
    $stmt->bind_param('i', $_SESSION['user_id']);                    // bind the id value to the placeholder
    
    if ($stmt->execute()) {                                          // execute the query
        echo "Records were deleted successfully.";
    } else {
        echo "ERROR: Not able to execute query " ;
    }

     

×
×
  • 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.