-4

我正在尝试更新文本框中已编辑并发布到数据库的任何内容。但是,只有第二条记录是更新的,而第一条记录不是。我认为应该是while循环问题,但我不知道错误是什么。这是我的编辑页面代码:viewadmindb.php

<?php
session_start();
include('adminconfig.php');


$sql = "SELECT * FROM admin ORDER BY ID"; 
$result = mysql_query($sql);  
?>


<body>
<div id="wrap">
<div id="status"></div>
<form method="POST" action="adminsave.php" onSubmit="return validate(this);">
<table class="viewdb"  contentEditable="true">
<tr><td id='fcolor' style='border:2px solid black' align=center> ID </td>
<td id='fcolor' style='border:2px solid black' align=center> Name </td>
<td id='fcolor' style='border:2px solid black' align=center> Password </td>
<td id='fcolor' style='border:2px solid black; width:auto;' align=center>           
 Department</td>
<td id='fcolor' style='border:2px solid black' align=center> Email </td></tr>
<div id="content">
<?php

 while($row = mysql_fetch_array($result)){ ?>
<tr>
      <td style='border:2px solid black; width:auto' align=center><?php echo $row[] = 
      $row['ID'] ?></td>
 <td style='border:2px solid black' align=center> <?php echo $row[]    
     = $row['name']  ?> </td>
<td style='border:2px solid black' align=center> <?php echo $row[] =    
    $row['password']  ?> </td>
<td style='border:2px solid black; width:200px' align=center> <?php echo $row[] =
    $row['department'] ?> </td>
<td style='border:2px solid black' align=center> <?php echo $row[] = $row['email']
    ?> </td>

<tr>
    <td><input id='edit' type = 'text' name="ID[]" value='<?php echo $row['ID'] ?>' 
     maxlength="50"></td>

    <td><input id='edit' type = 'text' name="name[]" value='<?php echo $row['name']  
      ?>' 
     maxlength="50"></td>
     <td><input id='edit' type = 'text' name="password[]" value='<?php echo  
     $row['password'] ?>' maxlength=50"></td>

     <td><input id='edit' type = 'text' name="department[]" value='<?php echo   
     $row['department'] ?>' maxlength="50"></td>

    <td><input id='edit' type = 'text' name="email[]" value='<?php echo 
     $row['email']?>' 
     style='width:300px' " maxlength="50"></td>

    <?php } ?>
   <td><input id='edit' type='submit' name='<?php $row['ID'] ?>' value='Submit'/> 
    </td></tr>
</table>
</form>
    <?php 
    $ID=$row['ID'];
$name=$row['name'];
$password=$row['password'];
$department=$row['department'];
$email=$row['email'];
?>

管理员保存.php

<?php
session_start();
include('adminconfig.php');

$ids=$_POST['ID'];
$name_arr=$_POST['name'];
$password_arr=$_POST['password'];
$department_arr=$_POST['department'];
$email_arr=$_POST['email'];

