Jump to content

Barand

Moderators
  • Posts

    24,344
  • Joined

  • Last visited

  • Days Won

    795

Posts posted by Barand

  1. You should use prepared statements and bind user input as parameters rather than using mysqli_real_escape_string().

    Better still, as you are still learning, put your efforts into learning PDO instead of mysqli.

    If $con is a PDO connection, the above code becomes

    $stmt = $con->prepare("INSERT INTO clients2 (fullname, company, email, serial_no)
                           VALUES (?,?,?,?)
                           ";
    $stmt->execute( [ $_POST['fullname'], $_POST['company'], $_POST['email'], $_POST['serial_no'] ]  );                

    IE prepare the query putting placeholders for the user inputs then execute it passing the values in an array.

  2. I would recomend that you don't prepopulate the "square" and then update.

    If you start with an empty table ...

    CREATE TABLE `square` (
      `square_id` int(11) NOT NULL ,
      `user_id` int(11) NOT NULL,
      PRIMARY KEY (`square_id`),
      KEY `idx_square_user_id` (`user_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    ... and add squares as they are selected then it becomes impossible for a user to poach someone else's square - the primary key will make it a strictly "first come first served" situation. Should cut down the bar-room brawls.

  3. If your db date fields are DATETIME or TIMESTAMP then they will not match your $message_date because of the time values.

    IE "2019-06-09" <> "2019-06-09 10:30:57"

    You need to extract the date portions

    ... WHERE DATE(message_date) = ?

    Also , to make the date calculations easier

    echo "<select>";                                    
    
    $d1 = new DateTime('next sunday');
    $d2 = new DateTime("last sunday of december");
    $d2->modify('+1 day');
    $sevendays = new DateInterval('P7D');
    $dp = new DatePeriod($d1, $sevendays, $d2);
    foreach ($dp as $d) {
        echo '<option value="' . $d->format('Y-m-d').'">' . $d->format('M d, Y') . '</option>';
    }
                                                                                            
    echo "</select>";

     

  4. 20 minutes ago, Jim R said:

    (It’s just a bunch of friends choosing up squares for sporting events.

    In that case, doesn't your query need to to check if a square is already taken? In between you displaying available squares and a user picking his squares, someone else might have picked one or more of the same squares.

  5. 9 hours ago, dil_bert said:

    well - to be honest - i really do not want to do any harm - so i don’t recommend granting ALL permissions. 
    For standard users, i now do recommend granting GRANT SELECT,INSERT,UPDATE,DELETE permissions.

    this is what i tell everybody...

    Then stop telling them.

    The last thing you want is for standard users to be able to grant privileges. That is an admin function.

    • Like 1
  6. Having got the keys with the min value you then need to get those items from the original array

    $a = array(-15, 3, 5, -1, -15);
    $b = array(1, 3, 5);
    
    function lowest_int ($c){
        $result = [];
        foreach (array_keys($c, min($c)) as $k) {
                $result[$k] = $c[$k];
        }
        return $result;    
    }          
    
    echo '<pre>';
    print_r(lowest_int($a));
    print_r(lowest_int($b));
    echo '</pre>';

    output

    Array
    (
        [0] => -15
        [4] => -15
    )
    Array
    (
        [0] => 1
    )

    An alternative solution could be to use array_filter() in your function to remove items not equal to the minimum value

    function lowest_int ($c){
          $min = min($c);
          return array_filter( $c, function($v) use($min) { return $v==$min; } );
    }          

     

  7. FROM A,B WHERE A.xxx = B.xxx

    Much better to use explicit joins ...

    FROM A INNER JOIN B ON A.xxx = B.xxx
    • The joins then reflect the structure of the data.
    • The WHERE clause isn't cluttered with these structure details
    • Implicit syntax can only be used for INNER JOINS so no code consistency with other join types
    • Optimization seems improved now but there used to be a distinct speed advantage when using explicit joins in older versions of mysql
  8. 12 minutes ago, mahenda said:

    #1054 - Unknown column 'c.comment' in 'field list'

    If you are getting that error then the create statements you posted earlier must be incorrect. It says your "comments" table does not have a column "comment".

    I created tables using your create code and ginerjm's query (although I don't agree with with his implicit join syntax) doesn't give an error.

  9. I have just run this sequence of SQL and no issues

    CREATE TABLE `foo` (
      `foo_id` int(11) NOT NULL AUTO_INCREMENT,
      `number` int(11) DEFAULT NULL,
      PRIMARY KEY (`foo_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
    
    INSERT INTO foo (number) VALUES (5),(10);
    
    SELECT * FROM test.foo;
    +--------+--------+
    | foo_id | number |
    +--------+--------+
    |      1 |      5 |
    |      2 |     10 |
    +--------+--------+
    
    --
    --  add generated columns
    --
    
    ALTER TABLE foo add number2 DECIMAL GENERATED ALWAYS AS (number*2) STORED, add nombre3 DECIMAL GENERATED ALWAYS AS (number*3) STORED;
    
    --
    -- check new table
    --
    SELECT * FROM test.foo;
    +--------+--------+---------+---------+
    | foo_id | number | number2 | nombre3 |
    +--------+--------+---------+---------+
    |      1 |      5 |      10 |      15 |
    |      2 |     10 |      20 |      30 |
    +--------+--------+---------+---------+
    
    --
    --  insert a couple more records
    --
    
    INSERT INTO foo (number) VALUES (15),(20);
    
    --
    --  check again
    --
    
    SELECT * FROM test.foo;
    +--------+--------+---------+---------+
    | foo_id | number | number2 | nombre3 |
    +--------+--------+---------+---------+
    |      1 |      5 |      10 |      15 |
    |      2 |     10 |      20 |      30 |
    |      3 |     15 |      30 |      45 |
    |      4 |     20 |      40 |      60 |
    +--------+--------+---------+---------+

    Perhaps you need to give us more details on exactly what you are doing

    • Thanks 1
  10. The usual advice I would give for this problem would be for you to echo $value within the loop so you can see what the variable contains.

    In your case you have already done this and you will have seen the values "5" and "obodo".

    As it is then blatantly obvious that $value is not an array, specifically not an array with a key of 'user_id', then it's hard to see why you even need to ask the question.

  11. 1. Don't put them in there in the first place.

    or

    2.

    $phone = "(123) 456-7890" ;
    $justdigits = '';
    
    for ($i=0, $k=strlen($phone); $i<$k; $i++) {
        $justdigits .= (ctype_digit($phone[$i])) ? $phone[$i] : ''; 
    }
    echo $justdigits ;     //--> 1234567890

     

    When it comes to formatting the output I find  a custom formatting function of use;
     

    $phone = '1234567890234'
    $format = '(###) ###-#### ext ###';
    
    echo formatIt ($format, $phone);                      // -> (123) 456-7890 ext 234
    
    
    function formatIt($format,$str)
    {
        $i = $j = 0;
        $res = '';
        $kf = strlen($format);
        $str = str_replace(' ','',$str);
        $ks = strlen($str);
        while ($i < $kf  && $j < $ks) {
            $res .= $format[$i]=='#' ? $str[$j++] : $format[$i];
            ++$i;
        }
        if ($j<$ks) $res .= substr($str,$j);
        return $res;
    }

     

  12. Something like this?

    <input type="text" name="phone" id="phone" value="(___) ___ - ____" >

    and

            $("#phone").keydown( function(e) {
                var output = $("#phone").val()
                if (output == '') output = "(___) ___ - ____"
                if (output.indexOf('_') != -1) {
                    e.preventDefault()
                    if (48 <= e.keyCode && e.keyCode <= 57) {         // keyboard top row
                        output = output.replace('_', e.keyCode-48)
                    }
                    else if (96 <= e.keyCode && e.keyCode <= 105) {   // numeric keypad
                        output = output.replace('_', e.keyCode-96)
                    }
                    $("#phone").val(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.