I have an insert command based on the following:
$baseINS = "INSERT INTO table2 (Points_ID, StaticCode) VALUES ";
$arrayINS = explode(", ", $arraystring);
foreach ($arrayINS as &$array1INS) {
$array1INS = "('" . $array1INS . "', '123456')";
}
$arrayvaluesINS = implode(', ', $arrayINS);
$insertSQL2 = $baseINS . $arrayvaluesINS;
The $insertSQL2
is queried from the following transaction:
$insertSQL = "BEGIN";
mysql_query($insertSQL) or die (mysql_error());
$insertSQL = $insertSQL1;
mysql_query($insertSQL) or die (mysql_error());
$insertSQL = $insertSQL2;
mysql_query($insertSQL) or die (mysql_error());
$insertSQL = "COMMIT";
mysql_query($insertSQL) or die (mysql_error());
(where $insertSQL1
is another SQL insert, to another table)
Table structure as follows:
table2
(ID
int(50) NOT NULL AUTO_INCREMENT,
StaticCode
varchar(100) NOT NULL,Points_ID
varchar(50) NOT NULL, PRIMARY KEY (ID
), UNIQUE KEYID
(ID
) ) ENGINE=InnoDB
Now, let's say for example that $insertSQL2
echos as INSERT INTO table2 (Points_ID, StaticCode) VALUES ('24859', '123456'), ('24649', '123456'), ('25166', '123456')
I would expect three rows to appear in the table2 ...but this is what happens:
- Both the insert commands (
$insertSQL1
and$insertSQL2
) do, in fact, insert data. - It's just that, while
$insertSQL1
inserts all of the data from all of the form to the required fields in table1, the$insertSQL2
(which is inserting the array) doesn't - it just inserts the static code, once, and leaves the Points_ID blank.
I have tested the INSERT statement by just copying the echoed command into PHPMySQL, and it does exactly what it is supposed to, so I know that my version of MySQL can handle it.
Any ideas why?
Points_ID
a foreign key field? do the matching foreign records exist? Your code is also vulnerable to SQL injection, you're using a very dangerous foreach() construct, and your actual query calls are needlessly verbose. e.g. clean up your code and maybe things will start working. – Marc B Jan 23 '13 at 15:18table2
(ID
int(50) NOT NULL AUTO_INCREMENT,StaticCode
varchar(100) NOT NULL,Points_ID
varchar(50) NOT NULL, PRIMARY KEY (ID
), UNIQUE KEYID
(ID
) ) ENGINE=InnoDB ` – user1259798 Jan 23 '13 at 15:24