我有一些代码如下所示,它将一些数据插入到sql表中。我尝试输入的数组工作正常,我还在插入代码之前进行了额外的检查,以正确显示数组$array_author[$i]
我真的不明白为什么product_rating
表的插入代码无法正常工作。
// Firstly the Author data is used to make a new user in the users table.
for($i = 0; $i<count($array_review); $i++){
$queryInsertUser=mysqli_query($link,"INSERT INTO `users`
(`Name`, `DOB`, `Sex`, `Email`, `Address`, `AddedDate`, `AddedFrom`,`Crawler`)
VALUES ('$array_author[$i]', '$dob', '$sex', '$email', '$address', NOW(), '$http_referrer',2)");
// Review and rating data is inserted into the product_rating table. The user created from the author data
//is then inserted into the product_rating table.
$selectuser = mysqli_query($link, " SELECT `UserID` FROM `users` ORDER BY `UserID` DESC LIMIT 1 ");
while ($rowGetDetails = mysqli_fetch_array($selectuser)){
echo '<br>';
echo $array_review[$i];
$sql2 = "INSERT INTO product_rating (ProductID,Rating,AddedDate,Comment,AddedBy,AddedFrom,crawler)
VALUES ('$ProductID','$array_rating2[$i]',NOW(),'$array_review[$i]','$UserID','".$_SERVER['REMOTE_ADDR']."','2')";
$insertcomment = mysqli_query($link, $sql2);
}
}
此代码在第一个表(users
)中正确插入。
我在没有$array_author[$i]
的情况下尝试了此代码,并在product_rating
表中正确插入了新记录。
抱歉array_author
在代码中定义得更高。我可以在下面填写array_author
,array_rating2
和array_review
:
$array_author = array();
$pos = -1;
while(FALSE != $pos = strpos($str_test, "<span itemprop=\"author\">",$pos+1)) {
$pos2 = substr($str_test, $pos+24);
$pos3 = strpos($pos2, "</span>");
$author= substr($pos2, 0, $pos3);
$author=ltrim ($author);
$author=rtrim ($author);
$array_author[ ] = $author;
}
$array_review = array();
$pos3 = -1;
while (FALSE != $pos3 = strpos($str_test, "<span itemprop=\"description\">",$pos3 + 1)){
$pos4= substr($str_test, $pos3+30);
$pos5 = strpos($pos4, "</span>");
$review= substr($pos4, 0, $pos5);
$review=ltrim ($review);
$review=rtrim ($review);
$array_review[ ] = $review;
}
$array_rating = array();
$pos3 = -1;
while (FALSE != $pos3 = strpos($str_test, "<span itemprop=\"ratingValue\">", $pos3 + 1)){
$pos4= substr($str_test, $pos3+29);
$pos5 = strpos($pos4, "</span>");
$rating= substr($pos4, 0, $pos5);
$rating=ltrim ($rating);
$rating=rtrim ($rating);
$array_rating[] = $rating;
}