Hier is het volledige voorbeeld van uw code met behulp van PDO
:
$dbhost = "localhost";
$dbname = "llamabre_farms1";
$dbusername = "llamabre_visitor";
$dbpassword = "llama";
$link = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbusername,$dbpassword);
$statement = $link->prepare("
INSERT INTO farmsdir
(name, owners, location, phone, email, website, description, dateadded, logo, state)
VALUES(:fname, :sowners, :slocation, :sphone, :semail,
:swebsite, :sdescription, :dateadded, :logo, :state)
");
$statement->execute(array(
"fname" => $_POST['name'],
"sowners" => $_POST['owners'],
"slocation" => $_POST['location'],
"sphone" => $_POST['phone'],
"semail" => $_POST['email'],
"swebsite" => $_POST['website'],
"sdescription" => $_POST['description'],
"dateadded" => date('Y-m-d',strtotime($_POST['dateadded'])),
"logo" => $_POST['logo'],
"state" => $_POST['state'],
));
Uitleg:
Prepared statements
wordt gebruikt om uw invoer op te schonen om SQL
te voorkomen injectie.U kunt uw waarde gebruiken zonder enkele of dubbele aanhalingstekens in SQL
verklaring voor bindend.
In Execute
functie kunt u een array doorgeven voor uw SQL statement
met dezelfde indexnaam.
Wat is de reden om PDO of mysqli_* te gebruiken in plaats van mysql_: Omdat de mysql_-extensie verouderd is en niet beschikbaar is in PHP 7.
Kanttekening:
Ik weet dat @Rahautos al de oplossing heeft geleverd om MYSQL
te gebruiken standaard Date Format
('Y-m-d')
zoals ik gebruikte in mijn execution
.