Escaping Characters Such as $ and % | MySQL and PHP

前端 未结 3 721
长发绾君心
长发绾君心 2021-01-20 21:54

So basically I’ve been digging deep into the realm of MySQL and PHP…specifically the security measures I should take when dealing with a database and form inputs. So far I’

3条回答
  •  渐次进展
    2021-01-20 22:13

    Depending on what kind of data and what it is used for.

    If you find PHP default prepared statements are too long and complex to remember I suggest to have look at some classes available on github to give you an idea of simplified queries.

    A Good example @ https://github.com/joshcam/PHP-MySQLi-Database-Class

    An example of insert queries with this class

    $data = Array (
        'login' => 'admin',
        'active' => true,
        'firstName' => 'John',
        'lastName' => 'Doe',
        'password' => $db->func('SHA1(?)',Array ("secretpassword+salt")),
        // password = SHA1('secretpassword+salt')
        'createdAt' => $db->now(),
        // createdAt = NOW()
        'expires' => $db->now('+1Y')
        // expires = NOW() + interval 1 year
        // Supported intervals [s]econd, [m]inute, [h]hour, [d]day, [M]onth, [Y]ear
    );
    
    $id = $db->insert ('users', $data);
    if ($id)
        echo 'user was created. Id=' . $id;
    else
        echo 'insert failed: ' . $db->getLastError(); 
    

提交回复
热议问题