dongnianchou7047 2013-04-17 21:18 采纳率: 0%
浏览 72
已采纳

mySQL通过RESULT但INSERT没有发生

I have a form that submits to itself. On submit, a query is generated and run and if ($result) then I make $success = 1 to use in a jQuery function that submits an email and then closes the form. The jQuery function is dependent on the $success value echoed being '1' to trigger. Recently I had someone submit the form, the email was generated, but the insert query did not appear to enter anything into the database.

The PHP code:

require_once ('inc/dbconnect.php');
include ('inc/dataFormat.php');

if (isset($_POST['submit']) && $_POST['submit'] == 'Submit') {

    $qins = sprintf('INSERT INTO becmtrack (siteName, siteNum, location, cluster, enodeName, upgDate, tech, techNum, becm, becmFail, ecm1, ecm2, ecm3, dateAlu, aluContact, aluContNum, trackNum, comment) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)',  [VALUES ARE HERE]);
    $rins = mysqli_query($dB, $qins) or die('Error in query => ' . $qins . '<br />' . mysqli_error($dB));

    $success = ($rins) ? 1 : 0;

}

The jQuery code (inside a document ready function):

if ("<? echo $success; ?>" == 1) {
   var mailBody = 'Site Name: <? echo $_POST['siteName2'];?>';
   mailBody += '%0ASite Number: <? echo $_POST['siteNum'];?>';
   mailBody += '%0ALocation Code: <? echo $_POST['location'];?>';
   mailBody += '%0AeCEMs Removed: <? echo $_POST['ecm1'] . ', ' . $_POST['ecm2'] . ', ' . $_POST['ecm3'];?>';
<? if ($_POST['dateAlu'] != '') { ?>
   mailBody += '%0ADate Cards Returned to Company: <? echo $_POST['dateAlu'];?>';
   mailBody += '%0AALU Contact: <? echo $_POST['aluContact'];?>';
   mailBody += '%0AContact Number: <? echo $_POST['aluContNum'];?>';
   mailBody += '%0AShipping Tracking Number (if applicable): <? echo $_POST['trackNum'];?>';
<? } ?>
$('#reloadTrig',window.opener.document).trigger('click');
window.open('mailto:email.address@somecompany.com?subject=eNodeB%20bCEM%20Upgrade%20Completed&body='+mailBody);
if ('<? echo $_POST['addmore'];?>' != 'addmore') {
window.close();
    }
}

Is there someway for this code to generate a valid result without the data being inserted into the database?

  • 写回答

2条回答 默认 最新

  • douhan1992 2013-04-17 21:35
    关注

    Your query after a successful connection to database is in most of the cases true, according to this documentation:

    http://www.php.net/manual/en/mysqli.query.php

    Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.


    You need to check out if there's really an inserted row.

    According to the documentation here (and a given example)

    http://php.net/manual/en/mysqli.commit.php

    You can check either by mysqli_commit (which in the example fails for duplicated entry, not for syntax error):

    $all_query_ok ? $mysqli->commit() : $mysqli->rollback();

    or via mysqli_affected_rows (which will only be true, if the insert statement really inserts a row)

    $result_query = @mysqli_query($query, $connect);
                if (($result_query == false) &&
                   (mysqli_affected_rows($connect) == 0)) 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站