gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:22:11 25 4
gpt4 key购买 nike

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

这是我的记录添加表单。我还想让机器 ID 在表单中的“机器 ID”字段下可见,但我还不知道如何将数据放在那里。

record add form

对于插入记录,它会从 asset 表中捕获 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

抱歉我的英语不好。

最佳答案

尝试像这样插入查询

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

关于php - 使用php表单向sql中的表插入数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23146740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com