它不是将数据插入到数据库(It is not inserting data into databas

2019-07-18 19:55发布

我想将数据插入Image_Question表。 但它不执行在插入Image_Question表。

我得到两个错误:

  • 注意:未定义抵消:在... 0上线305
  • 注意:未定义抵消:在... 3线305

这里是你可以使用应用程序: 应用程序

使用的应用程序,看看发生了什么做到以下几点:

  1. 点击Add Question按钮两次追加两种文件输入

  2. 现在上传一次一个文件之一,但像这样做。 上传文件输入一个文件第1行中,然后上传在第2行文件,然后上传1行中的文件,然后再上传最终文件中的第2行。

下面是mysqli的代码:

// Prepare your statements ahead of time
$questionsql = "INSERT INTO Question (QuestionNo) VALUES (?)";
if (!$insert = $mysqli->prepare($questionsql)) {
  // Handle errors with prepare operation here
  echo __LINE__.': '.$mysqli->error;
}

$imagequestionsql = "INSERT INTO Image_Question (ImageId, QuestionId)  
VALUES (?, ?)"; 

if (!$insertimagequestion = $mysqli->prepare($imagequestionsql)) { 
  // Handle errors with prepare operation here 
 echo __LINE__.': '.$mysqli->error; 
}

//make sure both prepared statements succeeded before proceeding
if( $insert && $insertimagequestion) {
  $c = count($_POST['numQuestion']);
  $question_ids = array();

  for($i = 0;  $i < $c; $i++ ) {
    $questionNo = $_POST['numQuestion'][$i];

    $insert->bind_param("i", $questionNo);

    $insert->execute();

    if ($insert->errno) {
        // Handle query error here
      echo __LINE__.': '.$insert->error;
      break 1;
    }

    $questionId = $mysqli->insert_id;

    $question_ids[$questionNo] = $questionId;
  }

  $imgresults = $_POST['imgid'];
  foreach($imgresults as $imgid => $imgvalue) { 
    $image = $imgvalue;
    $imgquesid = $question_ids[$imgid];  //LINE 305 where error is

    foreach($imgvalue as $image) {
      $insertimagequestion->bind_param("ii",$image, $imgquesid); 
      $insertimagequestion->execute();

      if ($insertimagequestion->errno) { 
        // Handle query error here
        echo __LINE__.': '.$insertimagequestion->error;
        break 2;
      }
    }
  }
  $insertimagequestion->close(); 
  $insert->close();
}

?>

下面是代码设置演示:

function GetFormImageCount(){ 
  return $('.imageuploadform').length + 1;
}

var qnum = 1;
var qremain = 5;

function insertQuestion(form) {   

  if (qnum > 5)
    return;

  var $tbody = $('#qandatbl_onthefly > tbody');
  var $tr = $("<tr class='optionAndAnswer' align='center'>");
  var $qid = $("<td width='5%' class='qid'></td>").text(qnum);
  var $image = $("<td width='17%' class='image'></td>"); 

  $('.num_questions').each( function() {

    var $this = $(this);

    var $questionNumber = $("<input type='hidden' class='num_questionsRow'>").attr('name',$this.attr('name')+"[]").attr('value',$this.val());

    $qid.append($questionNumber);                             

    ++qnum;    
    $(".questionNum").text(qnum);
    $(".num_questions").val(qnum);

    --qremain;
    $(".questionRemain").text(qremain);
  });

  var $fileImage = $("<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target_image' onsubmit='return imageClickHandler(this);' class='imageuploadform' >" + 
    "<p class='imagef1_upload_form'><label>" + 
    "Image File: <input name='fileImage' type='file' class='fileImage' /></label><br/><br/><label class='imagelbl'>" + 
    "<input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' /></label>" + 
    "<p class='imagef1_upload_process'>Loading...<br/><img src='Images/loader.gif' /></p>" +
    "<input type='hidden' class='numimage' name='numimage' value='" + GetFormImageCount() + "' />" +
    "</p><p class='imagemsg'></p><p class='listImage'></p>" +
    "<iframe class='upload_target_image' name='upload_target_image' src='/' style='width:0px;height:0px;border:0px;solid;#fff;'></iframe></form>");   

  $image.append($fileImage);

  $tr.append($qid);
  $tr.append($image);  
  $tbody.append($tr);
}

function imageValidation(imageuploadform) {
  var val = $(imageuploadform).find(".fileImage").val();
  switch(val.substring(val.lastIndexOf('.') + 1).toLowerCase()){
    case 'gif':
    case 'jpg': 
    case 'jpeg':
    case 'pjpeg':
    case 'png':
      return true;

    case '':
      $(imageuploadform).find(".fileImage").val();
      alert("To upload an image, please select an Image File");
      return false;

    default:
      alert("To upload an image, please select an Image File");
      return false;
  }
  return false;
}

function htmlEncode(value) { return $('<div/>').text(value).html(); }

function startImageUpload(imageuploadform){
  $(imageuploadform).find('.imagef1_upload_process').show()
  $(imageuploadform).find('.imagef1_upload_form').hide();
  $(imageuploadform).find('.imagemsg').hide();
  sourceImageForm = imageuploadform;

  return true;
}

var imagecounter = 0;

function stopImageUpload(success, imageID, imagefilename) {
  var result = '';
  imagecounter++;

  if (success == 1){
    result = '<span class="imagemsg'+imagecounter+'">The file was uploaded successfully</span>';   
    $('.hiddenimg').eq(window.lastUploadImageIndex).append('<input type="text" name="imgid[]" id="'+imageID+'" value="' + imageID + '" />');
    $('.listImage').eq(window.lastUploadImageIndex).append('<div>' + htmlEncode(imagefilename) + '<button type="button" class="deletefileimage" data-imageID="'+imageID+'"  data-image_file_name="' + imagefilename + '" value="'+imageID+'">Remove</button><br/><hr/></div>');
  }

  $(sourceImageForm).find('.imagef1_upload_process').hide();
  $(sourceImageForm).find('.imagemsg').html(result);
  $(sourceImageForm).find('.imagemsg').show();
  $(sourceImageForm).find(".fileImage").replaceWith("<input type='file' class='fileImage' name='fileImage' />");
  $(sourceImageForm).find('.imagef1_upload_form').show();


  var _imagecounter = imagecounter;

  $('.listImage').eq(window.lastUploadImageIndex).find(".deletefileimage").on("click", function(event) {
    jQuery.ajax("deleteimage.php?imagefilename=" + $(this).attr('data-image_file_name')).done(function(data) {
      $(".imagemsg" + _imagecounter).html(data);
    });

    var buttonid = $(this).attr('value');
    $(this).parent().remove();
    $("#"+ buttonid  +"").remove();
  });

  return true;   
}

function imageClickHandler(imageuploadform){ 
  if(imageValidation(imageuploadform)){ 
    window.lastUploadImageIndex = $('.imageuploadform').index(imageuploadform); 
    return startImageUpload(imageuploadform); 
  }
  return false;
}
</script>
</head>
<body>
  <form id="QandA" action="insertQuestion.php" method="post">
    <table id="question">
      <tr>
        <th colspan="2">
          Question Number <span class="questionNum">1</span>
          <input type="hidden" class="num_questions" name="numQuestion" value="1">
        </th>
      </tr>
    </table>

    <table id="questionBtn" align="center">
      <tr>
        <th>
          <input id="addQuestionBtn" name="addQuestion" type="button" value="Add Question" onClick="insertQuestion(this.form)" />
        </th>
      </tr>
    </table>

  </div><hr/><!-- This Divide close tag has no open tag!-->

  <table id="qandatbl" align="center" cellpadding="0" cellspacing="0" border="0">
    <thead>
      <tr>
        <th width="5%" class="qid">Question Number</th>
        <th width="17%" class="image">Image</th>
      </tr>
    </thead>
  </table>
  <div id="qandatbl_onthefly_container">
    <table id="qandatbl_onthefly" align="center" cellpadding="0" cellspacing="0" border="0">
      <tbody>
      </tbody>
    </table>
  </div>
  <div class="hiddenimg"><!-- All uploaded image file ids go here --></div>
</form>
</body>

通过执行以下操作var_dumps:

   var_dump($question_ids);
    var_dump($imgresults);

我得到这些结果:

     array(2) { 
        [1]=> int(159)
        [2]=> int(160) 
        } 
array(2) { 
[0]=> string(3) "129"
[1]=> string(3) "130" 
}

奇怪的是该号码imgresult启动0 1然后,也不知为什么它说明字符串,它应该是一个int。 不过,如果它是从一个输入来我叫bind_param i还是s在DB的一个int?

额外的细节:

我不明白的是这种mysqli的下面,我生产的答案类似mysqli的,什么是奇怪的是,它插入正确的,即使它从0开始像image_question上面插入:

$results = $_POST['value'];
foreach($results as $id => $value) 
{
    $answer = $value;

    $quesid = $question_ids[$id];   

    foreach($value as $answer) 
    {
        $insertanswer->bind_param("is", $quesid, $answer);

        $insertanswer->execute();

        if ($insertanswer->errno) {
            // Handle query error here
            echo __LINE__.': '.$insertanswer->error;
            break 5;
        }
    }
}

后续代码var_dump($结果)显示了与以往变种转储1个问题:

array(1) { //var_dump($question_ids);
[1]=> int(171) 
} array(1) { //var_dump($imgresults);
[0]=> string(3) "130" 
} array(1) { 
[1]=> array(1) { //var_dump($results);
[0]=> string(1) "B" } } 

Answer 1:

用Firebug我去到HTML和大量挖掘后,(我的意思了很多挖的),我知道你的问题是什么。 不过,让我先说你的常识是正确的,让你了解你需要自己编写的代码。

但无论如何,解决办法是,你需要添加一个多维数组和计数的每一行从0开始,你需要的是这样的:

 var imagecounter = 0;

      var result = '';
      imagecounter++;

....


     $('.hiddenimg').append('<input type="hidden" name="imgid[' + imagecounter + '][]" id="'+imageID+'" value="' + imageID + '" />');

现在你做了这样的事情早些时候与gQuestion++ ,这证明我的观点,你需要开始学习对码。 如果你有几个小时备用刚刚看了你的代码,并试图了解发生了什么。 它只能有利于你:)



