使用php将数据插入mysql时记录丢失

时间:2013-04-15 23:41:50

标签: php mysql sql database

我试图填充数据库。下面的代码在循环中执行以填充一些相关的表。当循环运行17次时,“pubmedarticle”和“pubmedarticle_belongsto_user”表将填充17条记录。但是,“抽象”表填充了10条记录,其他表填充了16条记录!运行循环没有限制,它运行正好17次,所有表应该在循环后包含17条记录。我无法找到为什么插入数据库时​​有一些缺失。我很感激你的想法。

以下是主键:

pubmedarticle: pubmedArticleId
pubmedarticle_belongsto_user: userId, pubmedArticleId   
article: articleId, pubmedArticleId
abstract: abstractId, articleId , pubmedArticleId
pagination: paginationId, articleId, pubmedArticleId

我检查了mysqli_error并且我收到了这个结果6次(有6个丢失了!):错误描述:你的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以获得正确的语法,以便在迄今为止最全面的地震灾难演习附近使用。第1行的半结构化访谈

这是代码:

    mysqli_query($con,"INSERT INTO pubmedarticle (pubmedId) VALUES ('$pmid')");
//------------
$result_last_row = mysqli_query($con, "SELECT pubmedArticleId from pubmedarticle order by pubmedArticleId desc limit 1");
while($row2 = mysqli_fetch_array($result_last_row)) {
    $last_userID = $row2['pubmedArticleId'];
    }
mysqli_query($con,"INSERT INTO pubmedarticle_belongsto_user (userId, pubmedArticleId) VALUES ('$userId','$last_userID')");
//------------
mysqli_query($con,"INSERT INTO article (pubmedArticleId, articleTitle, articleDate) VALUES ('$last_userID', '$title', '$year_final')");
$result_last_row2 = mysqli_query($con, "SELECT articleId from article order by articleId desc limit 1");
while($row3 = mysqli_fetch_array($result_last_row2)) {
    $last_userID_article = $row3['articleId'];
    }
if (!mysqli_query($con,"INSERT INTO abstract (articleId, pubmedArticleId, abstractText) VALUES ('$last_userID_article','$last_userID', '$abstract')")){

        echo("Error description: " . mysqli_error($con));
        echo "</br>";
        }

mysqli_query($con,"INSERT INTO pagination (articleId, pubmedArticleId, medlinePgn) VALUES ('$last_userID_article','$last_userID', '$medlinepgn')");
mysqli_query($con,"INSERT INTO publicationtype (articleId, pubmedArticleId, publicationType) VALUES ('$last_userID_article','$last_userID', '$publicationtype')");
mysqli_query($con,"INSERT INTO journal (articleId, pubmedArticleId, issn, journalIssuePubDate, volume, issue, journalTitle, isoAbbreviation) 
    VALUES ('$last_userID_article','$last_userID', '$issn', '$year_final','$volume', '$issue', '$journalTitle', '$isoAbbreviation')");

1 个答案:

答案 0 :(得分:0)

从你的评论中提到这个错误的声音:

You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version for the right syntax to use   
near 's most comprehensive earthquake disaster drill to date. Semistructured interview' at  
line 1

听起来你的数据中有撇号,这打破了SQL。

这是使用参数化查询的一个很好的理由(参见here)(这也会阻止SQL注入)。