2017-04-09 72 views
-2

我需要使用提交的数据编辑我的数据库表。在php页面编辑sql数据

这是形式:

mysql_query("set names 'utf8'"); 

$query = "SELECT * FROM sec1octa"; 
$result = mysql_query($query); 
?> 
<div align="center"> 
    <form method="get" action="edit_data.php"> 
     <table width="104" border="1" class="center1"> 
      <tr> 
       <th width="94">first</th> 
       <th width="94">second</th> 
       <th width="94">status</th> 
      </tr> 
      <tr> 
       <?php 
       if (mysql_num_rows($result) > 0) { 
        while ($row = mysql_fetch_array($result)) { 
         ?> 
         <tr> 
          <td><input type="text" name="id" value="<?php echo $row ['stu_no']; ?> " size=10></td> 
          <td><input type="text" name="name" value="<?php echo $row ['stu_name']; ?> " size=10></td> 
          <td><?php 
           echo '<select name="status">'; { 
            echo '<option value="open">'.$row['stu_status'].'</option>'; 
            echo '<option value="close">'.prevent.'</option>'; 
           } 
           echo '</select>'; 
          ?></td> 
         </tr> 
         <?php 
        } 
       } 
       ?> 
      </tr> 
     </table> 
     <input type="submit" name="submit" value="done" /> 
    </form> 

的问题是在edit_data.php页。 我不能UPDATE

我使用此代码,但它不工作。

require_once('../Connections/config.php'); 
$id= $_GET['id']; 
$status= $_GET['status']; 

$query= mysql_query("UPDATE `goh`.`sec1octa` SET `stu_status` = '$status' 
WHERE stu_no='".$id."'") or die (mysql_error()); 
if($query){echo $status ."done ";} 
+1

你有一个错字:$ qeury而不是$查询。并分享你的错误。 – contremaitre

+0

没有错误出现,它只是在我的数据库的2列,最后一列(编号42)和列编号(40)更新,当我尝试更新到另一列也更新到第42号 –

+0

@ mickmackusa,我该如何清洁它? ,我怎样才能使用edit_data.php中的名字?或者它没有用? –

回答

0

的原因,你只能在你的edit_data.php$_GET获得最后的值是因为你没有设置输入/选择的名称作为数组。

<input type="text" name="id" value="some_stu_no"> 

发生了一遍又一遍,每一个新的覆盖以前。

相反,你应该使用:

<input type="text" name="id[]" value="some_stu_no"> 

这将允许您通过多个ID的单一形式提交。

您的形式:

<form method="POST" action="edit_data.php"> 
.... 
echo "<tr>"; 
    echo "<th>id</th>"; 
    echo "<th>name</th>"; 
    echo "<th>status</th>"; 
echo "</tr>"; 
if(mysql_num_rows($result)>0){ 
    while($row=mysql_fetch_array($result)){ 
     echo "<tr>"; 
      echo "<td><input type=\"text\" name=\"id[]\" value=\"{$row['stu_no']}\" size=\"10\"></td>"; 
      echo "<td>{$row['stu_name']}</td>"; 
      echo "<td>"; 
       echo "<select name=\"status[]\">"; // I don't like your option set up here, but I don't fully understand it either. 
        echo "<option value=\"open\">{$row['stu_status']}</option>"; 
        echo "<option value=\"close\">.prevent.</option>"; 
       echo "</select>"; 
      echo "</td>"; 
     echo "</tr>"; 
    } 
} 
.... 
<input type="submit" value="Submit All"> 
</form> 

edit_data.php

// create a mysqli connection called $db 

if(isset($_POST['id'])){ 
    $tally=0; 

    // build all queries for the batch 
    foreach($_POST['id'] as $index=>$id){ 
     $queries[]="UPDATE `goh`.`sec1octa` SET `stu_status`='".mysqli_real_escape_string($db,$_POST['status'][$index])."' WHERE `stu_no`='".mysqli_real_escape_string($db,$id)."'"; 
    } 

    // run all queries 
    if(mysqli_multi_query($db,implode(';',$queries)){ 
     do{ 
      $tally+=mysqli_affected_rows($db); 
     } while(mysqli_more_results($db) && mysqli_next_result($db)); 
    } 

    // assess the outcome 
    if($error_mess=mysqli_error($db)){ 
     echo "Syntax Error: $error_mess"; 
    }else{ 
     echo "$tally row",($tally!=1?"s":"")," updated"; 
    } 
    mysqli_close($con); 
} 
+0

谢谢,我尝试通过PHP的形式,但我不能,也我使用你写的代码,但我发现语法错误,可以' t纠正它,如果打扰你,我很抱歉。 –

+0

现在它的工作谢谢,但我可以提交按钮给所有?不是一个接一个,我觉得更容易,选择任何想法​​怎么样?因为它是不好的 –

+0

,但我首先要求解决有关一个更新按钮到所有字段的问题,并且您逐个解决更新问题,所以谢谢您,但这不是我首先要问的问题,很明显。但感谢你的努力并加以标记。 –