0

我已经为此工作了两天,阅读了所有内容,但我无法得到答案。提前谢谢你看这个!!!

我有一个加入几个表的查询,它输出我正在寻找的内容这是我的选择

mysql_select_db($database_dbConnect, $dbConnect);
$query_rsMale = "SELECT  r.points, r.license, l.acaNo, l.firstName, l.lastName,   
l.cxage, l.cxcat,l.cxteam, l.city, l.state, l.gender     FROM rankingsM r INNER  
JOIN field f ON  r.license=f.USAC_No INNER JOIN racers l ON  l.usacNo=f.USAC_No
ORDER BY points";
$rsMale = mysql_query($query_rsMale, $dbConnect) or die(mysql_error());
$row_rsMale = mysql_fetch_assoc($rsMale);
$totalRows_rsMale = mysql_num_rows($rsMale);

echo "<br> This is num " . $totalRows_rsMale . "<br>";

我已将其下载到其他页面上的 csv 文件中,并且工作正常。然后我把那个数组放入另一个带有内爆的数组中。

$newData = implode(",",$row_rsMale );
$newData = mysql_real_escape_string($newData);

echo "<br> This is newdata <br>". $newData;

这是我得到 Column not matching 错误的地方。我检查了它们匹配的列,我检查了数据类型,它们匹配。

 $insertSQL ="INSERT INTO ranking (points, usacNo, acaNo, firstName, lastName,
 cxage, cxcat, cxteam, city, state, gender)
 SELECT ( '$newData' )";

 $Result1 = mysql_query($insertSQL, $dbConnect) or die(mysql_error());

 echo "<br>Insert complete!<br>";

接下来我做了列的显示

 $rankingSQL ="Select * from ranking";

 $result = mysql_query($rankingSQL, $dbConnect) or die(mysql_error());
echo "<table><tr>";
for($i = 0; $i < mysql_num_fields($result); $i++) {
$field_info = mysql_fetch_field($result, $i);
echo "<th>{$field_info->name},</th>";
}

这是我网站上我的页面上显示的内容 这是插入之前 这是数字 22

这是新数据 185.34,362355,4174,Shawn,Lortie,47,2,Rally Sport Cycling Team,BOULDER,CO,M points, usacNo, acaNo, firstName, lastName, cxage, cxcat, cxteam, city, state, gender, (为了便于阅读,我在列上添加了逗号。)列数与第 1 行的值计数不匹配

我最终回到了`while ($l = mysql_fetch_array($result)) {

{
  $insertSQL = sprintf("INSERT INTO ranking (points, usacNo, acaNo, firstName,   
lastName, cxage, cxcat, cxteam, city, `state`, gender) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                   GetSQLValueString($l ['points'], "double"),
                   GetSQLValueString($l ['license'], "int"),
                   GetSQLValueString($l ['acaNo'], "int"),
                   GetSQLValueString($l ['firstName'], "text"),
                   GetSQLValueString($l ['lastName'], "text"),
                   GetSQLValueString($l ['cxage'], "int"),
                   GetSQLValueString($l ['cxcat'], "int"),
                   GetSQLValueString($l ['cxteam'], "text"),
                   GetSQLValueString($l ['city'], "text"),
                   GetSQLValueString($l ['state'], "text"),
                   GetSQLValueString($l ['gender'], "text"));

  $Result1 = mysql_query($insertSQL, $dbConnect) or die("<br>Error is " . mysql_error());
}`
4

1 回答 1

0

我认为问题在于以下陈述:

$insertSQL ="INSERT INTO ranking (points, usacNo, acaNo, firstName, lastName,
                                  cxage, cxcat, cxteam, city, state, gender)
             SELECT ( '$newData' )";

一旦 php 用您的数据扩展它,您将拥有以下 SQL 语句:

INSERT INTO ranking (points, usacNo, acaNo, firstName, lastName,
                     cxage, cxcat, cxteam, city, state, gender)
SELECT ( '185.34,362355,4174,Shawn,Lortie,47,2,Rally Sport Cycling Team,BOULDER,CO,M' )

SELECT 正在返回一个字段。因此,您将一个字段提供给具有 11 个字段的插入语句,这会产生错误。

您需要正确地形成插入,可能类似于:

$newData = implode("','",$row_rsMale );
$insertSQL = "INSERT INTO ranking (...) VALUES ('$newData')";

这应该导致以下结果:

INSERT INTO ranking (...) VALUES ('185.34','362355','4174','Shawn','Lortie','47','2','Rally Sport Cycling Team','BOULDER','CO','M')

这不是最佳的,因为它用引号包围数字参数,但也许您的数据库会进行转换。

如果您不必通过 CSV 步骤,那么我会使用子选择进行插入,例如

INSERT INTO ranking (points, usacNo, acaNo, firstName, lastName,
                     cxage, cxcat, cxteam, city, state, gender) 
(SELECT  r.points, r.license, l.acaNo, l.firstName, l.lastName,
                     l.cxage, l.cxcat,l.cxteam, l.city, l.state, l.gender
FROM rankingsM r 
INNER JOIN field f ON r.license=f.USAC_No 
INNER JOIN racers l ON l.usacNo=f.USAC_No
ORDER BY points)

一个操作完成整个操作并返回插入的行数。没有类型争论,并且从数据库/应用程序的角度来看非常有效,因为您不会将所有数据拉回 PHP 只是为了再次将其反馈回来。

于 2012-11-20T17:59:21.780 回答