2014-04-25 78 views
1

我有一個表格可以在我的大學進行課程註冊統計,其中一個字段是將表單連接到數據庫的「studentid」似乎是成功的,並且帶有自動增量的主要字段正在工作就好了,但是進入學生證和提交表單失敗,每一次一個新的記錄添加一個「0」值...MySQL數據庫保存0值

這是HTML代碼

 <div class="content"> 

    <form action="connect-mysql.php" method="POST" enctype="text/plain" id="form3" name="form3" method="post"> 

    <table width="100%" border="0"> 
     <tr> 
     <td width="50%" bgcolor="#e3e3e3"><center> 
     Student ID : 
     </center></td> 
     <td width="50%" bgcolor="#e3e3e3"> 
      <span id="sprytextfield1"> 
      <label for="studentid"></label> 
      <input name="studentid" type="text" id="studentid" onblur="MM_validateForm('studentid','','NisNum');return document.MM_returnValue" maxlength="11" /> 
*<span class="textfieldRequiredMsg">A value is required.</span></span> 
     </td> 

PHP代碼

<?php 
$host="fdb7.biz.nf" ;`enter code here` 
$username="1662822_db1" ; 
$password="421343" ; 
$db_name="1662822_db1" ; 
$tbl_name="courses" ; 
    $dbcon = mysqli_connect("$host","$username","$password","$db_name") ;   

    if (!$dbcon) { 
    die('error connecting to database'); } 

    echo 'you have connected sucessfully' ; 

// escape variables for security 
$studentid = mysqli_real_escape_string($dbcon, $_POST['studentid']); 

$sql="INSERT INTO courses (studentid) 
VALUES ('$studentid')"; 

if (!mysqli_query($dbcon,$sql)) { 
die('Error: ' . mysqli_error($dbcon)); 
} 
echo "1 record added"; 
    mysqli_close($dbcon); 
    ?> 
+1

在窗體中取出'enctype =「text/plain」'。 –

+0

你有沒有試過驗證$ _POST ['studentid']能幫助你? –

+0

當你說'每次失敗'時,你的意思是什麼 – JohnnyFaldo

回答

0
method="POST" 

重複形式

檢查studentid這樣:

$studentid = mysqli_real_escape_string($dbcon, $_POST['studentid']); echo $studentid; 

您可以打印$ SQL變量設置之後。

+0

我不確定在整數上使用mysqli_real_escape_string將工作。它會返回一個字符串,MySQL可能會拒絕輸入爲不正確的數據類型。 – dsimer

+0

哦!感謝指出後的重複,遺憾的是,問題仍然存在,無論我在表格中放置的輸入studentid列保存一些隨機數...示例輸入= 1234123123 ...保存= 2147483647 輸入= 12312312312保存= 2147483647 ...相同的數字 – Azizi

+0

@dsimer,這可能是問題,是有道理的。你認爲我應該用escape_integer替換它嗎? – Azizi