douxian3828
2018-06-04 16:10
浏览 57
已采纳

mysql中的多个插入输入数据

I have some problem with multiple input $_POST into sql but I can't see the $_POST, here my code :

Html Code:

<form action="inc/barangproses.php" method="post" class="modal-content form-horizontal" id="updateallform">
<input id="CPU0014" name="noresi[]" value="CPU0014" class="hidden" type="text">
<input id="CPU0017" name="noresi[]" value="CPU0017" class="hidden" type="text">
<input id="CPU0020" name="noresi[]" value="CPU0020" class="hidden" type="text">
<button type="submit" class="btn btn-success" name="updateallbarang">Update</button>
</form>

PHP Code:

    if (isset($_POST['updateallbarang'])) {
    $resi           = $_POST['noresi'];
    $idstatus       = '3';
    $sql = "";
    foreach($resi as $noresi ){
    $sql .= "INSERT INTO tracking(NORESI)
                VALUES ('$noresi');";
    $sql .= "INSERT INTO logkiriman(NORESI)
                VALUES ('$noresi');";
    $sql .= "UPDATE kiriman SET IDSTATUS = '$idstatus' WHERE NORESI = '$noresi';";
    }
    multiquery_db($sql);
    }
    else {
        header('location: ../barang.php?error='.base64_encode('Pilih kiriman terlebih dahulu.'));
        }
    }

Config Code:

$db_host        = "localhost";
$db_user        = "root";
$db_password    = "";
$db_name        = "db";
$dir            = "http://localhost/admin/";

$konek      = mysqli_connect($db_host,$db_user,$db_password,$db_name);

    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

function multiquery_db($query){
  global $konek;
  $result = mysqli_multi_query($konek, $query);
  return $result;
  mysqli_free_result($konek);
  mysqli_close($konek);
  }

Sql Result not show $noresi :

INSERT INTO tracking(NORESI) VALUES ('');
INSERT INTO logkiriman(NORESI) VALUES ('');
UPDATE kiriman SET IDSTATUS = '3' WHERE NORESI = '';
INSERT INTO tracking(NORESI) VALUES ('');
INSERT INTO logkiriman(NORESI) VALUES ('');
UPDATE kiriman SET IDSTATUS = '3' WHERE NORESI = '';
INSERT INTO tracking(NORESI) VALUES ('');
INSERT INTO logkiriman(NORESI) VALUES ('');
UPDATE kiriman SET IDSTATUS = '3' WHERE NORESI = '';

Please tell me where the wrong code ? I want to input multiple $_POST['noresi'] with multiple query into MySQL

</div>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • douxin8610 2018-06-04 16:32
    最佳回答

    Why not in the foreach loop, execute each query and test this way first?

    Example

    var_dump($resi);
    foreach($resi as $noresi ){     
    mysqli_query("INSERT INTO tracking(NORESI)  VALUES ('$noresi')");
    mysqli_query("INSERT INTO logkiriman(NORESI)                VALUES ('$noresi')"); 
    mysqli_query("UPDATE kiriman SET IDSTATUS = '$idstatus' WHERE NORESI    = '$noresi'");
    }
    

    EDIT The purpose of var_dump here is to echo the contents of the array to the screen, this way you can see if you have what you expect in $resi

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题