0

目前,我被要求完成剧本的最后一道关卡。

我知道 sql 注入的风险,但它是一个私人网站,其中 1 人可以访问表格等。

我现在的问题是,当我尝试更新数据库中的字段时,最后一页显示成功。但是数据库实际上并没有更新。在你们的帮助下,这是我的 2 个脚本:

<?php
     session_start();
    include_once("isadmin.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Update Client Message</title>
<link href="loginmodule.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
    if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) >0 ) {
        echo '<ul class="err">';
        foreach($_SESSION['ERRMSG_ARR'] as $msg) {
            echo '<li>',$msg,'</li>'; 
        }
        echo '</ul>';
        unset($_SESSION['ERRMSG_ARR']);
     }
?>
<form id="updateform" name="updateform" method="post" action="updateexec.php">
  <table width="500" border="0" align="center" cellpadding="2" cellspacing="0">
    <tr>
      <th width="200">Select User</th>
      <td>
 <?php
require_once('config.php');

    //Connect to mysql server
    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    if(!$link) {
        die('Failed to connect to server: ' . mysql_error());
    }

    //Select database
    $db = mysql_select_db(DB_DATABASE);
if(!$db) {
    die("Unable to select database");

    }


$useruploadids = mysql_query("SELECT member_id, firstname, lastname FROM members");
while ($row = mysql_fetch_assoc($useruploadids)) {
    $userid = $row['member_id']; 
    $firstname = $row['firstname'];
    $lastname = $row['lastname'];
?>
<input type="checkbox" name="userid_<?php echo $userid ?>" value="y" /><?php echo     $firstname ?><?php echo $lastname ?><br />
<?php } ?>
</td>
    </tr>
     <tr>
      <th>Message For Client </th>
      <td>
      <textarea input name="otherdeets" type="textarea" class="textfield" id="otherdeets" style="width: 356px; height: 176px">
          </textarea>
      </td>
    </tr>


    <tr>
      <td>&nbsp;</td>
          <td><input type="submit" name="Submit" value="Update" /></td>
    </tr>
  </table>
</form>
</body>
</html>

如您所知,这是表单,现在是 exec 脚本:

 <?php 

 echo( "<pre>" );
 print_r( $_POST );
 echo( "</pre>" );

include ("config.php"); 
$tbl_name="members";
    //Connect to mysql server
    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    if(!$link) {
        die('Failed to connect to server: ' . mysql_error());
    }

    //Select database
    $db = mysql_select_db(DB_DATABASE);
    if(!$db) {
        die("Unable to select database");

        }

 //This gets all the other information from the form 
 $update = $_POST['otherdeets']; 
 $id = $_POST['userid'.$row['member_id']];

 // Cycle through each member and check that it needs to be added to the db
$useruploadids = mysql_query( "SELECT member_id FROM members" );
while ($row = mysql_fetch_assoc($useruploadids))
{
    // Check that the member was sent from the last form
    if( isset( $_POST['userid_'.$row['member_id']] ) && $_POST['userid_'.$row['member_id']] == "y" )
    {


// update data in mysql database
$sql="UPDATE $tbl_name SET otherdeets='$update' WHERE member_id='$id'";
$result=mysql_query($sql);  
}
}


 if($result){
echo "Successful";
echo "<BR>";
echo "<a href='admin-welcome.php'>Admin Home</a>";
}

else {
echo "ERROR";
}
 ?> 

所以我已经经历了很多次,并且无法为我的生活找出问题所在。

如果有帮助,其他字段是 VARCHAR 吗?

4

2 回答 2

0

尝试更好。

  if ($result && mysql_num_rows($result) > 0) {
    echo "Successful";
    echo "<BR>";
    echo "<a href='admin-welcome.php'>Admin Home</a>";
    }
于 2013-07-24T12:02:13.680 回答
0

您需要$row['member_id']在表单的隐藏字段中传递值

<input type="hidden" name="member_id" value="<?php echo $row['member_id']; ?>" />

所以在提交页面后,您可以获得 member_id 的值

//This gets all the other information from the form 
 $update = $_POST['otherdeets']; 
 $id = $_POST['member_id'];

希望这可以帮助您解决问题。

于 2013-07-24T11:11:15.263 回答