简体   繁体   中英

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

For example:

array('u_ad'=>'example name','u_mail'=>'example@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM