简体   繁体   English

使用PHP表单在SQL中将数据插入表中时出错

[英]Error when inserting data into table in sql using php form

i'm creating asset database system (just an offline system, not connected to internet) that will show all assets list. 我正在创建资产数据库系统(只是一个脱机系统,未连接到Internet),它将显示所有资产列表。 on the list, i can click on any asset to view the details. 在列表中,我可以单击任何资产以查看详细信息。 also i'm manage to update the details or delete the asset. 我也设法更新细节或删除资产。 but when it goes to asset record parts, it give an error on inserting record to asset using a form. 但是当涉及资产记录部分时,使用表格将记录插入资产时会出错。

here is my record add form. 这是我的记录添加表格。 and i'm also wanna make machine id visible under MACHINE ID field in the form, but i did't know yet how to put the data there. 而且我也想让机器ID在表单的“机器ID”字段下可见,但是我还不知道如何将数据放入那里。

记录添加表格

for inserting record, its will capture machine id on rekod_add.php ( record add) url address from asset table to passing into rekod_tab table. 对于插入记录,它会捕获从资产表到传递到rekod_tab表中的rekod_add.php(添加记录)URL地址上的机器ID。

machine_id

here is my record add page ( rekod_add.php ) 这是我的记录添加页面(rekod_add.php)

<?php

//Start session
session_start();

//Check whether the session variable SESS_MEMBER_ID is present or not
if(!isset($_SESSION['username']) || (trim($_SESSION['password']) == '')) {
    header("location: login.php");
    exit();
}

?>
<html>
<head>
<title>EXA_mySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
body,td,th {
    font-family: Tahoma, Geneva, sans-serif;
}
</style>
</head>

<body>
<script type="text/javascript">function checkinput() { 
    var id_mesin = document.getElementById('id_mesin').value;  
    if(!id_mesin.match(/\S/)) {
        alert ('Please enter Machine ID');
        return false;
    } else {
        return true;
    }
}
</script>
<?php

$con=mysqli_connect("localhost","root","admin","exa");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$id_mesin = $_POST['id_mesin'];
$query = "SELECT * FROM asset WHERE id_mesin ='".$id_mesin."'";
$result = mysqli_query($con,$query);
$rows = mysqli_fetch_array($result);
?>
<table width="733" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form_insert" method="post" action="rekod_add_ac.php">
  <table width="709" border="0" align="center">
    <tr>
      <th width="23" scope="col">MACHINE ID</th>
      <th colspan="2" scope="col">DATE</th>
      <th width="68" scope="col">TIME</th>
      <th width="175" scope="col">RECEIVE CALL BY</th>
      <th width="97" scope="col">CURRENT METER</th>
      <th width="90" scope="col">LAST METER</th>
      <th width="136" scope="col">J.SHEET NO</th>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td colspan="2"><input name="tarikh_rekod" type="text" id="tarikh_rekod" size="15" /></td>
      <td><input name="time" type="text" id="time" size="10" maxlength="9" /></td>
      <td><input type="text" name="call_by" id="call_by" /></td>
      <td><input name="meter_semasa" type="text" id="meter_semasa" size="15" /></td>
      <td><input name="meter_last" type="text" id="meter_last" size="15" /></td>
      <td><input name="rujukan" type="text" id="rujukan" size="10" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <th width="81">PROBLEM</th>
      <th width="5">:</th>
      <td colspan="3"><textarea name="masalah" id="masalah" cols="55" rows="5"></textarea></td>
      <th colspan="2" rowspan="2"><p>REMARK</p>
        <p>
          <textarea name="remark" cols="30" rows="6" id="remark"></textarea>
        </p></th>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <th>SOLUTION</th>
      <th>:</th>
      <td colspan="3"><textarea name="solution" id="solution" cols="55" rows="5"></textarea></td>
    </tr>
    <tr>
      <td colspan="8" align="right"><?php echo "<input type='hidden' value='" . $rows['id_mesin'] . "' name='id_mesin'>"; echo "<input type='submit' value='Add Record'>";?></td>
    </tr>
  </table>


</form>
</td>
</tr>
</table>
<?php
mysqli_close($con);
?>
</body>
</html>

here is my rekod_add_ac.php 这是我的rekod_add_ac.php

<?php
session_start();

if(!isset($_SESSION['username']) || (trim($_SESSION['password']) == '')) {
    header("location: login.php");
    exit();
}
?>
<html>
<head>
<title>EXA_mySQL</title>
<script type="text/javascript">
<!--
function CloseWindow() {
    window.close(); 
    window.opener.location.reload();
}

//-->
</script>
</head>

<body>

<?php
error_reporting(E_ALL);
ini_set('display_errors','on');

$con=mysqli_connect("localhost","root","admin","exa");

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

$id_mesin=$_POST['id_mesin'];    
$tarikh_rekod=$_POST['tarikh_rekod'];
$time=$_POST['time'];
$call_by=$_POST['call_by'];
$meter_semasa=$_POST['meter_semasa'];
$meter_last=$_POST['meter_last'];
$rujukan=$_POST['rujukan'];
$masalah=$_POST['masalah'];
$solution=$_POST['solution'];
$remark=$_POST['remark'];

$rekod_in="INSERT INTO rekod_tab ( id_mesin, tarikh_rekod, time, call_by, meter_semasa, meter_last, rujukan, masalah, solution, remark) VALUES ( $'id_mesin', $'tarikh_rekod', $'time', $'call_by', $'meter_semasa', $'meter_last', $'rujukan', $'masalah', $'solution', $'remark')";
$result=mysqli_query($con, $rekod_in);

if($result){

echo "Successful";
echo "<BR>";
echo "<th><form>";
echo "<input type='button' onClick='CloseWindow()' value='Back to Exa_mySQL' align='middle'>";
echo "</form></th>";

}   
else {
echo "Data error, please recheck before submit.";
echo "<BR>";
echo "Click back to add record.";
echo "<BR>";
echo "<form action='rekod_add.php?id=$id_mesin' method='post'>";
echo "<td><input type='hidden' value='$id_mesin' name='id_mesin'>";
echo "<input type='submit' value='Back'></td>";
echo "</form>";
echo "<th><form>";
}

mysqli_close($con);

?>
</body>
</html>

after user done inserting record details, the form will add the record on rekod_tab table including machine id ( id_mesin ) which automatically capture from url like i said before. 用户完成记录详细信息的插入之后,表单将在rekod_tab表中添加记录,其中包括机器ID( id_mesin ),该机器ID会像我之前所说的那样从url自动捕获。

but the result is error. 但结果是错误。 when inserting detail manual in sql, its work. 在sql中插入详细手册时,其工作原理。 can anyone help me? 谁能帮我?

here my error result. 这是我的错误结果。

记录添加错误

sorry for my bad english. 对不起,我的英语不好。

Try INSERT query like this 试试这样的INSERT查询

$rekod_in="INSERT INTO rekod_tab 
( id_mesin, tarikh_rekod, time, call_by, meter_semasa, meter_last, 
 rujukan, masalah, solution, remark) 
 VALUES ( '$id_mesin', '$tarikh_rekod', '$time', '$call_by', '$meter_semasa',
'$meter_last', '$rujukan', '$masalah', '$solution', '$remark')";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM