dongxiong1935 2013-04-15 23:41 采纳率: 0%
浏览 46
已采纳

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

I ma trying to populate a db. The below code is executed in a loop to fill some related tables. When the loop runs 17 times, the "pubmedarticle" and "pubmedarticle_belongsto_user" table are populated with 17 records. However the "abstract" table is populated with 10 records and the other tables with 16 records! There is no restriction in running the loop and it runs exactly 17 times and all the tables should contain 17 records after the loop. I cannot find why there is some missing in the inserting into the db. I appreciate your thoughts.

Here are the primary keys:

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

I checked mysqli_error and I recieve this result 6 times (there is 6 missing!): Error description: 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

And this is the code:

    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条回答 默认 最新

  • dongyin6576 2013-04-16 01:09
    关注

    From the sounds of your comment mentioning this error:

    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
    

    It sounds like you have apostrophe's in your data which is breaking the SQL.

    This is a good reason to use parameterised queries (see here) (which will also prevent SQL injection).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab