如何将$ _post中的数组值存储到db

时间:2013-09-06 11:00:42

标签: php

我添加了一个表格列,用于添加动态行,因此在提交单击时,值应存储在db中, 用PHP代码

if (isset($_POST['submit'])) 
{
foreach($_POST AS $key=>$value)
{
$sql = "INSERT INTO `shows` ( `from` ,  `to` ,  `seats` ,  `cost` ,  `screen`  ) VALUES(  '{$_POST['first_name']}' ,  '{$_POST['last_name']}' ,  '{$_POST['tamil']}' ,  '{$_POST['english']}' ,  '{$_POST['computer']}' ) ";

我正在等待

INSERT INTO `shows` ( `from` , `to` , `seats` , `cost` , `screen` ) VALUES( 'Array' , 'Array' , 'Array' , 'Array' , 'Array' )

所以只有数组变量存储在我的数据库中如何在我的数据库中存储值

提前致谢

Ameeth

1 个答案:

答案 0 :(得分:1)

<?php 
if (isset($_POST['submit'])) {
    foreach($_POST as $varname => $value) {
        $clean = trim($value);
        $clean = striptags($clean);
        $clean = mysql_real_escape_string($clean); // use inplace of addslashes
        //create sanitized array
        $cleanVars[$varname] = $clean;
    } 

}
$sql = 'INSERT INTO `shows` ( from ,  to,  seats ,  cost ,  screen  ) VALUES('. $cleanVars["first_name"].','. $cleanVars["last_name"].','. $cleanVars["english"].','. $cleanVars["tamil"].','. $cleanVars["computer"].' ) ';
?>