English 中文(简体)
将各种价值观一度纳入多指标类集调查表格
原标题:Inserting values into multiple MySQL tables at once
  • 时间:2011-09-30 20:08:53
  •  标签:
  • php
  • mysql

我建立了小型内容管理系统。 我们现在要提的问题

I m 过滤站,其职能如下:

function filter($data, $db)
{
    $data = trim(htmlentities(strip_tags($data)));
    if (get_magic_quotes_gpc())
    $data = stripslashes($data);
    $data = $db->escape_string($data);
    return $data;
}

而《公共卫生法》则认为

$name=filter($_POST[ name ], $db);
$title=filter($_POST[ title ], $db);
$parent=filter($_POST[ parent ],$db);
$switch=filter($_POST[ switch ], $db);
    if($switch=  ){
        echo "Return back and select an option";
        die();
    }
$parentcheck=filter($_POST[ parentcheck ],$db);
    if($parentcheck== 0 )
    {
        $parent=$parentcheck;
    }   
$purifier = new HTMLPurifier();
$content = $db->real_escape_string( $purifier->purify( $_POST[ content ]) );

if(isset($_POST[ submit ])&&$_POST[ submit ]== Ok ){
    $result=$db->query("INSERT INTO menu (parent, name, showinmenu) VALUES ( $parent ,  $name ,  $switch ") or die($db->error);
    $result2=$db->query("INSERT INTO pages (id, title, content) VALUES ( <what?> ,  $title ,  $content ") or die($db->error);           
    }

我的表象

Table named "pages" enter image description here

而“男性”

“entergraph

www.un.org/Depts/DGACM/index_spanish.htm 我的问题如下:。


  1. I m trying to get autoincremented id value from menu table after ( $parent , $name , $switch ") insertion and set this id in pages table while inserting ($title, $content). How to do it? Is it possible with single query?

  2. $content s value is the text with HTML tags. I m using html purifier. May I filter it s value too before inserting into db table? Any suggestion/advice?

问题回答

$result2=$db->query("INSERT INTO pages (id, title, content) VALUES (LAST_INSERT_ID(),  $title ,  $content ") or die($db->error);

使用真正的“替罪羊”(string)进行追捕应当安全。 是否还有你想要过滤的东西?

看像你重新使用<代码>mysqli作为亚洲开发银行的图书馆,因此,你可以使用<代码>$db->insert_id(<>来检索由该特定亚洲开发银行操作的插入操作所产生的LAST。 因此,您的询问将成为:

$result=$db->query("INSERT INTO menu (parent, name, showinmenu) VALUES ( $parent ,  $name ,  $switch ") or die($db->error);
$new_id = $db->insert_id();
$result2=$db->query("INSERT INTO pages (id, title, content) VALUES ($new_id,  $title ,  $content ") or die($db->error);           
                                                                    ^^^^^^^

您确实可以在单一询问中这样做,因为我的sql在联邦武装部队完成盘问之前没有为插入_id功能提供识别值。 因此,在三个步骤中,你必须这样做:插入、获得援助、再插入。

亚洲开发银行过滤(最好称为“ escaping”)的规则是逃避用户提供的“ANYTH”。 这甚至包括你在其他问答中检索和重新插入的数据。 作为一项安全措施,西班牙确实有资本,因此,它要求确保,你重新将任何资金投入到电梯中,都会把电筒拖到底。 防止注射攻击只是这一副作用。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

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 = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签