php - PDO insert not working -
so please bare me, i'm trying work pdo create scripts more secure. have demo , set variable $sth->bindparam(':ip', $ip);
, phone call
$data = $con->prepare("insert users (ip) values (':ip')");
the problem reason not entering database. here's total code
<?php $host = "localhost"; $dbname = "users"; $user = "root"; $pass = "root"; try{ $con = new pdo("mysql:host=$host;dbname=$dbname", $user, $pass); } catch(pdoexception $e){ echo $e->getmessage(); } function generaterandomstring($length = 10) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'; $randomstring = ''; ($i = 0; $i < $length; $i++) { $randomstring .= $characters[rand(0, strlen($characters) - 1)]; } homecoming $randomstring; } $ip = generaterandomstring().sha1($_server['server_addr']).generaterandomstring(); $sth->bindparam(':ip', $ip); $data = $con->prepare("insert users (ip) values (':ip')"); $data->execute(); ?>
and yes understand that's not how ip fourth, i'm playing around pdo , mysql. additional tips help me improve code nice, in advance.
you have binfd parameter after query not before
$data = $con->prepare("insert users (ip) values ( :ip )"); $data->bindvalue(':ip', $ip , pdo::param_str); // if ip column string $data->execute();
some ref
php mysql database pdo sql-injection
No comments:
Post a Comment