数据未插入数据库

时间:2017-04-05 05:09:20

标签: php mysql sql

我为某个项目制作表格。问题是当用户使用该字段输入他们的数据然后提交时,输入不会保留在数据库中。 此外,当点击提交直接页面显示空白空页甚至连接测试也没有显示。 我对其他项目使用几乎相似的代码,除此之外它都有用。 下面是我的代码:

 <?php

    //check connection
    require 'config.php';
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully";

   //asas (table name)
       $id = $_POST["Sid"]; $ic = $_POST["Sic"];
       $name = $_POST["Snp"]; $jant = $_POST["J1"];
       $trum = $_POST["Chr"];$tbim = $_POST["Chp"];
       $mel = $_POST["Sem"]; $arum = $_POST["Ar"];
       $asum = $_POST["As"];

  //institusi
       $thp = $_POST["T1"]; $uni = $_POST["Sis"];
       $bid = $_POST["tpe"];$Aint = $_POST["Ai"];

 //industri
      $bip = $_POST["bid"];$bik = $_POST["B1"];
      $tem = $_POST["te"];$mula = $_POST["tm"];
      $tamm = $_POST["tt"]; $res = $_POST["fileToUpload1"];
      $tran = $_POST["fileToUpload2"];$keb = $_POST["fileToUpload3"];


    $link = mysqli_connect($h,$u,$p,$db);

    if('id' != '$Sid'){
    $asas = "insert into asas Values ('$id','$ic','$name','$jant','$trum','$tbim','$mel','$arum','$asum')";
    $inst = "insert into institusi Values ('$thp','$uni','$bid','$Aint')";
    $indr = "insert into industri Values ('$bip','$bik','$tem','$mula','$tamm','$res','$tran','$keb')";

      mysqli_query($link,$asas);
      mysqli_query($link,$inst);
      mysqli_query($link,$indr);
      mysqli_close($link);
     }
      else
     {
     echo "failed"
     }
    ?>
   <b>Register complete</b>

任何人都可以告诉我错误或可能是一些解决方案。感谢

3 个答案:

答案 0 :(得分:1)

在mysqli_query

之后使用或死亡
var index = 0;
function newitem() {

    index++;
    var objTo = document.getElementById('newitem')
    var divtest = document.createElement("div");
    divtest.setAttribute("class", "form-group removeclass" + index);
    var rdiv = 'removeclass' + index;
    divtest.innerHTML = '<div class="col-sm-3 nopadding"><div class="form-group">
<select class="form-control" id="ddlitem' + index + '"name="ddlitem"><option
value="">--select--</select> </div></div><div class="col-sm-3
nopadding"><div class="form-group"> <input type="text"
class="form-control" id="txtquantity' + index + '" 
name="txtquantity" value=""></div></div><div class="col-sm-3
nopadding"><div class="form-group"> <input type="text"
class="form-control" id="txtprice' + index + ' "name="txtprice"
disabled="disabled" value="" ></div></div><div class="col-sm-3
nopadding"><div class="form-group"><div class="input-group"> <input
type="text" class="form-control" id="txttotal' + index + '"
name="txttotal" disabled="disabled" value="" ><div
class="input-group-btn"> <button class="btn btn-danger"
type="button" onclick="remove_created_item(' + index + ');"> <span
class="glyphicon glyphicon-minus" aria-hidden="true"></span>
</button></div></div></div></div><div class="clear"></div>';
    objTo.appendChild(divtest);
};

function remove_created_item(idOfCreatedItem) {
    $('.removeclass' + idOfCreatedItem).remove();
}

你会知道实际问题是什么

答案 1 :(得分:1)

我认为你在插入查询中遇到问题,请检查一下:

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";

写这种。

谢谢

答案 2 :(得分:1)

 there are few issues with the code like variable id was used without $     
 and need to use die method with mysqli_query() function to check for 
 errors, please check below improved codes, it may help you - 

<?php
    //check connection
    require 'config.php';
   if (isset($_POST)) {
 //asas (table name)
$id   = $_POST["Sid"];
$ic   = $_POST["Sic"];
$name = $_POST["Snp"];
$jant = $_POST["J1"];
$trum = $_POST["Chr"];
$tbim = $_POST["Chp"];
$mel  = $_POST["Sem"];
$arum = $_POST["Ar"];
$asum = $_POST["As"];
//institusi
$thp  = $_POST["T1"];
$uni  = $_POST["Sis"];
$bid  = $_POST["tpe"];
$Aint = $_POST["Ai"];
//industri
$bip  = $_POST["bid"];
$bik  = $_POST["B1"];
$tem  = $_POST["te"];
$mula = $_POST["tm"];
$tamm = $_POST["tt"];
$res  = $_POST["fileToUpload1"];
$tran = $_POST["fileToUpload2"];
$keb  = $_POST["fileToUpload3"];
}
$link = mysqli_connect($h, $u, $p, $db);
if (!$link) {
 die("Connection failed: " . mysqli_connect_error());
}
//   if('id' != '$Sid'){
 if ($id != '$Sid') {
 $asas = "insert into asas Values 
 ('$id','$ic','$name','$jant','$trum','$tbim','$mel','$arum','$asum')";
 $inst = "insert into institusi Values ('$thp','$uni','$bid','$Aint')";
 $indr = "insert into industri Values 
 ('$bip','$bik','$tem','$mula','$tamm','$res','$tran','$keb')";
 if (mysqli_query($link, $asas)) {
    echo "records inserted";
 } else {
    echo "failed".mysqli_error($link) ;
 }
 if (mysqli_query($link, $inst)) {
    echo "records inserted";
 } else {
    echo "failed".mysqli_error($link) ;
 }
 if (mysqli_query($link, $indr)) {
    echo "records inserted";
 } else {
    echo "failed".mysqli_error($link) ;
 }
}
 mysqli_close($link);
?>
 <b>Register complete</b>