开发者

How to save the data in the array directly to the database? with php

开发者 https://www.devze.com 2023-03-17 17:07 出处:网络
For example: array(\'u_ad\'=>\'example name\',\'u_mail\'=>\'exam开发者_如何学Cple@mail.com\',\'u_sifre\'=>\'exapmlepass\')

For example:

array('u_ad'=>'example name','u_mail'=>'exam开发者_如何学Cple@mail.com','u_sifre'=>'exapmlepass')

Required query:

$sql = "INSERT INTO uyeler 
          (u_ad,u_mail,u_sifre) 
        VALUES 
          ('example name','example@mail.com','examplepass')";

How I do that?


$sql = "INSERT INTO uyeler (". implode(",", array_keys($array)) .") VALUES ('". implode("','", $array) ."')";


Quick/dirty/unsafe:

$sql = "INSERT INTO uyeler (u_ad,u_mail,u_sifre) VALUES ('" . $theArray['u_ad'] . "','" . $theArray['u_mail'] . "','" . $theArray['u_sifre'] . "')";

Better:

$ad = mysql_real_escape_string($theArray['u_ad']);
$mail = mysql_real_escape_string($theArray['u_mail']);
$sifre = mysql_real_escape_string($theArray['u_sifre']);

$sql = "INSERT INTO uyeler (u_ad,u_mail,u_sifre) VALUES ('" . $ad . "','" . $mail . "','" . $sifre . "')";


Don't mess around with escaping! You should be using prepared statements where possible, and using PDO is a good way to do it.

See:

Why you Should be using PHP’s PDO for Database Access
ext/mysqli: Part I - Overview and Prepared Statements

0

精彩评论

暂无评论...
验证码 换一张
取 消