2016-12-31 45 views
0

使用PHP的Im & MySQL,使用"mysql_fetch_array"即時面對INSERT數據的問題。使用「mysql_fetch_array」錯誤INSERT數據

這是我的連接到Mysql和我的查詢來顯示數據。

這裏有2張表。 1個表格用於顯示。另外1個用於插入數據。

<?php 
 

 
$host="localhost"; // Host name 
 
$username="root"; // Mysql username 
 
$password=""; // Mysql password 
 
$db_name="skpj"; // Database name 
 

 
// Connect to server and select databse. 
 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
 
mysql_select_db("$db_name")or die("cannot select DB"); 
 

 
$sql="SELECT * FROM `student` WHERE cls_id = '13' "; 
 
$result=mysql_query($sql); 
 

 
// Count table rows 
 
$count=mysql_num_rows($result); 
 
?>

<form name="form1" method="post" action=""> 
 
    <?php while($rows=mysql_fetch_array($result)){ ?> 
 

 
    <?php echo $rows['s_no']; ?> 
 

 
    <?php echo $rows['name']; ?> 
 
    <input name="s_no[]" type="hidden" id="name" 
 
value="<?php echo $rows['s_no']; ?>"> 
 

 
    <?php echo $rows['ic']; ?> 
 

 
    <?php echo $rows['cls_id']; ?> 
 
    <input name="class_n[]" type="hidden" id="cls_id" 
 
value="<?php echo $rows['cls_id']; ?>"> 
 

 
    <select name="att[]" id="att" style=" width:80px" > 
 
     <option value="1">Atten</option> 
 
     <option value="2">Absend</option> 
 
     <option value="3">MC</option> 
 
    </select> 
 
    <input name="tmp[]" type="hidden" id="name" value="1"> 
 
    <?php } ?> 
 

 
    <input type="submit" name="submit" value="submit"></td> 
 
</form> 
 

 
<?php 
 

 
if($submit){ 
 
    for($i=0;$i<$count;$i++){ 
 
     $sql1="INSERT INTO attendance (s_no, class_n, att, tmp) 
 
     VALUE '$s_no[i]','$class_n[i]','$att[i]','$tmp[i]' "; 
 
     $result1=mysql_query($sql1); 
 
    } 
 
} 
 

 
if($result1){ 
 
    header("location:att2.php"); 
 
} 
 
mysql_close(); 
 
?>

下面

是通知敵人我的錯誤。

SCREAM:錯誤抑制忽略

說明:未定義變量:提交在C:\瓦帕\ WWW \ ATT \ att2.php上線67

說明:未定義變量:RESULT1用C :\ WAMP \ WWW \上線ATT \ att2.php 74

此外,我不能插入我的數據

This is the error massage

+0

變化'如果($提交)''來,如果(isset($ _ POST [ 「提交」]))'。 – birraa

+0

仍然有錯誤。這次注意:未定義的變量:s_no,class_n,att,tmp – Ameer

+0

因爲您必須使用$ _POST []數組來訪問這些表單值。 – birraa

回答

-1

試試這個:

$sno = mysql_real_escape_string($_POST['s_no'][i]); 
$cls = mysql_real_escape_string($_POST['class_n'][i]); 
$att = mysql_real_escape_string($_POST‌​['att'][i]); 
$tmp = mysql_real_escape_string($_P‌​OST['tmp'][i]); 
$sql1="INSERT INTO attendance (s_no, class_n, att, tmp) VALUES ('$sno', '$cls', '$att', '$tmp')"; 
+0

以及它的工作..沒有錯誤。也要插入數據。但所有數據0 0 0 0. – Ameer

+0

您必須檢查表單是否正確傳遞數據。去查看源代碼並檢查瀏覽器。 – birraa

0

這是我在它刺傷....讓我知道,如果這有助於。我用一種對我有意義的方法重新組織它。再一次,你真的應該更好地鎖定這些代碼。我只是在這裏與失眠做鬥爭......我不認爲我可以爲這篇文章貢獻太多。

<?php 
    #Use Object-Oriented Programming to open DB 
    $mysqli = new mysqli("$host", "$username", "$password", "$db_name"); 
    if ($mysqli->connect_error) { 
     die('Connect Error: ' . $mysqli->connect_error); 
    } 
    $students=$mysqli->prepare("SELECT * FROM `student` WHERE cls_id = '13'"); 
    $students->execute(); 
    $students->store_result(); 
    $studentslist = $students->fetch_array(MYSQLI_ASSOC); 
    $count = $stmt->num_rows 
?> 

格式文件...

<form name="form1" method="post" action=""> 
    <?php 
     foreach ($studentslist as $row=>$rows) { 
      echo "<tr>" 
      echo "<td>$rows['s_no']</td>"; 
      echo "<td>$rows['name']</td>"; 
      echo "<td>$rows['ic']</td>"; 
      echo "<td>$rows['cls_id']</td>"; 
      echo "<td><select name='att[]' id='att' style=' width:80px'>"; 
      echo " <option value='1'>Atten</option>"; 
      echo " <option value='2'>Absend</option>"; 
      echo " <option value='3'>MC</option>"; 
      echo "</select></td>"; 
      echo "<input name='class_n[]' type='hidden' id='cls_id' value='$rows['cls_id']>"; 
      echo "<input name='s_no[]' type='hidden' id='name' value='$rows['s_no']>"; 
      echo "<input name='tmp[]' type='hidden' id='name' value='1'>";//???? I DONT UNDERSTAND THIS VALUE 
     } 
     $students->close; 
    ?> 
    <input type='submit' name='submit' value='submit'></td> 
</form> 

<?php 
    #SECURE YOUR INPUT/POST PARAMETERS TO ONLY ALLOW FROM THE SERVER ITSELF. 
    #JUST CHECKING FOR SUBMIT FOR NOW 
    if(isset($_POST["submit"])) { 
     $sno = $_POST['s_no'];//CAPTURED ARRAY 
     $cls = $_POST['class_n'];//CAPTURED ARRAY 
     $att = $_POST‌​['att']);//CAPTURED ARRAY 
     $tmp = $_P‌​OST['tmp'][i];//CAPTURED ARRAY 
     for ($i = 0; $i <= count(sno); $i++) { 
      $insert_stmt = $mysql->prepare("INSERT INTO attendance(s_no, class_n, att, tmp) VALUES (?, ?, ?, ?)")) { 
      $insert_stmt->bind_param('iiii', mysql_real_escape_string($s_no[$i]), mysql_real_escape_string($class_n), mysql_real_escape_string($att), mysql_real_escape_string($tmp)); 
      // Execute the prepared query. 
      if (! $insert_stmt->execute()) { 
       echo "Uh oh! Houston we have a problem!!"; 
      else 
       //$insert_stmt->affected_rows 
       // DO Something here.... 
       //$insert_stmt->close 
       echo "<script>console.log('Affected ".$insert_stmt->affected_rows."');</script>"; 
      } 
     } 
    } 
?>