Answer 2:

我的答案是不特定的问题,而是在稍宽的话题。

这是关于你一般做你的工作方式。
你需要去改变它。
你看,你的所有问题都的方式大要回答。 并得到更大。 然而,获得较少的答案。
它让你做绝望的事 - 欺骗问题,富饶,账户 - 但一切都是徒劳。 这就是路不通。

你需要改变你正在学习的东西的方式,你想利用这个伟大的网站的方式。
任何人都知道他们正在使用的工具是非常重要的。 你必须自己编写代码,寻求帮助,只有当你在一个真正的解决是。
其结果是,你会明白你的代码。 这是必要的 - 要了解什么是你的代码做。 每行的吧。 所以,你就可以出来的东西自己整理和修复你的代码。
否则,你注定要寻求帮助,为每一个错字,想在这种情况下。

而且,你也不能挖了问题的根源,你必须与你的问题一起发布万吨代码,导致难以回答的问题。
有你的问题三个不同的领域 -的JavaScript,PHP和MySQL。 而问题只属于其中之一。 你必须学会​​你的问题分开,至少在这些领域的限制。
难道JS从客户端发送错误(意外)的数据? 好了,收起所有的PHP和MySQL的代码,只留下JS。 这将帮助Stackoverflowers,甚至自己与调查的问题。
这是数据好吗? 你确定吗? 它打印出来,并确保它正是你所期望的。
如果它的所有权利-转移到PHP的一部分( 但不要忘记从这个问题中删除所有JS)。
等等。