foreach(($ids as $key=>$id) {
     $name = $name_arr[$key];
     $password = $password_arr[$key];
     $department = $department_arr[$key];
     $email = $email_arr[$key];       
$sql = "UPDATE admin SET name = '$name',password = '$password',
department ='$department',email = '$email' WHERE ID = '$id'";
}
$result = mysql_query($sql);
if(!$result){
   die('invalid query:'.mysql_error());
 }
else
echo ("<tr><td>" . "Data updated succesfully..." . "</td></tr>");
header('Refresh:5; url=viewadmindb.php');
die;

?>
4

4 回答 4

1

You really should look up into how ID's are supposed to work in html. The basic things is that ID must be unique. You should not have two or more elements with same ID. But in your case it's the name-attribute that is the issue.

If you have a loop like this...

while($row = mysql_fetch_array($result)){ ?>
<tr>
<td><input id='edit' type = 'text' name="ID" value='<?php echo $row['ID'] ?>' 
 maxlength="50"></td>
</tr>

}?>

...and you have two rows from the $result-recordset, you will echo out html something like this:

<tr>
<td><input id='edit' type = 'text' name="ID" value='1' 
 maxlength="50"></td>
</tr>
<tr>
<td><input id='edit' type = 'text' name="ID" value='2' 
 maxlength="50"></td>
</tr>

Your then saving values into the database based on a element with name ID. But the problem is that PHP doesn't know which of the rows above it should use (How could PHP know?). When refering to an element that has a duplicate the last element in the DOM is used. Therefore only this row is take into account:

<tr>
<td><input id='edit' type = 'text' name="ID" value='2' 
 maxlength="50"></td>
</tr>

There are no loop in adminsave.php that indicates you want to save several values. It just tells that you want to save content into database with a specific ID.

$sql = "UPDATE admin SET name = '$name',password = '$password',
department ='$department',email = '$email' WHERE ID = '$ID'";

and because the last row in the DOM is used, the update-statement would be:

$sql = "UPDATE admin SET name = '$name',password = '$password',
department ='$department',email = '$email' WHERE ID = '2'";

You can solve this by making the name-element an array by adding brackets to name-elements: (Also make edit a class instead of an id because it's ok to have duplicate classes but not duplicate ids)

<tr>
<td><input class='edit' type = 'text' name="ID[]" value='<?php echo $row['ID'] ?>' 
 maxlength="50"></td>
</tr>

But then you would also have to loop through the array

<?php
$ids = $_POST['ID']; //Get array from form
$name_arr = $_POST['name'];
$password_arr = $_POST['password'];
$department_arr = $_POST['department'];
$email_arr = $_POST['email'];

foreach($ids as $key=>$id) {
    //Get specific element in each array
    $name = $name_arr[$key]; 
    $password = $password_arr[$key];
    $department = $department_arr[$key];
    $email = $email_arr[$key];

    //Create sql and execute
    $sql = "UPDATE admin SET name = '$name',password = '$password',
    department ='$department',email = '$email' WHERE ID = '$id'";
    $result = mysql_query($sql);
}

The row:

$sql = "SELECT * FROM admin WHERE $ID = '$ID'";  

is pointless because the variable $sql is overritten on the next row.

Note that above is just for demonstrating how the basic concepts of ids, names and arrays works when handling forms. You should really not just mysql_* functions, but instead read up on PDO or mysqli instead. You should sanitize (make sure unwanted data is not injected into db) before updating.

于 2013-08-06T07:26:33.063 回答
0

整个逻辑是错误的。

  1. 只需将查询字符串从主页传递到另一个 php 页面,例如从:admin_detail.php 到 edit_admin.php
  2. 然后根据传递的查询字符串查询数据库的数据
  3. 在所需的文本框中回显它们。
  4. 然后调用更新语句。
于 2013-08-06T06:25:30.047 回答
0

看来您是 php 新手。

  1. 您的代码格式不正确,并且不是真正可读的。
  2. 不要做 $_POST['...'] 并将这个值直接写入数据库(安全问题 => mysql 注入)所以请在插入数据库之前插入 mysql_real_escape_string($value)。
  3. 那是什么黑客?echo $row[] = $row['password']不要那样做!只有回声就足够了。

您的答案的解决方案: 您的代码仅更新 while 循环的最后一次迭代是正常的,因为只有最后一个值将存储到 $_POST 数组中。如果你想解决这个问题,你必须将表单设置为数组,如:

<input id='edit' type = 'text' name="name[]" value='<?php echo $row['name'] ?>' 
 maxlength="50">

然后在您的 viewadmindb.php 中,您必须再次遍历这些值,并为每个值创建一个额外的更新查询,以更新数据库中的值。

更新:

adminsave.php 中的 foreach 循环应如下所示:

$arrIds = array();
$arrNames = array();
$arrDepartments = array();
$arrPasswords = array();
// ... add all necessary vars you wan a fetch from the post request
$arrIds[] = $_POST['name'][];
$arrNames[] = $_POST['name'][];
$arrDepartments[] = $_POST['department'][];

$arrResults = array();   // To store result data if necessary 
foreach($arrIds as $key => $item) {
  // Build sql query
  $sql = "UPDATE admin SET name = '". $arrNames[$key] . "',password = '". $arrPasswords[$key] . "',
  department ='". $arrDepartments[$key] . "',email = '". $arrEmailss[$key] . "' WHERE ID = '$item'";
  // Execute query!
  $arrResults[] = mysql_query($sql);
}

所以现在你应该能够让它运行......

于 2013-08-06T06:29:38.817 回答
0

查看admindb.php

您没有设置的 var $row。只需$row = mysql_fetch_array($reslult);在访问表值之前添加此内容。

这是什么$row[] = $row['name']?您重新填充$row,并且在您无法从数据库访问原始值之后。只使用标签,不使用 vars<td> E-mail: </td>

adminsave.php 你重写了 $sql 变量。$sql = "SELECT * FROM admin WHERE $ID = '$ID';您不需要使用的线路。

好的提示:使用 css 语法`并将 varchars 与{$var}

"UPDATE `admin` SET `name` = '{$name}', `password` = '{$password}', `department` = '{$department}', `email` = '{$email}' WHERE `ID` = '{$ID}'"
于 2013-08-06T06:31:25.433 回答