English 中文(简体)
如何使用“oops php”概念插入数据库中的数据?
原标题:How to insert data from database using oops php concepts?
  • 时间:2011-08-11 09:05:08
  •  标签:
  • php

我试图插入一个人的细节,并成功插入。 如果在行文中进行“相同数据”插入3倍。 为什么数据插入3次?

我在数据库中有这一数据。

id      name       dob             gen
1       James     12-03-1977        M
2       James     12-03-1977        M
3       James     12-03-1977        M

PHP级

class Detail
{
function savePerson_detail($vars){
    foreach($vars as $key => $value){
       if(is_numeric($key) && $value >0){
         $qry = sprintf("INSERT INTO cca_student_list(per_name, per_dob, per_gen) VALUES( %s ,  %s ,  %s )",
        mysql_real_escape_string($vars[ name ]),
        mysql_real_escape_string($vars[ dob ]),
        mysql_real_escape_string($vars[ gen ]));
        mysql_query($qry) or die(mysql_error());
         if($qry)
    {
    print  Successfully Insert your details ;
    }
   }
}

Html

<?php
$detail = new Detail();
if(isset($_POST[ btnSaveDetail ])){
   $detail->savePerson_detail($_POST);
}?>
问题回答

您实际操作了三次问询,这就是为什么你三次插入数据。 只是一次问答,应当罚款。

为此,你需要修改你的法典: 首先对投入数据进行全心化,then处理。 您目前正在选取<代码>/$vars(包含三个要素)的每个要素,然后排在each

Do one step after the other:

function savePerson_detail($vars)
{
    // validate function input
    foreach($vars as $key => $value)
    {
       if(!is_numeric($key) || !$value >0)
         return;
    }

    // build sql query
    $qry = sprintf(
        "INSERT INTO cca_student_list(per_name, per_dob, per_gen) VALUES( %s ,  %s ,  %s )",
        mysql_real_escape_string($vars[ name ]),
        mysql_real_escape_string($vars[ dob ]),
        mysql_real_escape_string($vars[ gen ])
    );

    // run sql query
    $result = mysql_query($qry) or die(mysql_error());

    // check query result
    if($result)
    {
        print  Successfully Insert your details ;
    }
}

foreach($vars as $key => $value){

$vars_$_POST通过时,该编码即为之。

$_POST[ name ] =  James ;
$_POST[ dob ] =  12-03-1977 ;
$_POST[ gen ] =  M ;

So it went through each of your $_POST items 3 times. I think you can remove the validation and do it like this.

function savePerson_detail($vars){
  $qry = sprintf("INSERT INTO cca_student_list(per_name, per_dob, per_gen) VALUES( %s ,  %s ,  %s )", mysql_real_escape_string($vars[ name ]), mysql_real_escape_string($vars[ dob ]), mysql_real_escape_string($vars[ gen ]));
  mysql_query($qry) or die(mysql_error());
  if($qry)
    { print  Successfully Insert your details ; }
}

除非我没有东西,否则你又想做些什么?

class Detail
{
function savePerson_detail($vars) {
     foreach($vars as $key => $value) {
          $vars[$key] = mysql_real_escape_string($value);
     }

     if($qry)
     {
       print  Successfully Insert your details ;
     }

   $qry = sprintf("INSERT INTO cca_student_list(per_name, per_dob, per_gen) VALUES( %s ,  %s ,  %s )";
   mysql_query($qry) or die(mysql_error());
}




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签