我试图破坏一些变量并将它们插入到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')" );
}
答案 0 :(得分:0)
因为您开始使用字段名称1,所以您的字段很短! 最后,您必须以相同数量的字段和值结束。
试试这个:
$t = 0;
while ( $t < $NumberVals ) {
$ad[$i] = "add$i";
$t++;
}
或者,如果您不希望第一个字段为“add”,请将其更改为:
$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);
答案 1 :(得分:0)
未经测试,
我怀疑你改变了,
$MysqlValues = implode("','", $AddressSplit);
到
$MysqlValues = implode(",", $AddressSplit);
答案 2 :(得分:0)
只需使用
$MysqlValues = implode( ",", $AddressSplit );
并尝试编辑代码
mysql_query( "INSERT INTO pstc_add_main (".$TableNames." ,add10,date)
VALUES (" . $MysqlValues . ",$cdate)" );