简体   繁体   中英

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. 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.

记录添加表格

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.

machine_id

here is my record add page ( 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

<?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.

but the result is error. when inserting detail manual in sql, its work. can anyone help me?

here my error result.

记录添加错误

sorry for my bad english.

Try INSERT query like this

$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')";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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