I am trying to learn how to use PDO so my forms aren't SQL injected. I am confused on how to code the execute function for my insert.
This is my code:
$db = new PDO('mysql:host=x;dbname=x;charset=utf8', 'x', 'x');
if ( !$db )
{
die('Could not connect: ' . mysql_error());
}
$ipaddress = $_SERVER['REMOTE_ADDR'];
$mail = $_POST['mail'];
$stmt = $db->prepare("SELECT * FROM ucm_signup WHERE email =? ") or exit(mysql_error());
$stmt->bindValue(1, $mail, PDO::PARAM_STR);
$stmt->execute();
$num_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
if($num_rows == 0)
{
//if there are no duplicates...insert
$sql = $db->prepare("INSERT INTO ucm_signup company, address1, address2, city, province, zip, fname, lname, email, phone, session, iama, buyfrom, group1, ipaddress )
VALUES ( '$_POST[company]','$_POST[address1]','$_POST[address2]','$_POST[city]', '$_POST[province]','$_POST[zip]','$_POST[fname]','$_POST[lname]','$_POST[mail]', '$_POST[phone]','$_POST[session]','$_POST[iama]','$_POST[buyfrom]','$_POST[group1]', '$ipaddress')");
$sql->execute(array(':company' => I AM LOST))
}
Am I even close on the INSERT ? Thank you
$_POST[fields]in theexecute(array(...)), associate them to :placeholder names, which you put in the INSERT statement in place of the quoted literal variables.T.Here, read thisVALUES(:foo)thenexecute(array(":foo" => "bar"))wherebaris$_POST['bar']in your case..$field1is to be read between the lines. You need to do$field1=$_POST['company'];as an example. You don't do the INSERT that way for your VALUES. @DDDD