English 中文(简体)
在Mysql使用Foreach阵列插入多罗
原标题:Insert Multiple Rows in Mysql Using Foreach array

我正试图从允许多种检查箱的形式上插入我的桌子。

这是:

<input type= submit  name= invite-group  value= Invite To Group >
<br />
<?php
$query2aac = mysql_query("SELECT * FROM follow WHERE yoozer1= $userdb1a  AND  followaccept= Yes  ORDER BY yoozer2 DESC");

while($row2aac = mysql_fetch_array($query2aac))
{
    $rowid = $row2aac[ followid ];
    $yoozer2aac = $row2aac[ yoozer2 ];
    $yoozer2aacurl = strtolower($yoozer2aac);
    $palurl =  <a href="http://www.bunchofus.com/fanpage/  . $yoozer2aacurl .  ">  .    $yoozer2aac .  </a> ;
    echo  <input type= "hidden" name = "id[]" value="  . $rowid .  "></input> ;
    echo  <input type= "checkbox" name = "friends[]" value="  . $yoozer2aac .  ">  . $palurl .  </input><br /> ;
}   
?>

This is the php:

if ($submit)
{
    $submit = $_POST[ invite-group ];
    $date1 = date("Y-m-d");
    $lowername = strtolower($username);
    $combined = $_POST[ friends ];

    foreach ($combined as $username) 
    {
        $insert1 = mysql_query("INSERT INTO grprequest VALUES (  , $grpid3 , $username , $myuser , $grprights3 , $grpactive3 )");
    }
}
?>

它正在数据库中插入一条线,但不是多个线。 如果在标书之后我重复使用名称,那么多个用户名称正在显示。

如果任何人都能够帮助,我将不胜感激。

最佳回答

根据你提供的样本代码,以及<代码>(combined)的var,我不肯定为什么不为你工作。 但是,也许你可以尝试这样的事情:

$inserts = array();
foreach ($combined as $username)
    $inserts[] = "(  , $grpid3 , $username , $myuser , $grprights3 , $grpactive3 )";

$query = "INSERT INTO grprequest VALUES ". implode(", ", $inserts);

echo "query = $query"; // for debugging purposes, remove this once it is working
mysql_query($query) or die(mysql_error());

This will perform all of your inserts in one call to the database. I added an echo before performing the query so that you can see the exact query that s being performed.


你们应该明确注意到的一件事是,它像你们的法典一样,很容易遭到排斥。 你们可以找到数百个地方,更多地了解如何保护生态环境,这样就是为了保护生态环境。 我赞成的是Bobby Tables

它也认为你的形式很容易受到特别安全局的攻击。 你从数据库中选择数据,并直接输入超文本,但你应首先使数据制度化(使用<代码>htmlspecialchars)。

问题回答

除<代码>用户名外,是否尝试使用另一个变量? 因为似乎你已经在<代码>底线上使用这一名称: 底线 = 底线(用户名称);。





相关问题
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 ...

热门标签