问你的问题不是像“它没有插入”一般条款,但在特定的数据流的术语:“我期待着这样的数据输入程序,但我有另一个。”
一旦你开始使用它,您将不再需要对#1回答:你将能够追查问题最多的地方数据得到意外错误的地方。 所以 - 你找到了错误的地点。

这也是非常重要的运行问题代码。 而在非常环境中,你有你的问题,运行它。
运行代码并检查的实际效果是远远不只是看你的眼睛代码 更高效
甚至还可能有一些情况下是属于你的服务器只和未知的其他人,使他们无法在所有回答你的问题。
此外, 人类不应该在他们的头上运行PHP代码。 它可以工作了几行,但某些时候它在精神上运行所有的代码,并发现错误只是不可能的。
这就是为什么它是如此重要的运行您的代码,并打印出所有的值,函数结果,检查变量状态和这样的 - 和寻找其中的不一致。

当然,使用这种技术,你要明白,这数据是正确的,这是不。
这就是为什么它是如此重要从头开始编写代码,并自己打算了。
如果是你是谁决定的,什么样的数据应该从JS是未来-你可以知道它是好还是坏。 如果是别人 - 你是在一个死胡同,注定要再问一遍,使事情变得更糟。
你需要打破这种恶性循环。
这就是为什么它是如此重要的事情 - 写代码自己,而不是让别人来为你做它。

希望它可以帮助和学习的好运气。



文章来源: It is not inserting data into database