0

我试图内爆一些变量并将它们插入到 MySql 数据库中,但由于某种原因它不起作用。我已经尝试了几个小时,但我只是不确定我做错了什么。如果您能提供帮助,将不胜感激。

$AddressString = "address1,address2,address3,address5,postcode";

$AddressSplit = explode( ",", $AddressString );    //split the address string
$StringLength = count( $AddressSplit ) - 1;
$s = 0;      //trim any white spaces from the address string
while ( $s < count( $AddressSplit ) ) {
  $AddressSplit[$s] = trim( $AddressSplit[$s] );
  $s++;
}

//Create the Values to insert into DB
$MysqlValues = implode( "','", $AddressSplit );
$MysqlValues = "'$MysqlValues'";
$NumberVals = count( $AddressSplit );

$t = 1;
while ( $t < $NumberVals ) {
  $ad[$i] = "add$i";
  $t++;
}
$TableNames = implode( ", ", $ad );

mysql_query( "INSERT INTO pstc_add_main (" . $TableNames . ",add10,date)
                                VALUES (" . $MysqlValues . ",'$cdate')" );
}
4

3 回答 3

0

因为您开始使字段名称基于 1,所以您是一个字段短!最后,您必须以相同数量的字段和值结束。

试试这个:

$t = 0;
while ( $t < $NumberVals ) {
  $ad[$i] = "add$i";
  $t++;
}

或者,如果您不希望第一个字段是“添加”,请按如下方式更改:

$t = 1;
while ( $t <= $NumberVals ) {
  $ad[$i] = "add$i";
  $t++;
}

当然,这将是一个简单的测试:

$sql = "INSERT INTO pstc_add_main (" . $TableNames . ",add10,date)
                           VALUES (" . $MysqlValues . ",'$cdate')";
var_dump($sql);
mysql_query($sql);
于 2012-09-08T11:41:45.417 回答
0

未经测试,

我怀疑你会改变,

   $MysqlValues = implode("','", $AddressSplit);

  $MysqlValues = implode(",", $AddressSplit);
于 2012-09-08T11:41:48.727 回答
0

只需使用

$MysqlValues = implode( ",", $AddressSplit );

并尝试编辑代码

mysql_query( "INSERT INTO pstc_add_main (".$TableNames." ,add10,date)
                            VALUES (" . $MysqlValues . ",$cdate)" );
于 2012-09-08T11:48:44.687 回答