使用php表单将数据插入sql中的表时出错

时间:2014-04-18 03:02:53

标签: php mysql sql

我正在创建资产数据库系统(只是一个离线系统,未连接到互联网),它将显示所有资产列表。在列表中,我可以点击任何资产来查看详细信息。我也设法更新细节或删除资产。但是当它转到资产记录部分时,它会在使用表单将记录插入资产时出错。

这是我的记录添加表单。我也想在表格中的机器ID字段下显示机器ID,但我还不知道如何将数据放在那里。

record add form

用于插入记录,它会将rekod_add.php(记录添加)url地址上的机器ID从资产表捕获到传递给rekod_tab表。

machine_id

这是我的记录添加页面(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>

这是我的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>

用户完成插入记录详细信息后,表单将在rekod_tab表中添加记录,包括机器ID(id_mesin),它会像我之前说的那样从url中自动捕获。

但结果是错误。当在sql中插入详细手册时,它的工作。有谁可以帮助我?

这是我的错误结果。

record add error

抱歉我的英语不好。

1 个答案:

答案 0 :(得分:0)

尝试像这样的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')";