2016-07-06 88 views
0

我試圖顯示和更新一次所有的記錄表。我可以成功顯示記錄。但我無法更新我的數據庫,請幫我更新表中的記錄。
enter image description here更新php中的多行mysql

<?php while($rows=mysqli_fetch_array($result)){ ?> 
 

 
<tr> 
 
    <td align="center"> 
 
    <input name="id[]" type="text" id="name" value="<?php echo $rows['id']; ?>"> 
 
    </td> 
 
    <td align="center"> 
 
    <input name="name[]" type="text" id="name" value="<?php echo $rows['name']; ?>"> 
 
    </td> 
 
    <td align="center"> 
 
    <input name="lastname[]" type="text" id="lastname" value="<?php echo $rows['lastname']; ?>"> 
 
    </td> 
 
    <td align="center"> 
 
    <input name="email[]" type="text" id="email" value="<?php echo $rows['email']; ?>"> 
 
    </td> 
 
</tr> 
 

 
<?php } ?> 
 

 
<tr> 
 
    <td colspan="4" align="center"> 
 
    <input type="submit" name="Submit" value="Submit"> 
 
    </td> 
 
</tr> 
 
</table> 
 
</td> 
 
</tr> 
 
</form> 
 
</table> 
 
<?php if(isset($_POST[ 'Submit'])) { $id_array=$_POST[ 'id']; $name_array=$ _POST[ 'name']; $year_array=$_POST[ 'lastname']; $emil_array=$_POST[ 'email']; for($i=0;$i<$count;$i++){ $sql1="UPDATE test_mysql SET name='$name_array[$i]', lastname='$year_array[$i]', email='$emil_array[$i]' WHERE id='id_array[$i]'" 
 
; } mysqli_close($con); } ?>

回答

0

您的形式沒有一個行動處理器。

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
0

嘗試:

HTML:

<table> 
<form name="form1" method="post"> <!-- add form tag --> 

<?php while($rows=mysqli_fetch_array($result)){ ?> 

<tr> 
    <td align="center"> 
    <input name="id[]" type="text" id="name" value="<?php echo $rows['id']; ?>"> 
    </td> 
    <td align="center"> 
    <input name="name[]" type="text" id="name" value="<?php echo $rows['name']; ?>"> 
    </td> 
    <td align="center"> 
    <input name="lastname[]" type="text" id="lastname" value="<?php echo $rows['lastname']; ?>"> 
    </td> 
    <td align="center"> 
    <input name="email[]" type="text" id="email" value="<?php echo $rows['email']; ?>"> 
    </td> 
</tr> 

<?php } ?> 

<tr> 
    <td colspan="4" align="center"> 
    <input type="submit" name="Submit" value="Submit"> 
    </td> 
</tr> 
</table> 

</form> 
</table> 

PHP:

if(isset($_POST['Submit'])) { 
    $id_array=$_POST['id']; 
    $name_array=$ _POST['name']; 
    $year_array=$_POST['lastname']; 
    $emil_array=$_POST['email']; 
    for($i=0;$i<$count;$i++){ 
    $id = $name_array[$i]; 
    $name = $year_array[$i]; 
    $year = $emil_array[$i]; 
    $email = $id_array[$i]; // use $id_array[$i] to access id, as in above code you did not use $ in id_array 
    $sql="UPDATE test_mysql SET name='$name', lastname='$year', email='$email' WHERE id='$id'"; 
    if ($conn->query($sql) === TRUE) { // execute query like this 
     echo "Record updated successfully"; 
    } else { 
     echo "Error updating record: " . $conn->error; 
    } 
    } 
    $conn->close(); 
} 
+0

我想你的代碼,它仍然沒有更新, – Deepashika

0

在你的表格你不提任何操作處理程序。代碼必須

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
+0

現在我加動作處理,但還是其OP使用的MySQLi不工作 – Deepashika

0

希望這會幫助你,我並沒有把函數mysql_escape_string因爲你尚在啓動,但它會幫助逃避MySQL的攻擊。並且還包 窗體上<form>

if(isset($_POST['Submit'])) { 
    $id_array= $_POST['id']; 
    $name_array=$ _POST['name']; 
    $year_array=$_POST['lastname']; 
    $emil_array=$_POST['email']; 


    if($id_array){ 

    foreach($id_array as $key=>$id_val){ 
     $name = $name_array[$key]; 
     $year = $year_array[$key]; 
     $email = $emil_array[$key]; 
     $sql = "update activity_logs set name='{$name}' , lastname = '{$year}' , email='{$email}' where activity_logs_id = {$id_val}"; 
     $res = mysql_query($sql); 

    } 

    } 

} 
+0

。建議使用[prepared statements](http://php.net/manual/en/mysqli.prepare.php)也是一個好主意。 – Mikey

+0

感謝您的建議@Mikey的歡呼聲 –