2014-04-18 45 views
0

我正在創建將顯示所有資產列表的資產數據庫系統(只是一個離線系統,未連接到互聯網)。在列表中,我可以點擊任何資產查看詳細信息。另外我還設法更新細節或刪除資產。但是當它轉到資產記錄部件時,它會在使用表單將記錄插入資產時發生錯誤。使用php表格在sql中插入數據時出錯

這是我的記錄添加表單。我也想讓機器ID在機器ID字段的形式下可見,但我還沒有知道如何把數據放在那裏。

record add form

插入記錄,其將捕獲的機器ID上rekod_add.php(記錄添加)地址從資產表地址突入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

對不起我的英文不好。

+0

所有你的價值觀'$'id_mesin''等嘗試INSERT查詢有變量外的'$'跡象。 –

+0

什麼是'$'id_mesin''?從來沒有見過任何人使用這個...應該是''$ id_mesin'' – Darren

+0

只是通知我把'''標誌放在錯誤的地方。已經改爲''$ id_mesin'' – newbie

回答

0

這樣

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

已經嘗試了你的方法。仍然給出錯誤結果。 – newbie

+0

並且結果是? – Jonast92

+0

@ Jonast92,結果與我發佈的問題的錯誤結果相同。 – newbie

相關問題