PHP和MySQL正在更新记录,但是页面显示没有显示新的数据


PHP & MySQL updating records, but page display not showing new data

感谢帮助的家伙,它现在重定向到赢家。php,但不更新数据库…

下面是我现在的文件:

// 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 $tbl_name WHERE depot = 'plainview'";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);

if( isset($_POST['Submit'])){
for($i=0;$i<$count;$i++){
$sql1 = "UPDATE $tbl_name SET 
    available='{$_POST['available'][$i]}', 
    rent='{$_POST['rent'][$i]}', 
    corp_ready='{$_POST['corp_ready'][$i]}', 
    down='{$_POST['down'][$i]}', 
    gfs='{$_POST['gfs'][$i]}',
    dateTime = NOW()  
WHERE id='$id[$i]'"; 

$result1 = mysql_query($sql1) or die(mysql_error());
}
}

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

<!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=UTF-8" />
<script language="JavaScript1.1" type="text/javascript">
<!--
function mm_jumpmenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
//-->
</script>
<title>Untitled Document</title>
</head>
<body>
<div>
    <p>Plainview, North East Region</p>
    <p>Select a different region: <select onchange="mm_jumpmenu('parent',this,0)" name="lostlist">
                <option value="" selected="selected">Choose Your Depot</option>
                <option value="plainview.php">Plainview</option>
                <option value="worcrester.php">Worcrester</option>
                </select></p>
</div><Br />
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr>
<td>
<table width="700" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>ID</td>
<td align="center"><strong>Product Name</strong></td>
<td align="center"><strong>Available</strong></td>
<td align="center"><strong>Rent</strong></td>
<td align="center"><strong>Corp Ready</strong></td>
<td align="center"><strong>Down</strong></td>
<td align="center"><strong>GFS</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="left"><?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?></td>
<td align="left"><?php echo $rows['product']; ?></td>
<td align="center"><input name="available[]" type="text" id="available" value="<?php echo $rows['available']; ?>" size="5"></td>
<td align="center"><input name="rent[]" type="text" id="rent" value="<?php echo $rows['rent']; ?>" size="5"></td>
<td align="center"><input name="corp_ready[]" type="text" id="corp_ready" value="<?php echo $rows['corp_ready']; ?>" size="5"></td>
<td align="center"><input name="down[]" type="text" id="down" value="<?php echo $rows['down']; ?>" size="5" /></td>
<td align="center"><input name="gfs[]" type="text" id="gfs" value="<?php echo $rows['gfs']; ?>" size="5"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>

</body>
</html>

我确实意识到这是一个拼凑,我刚刚开始学习PHP…慢慢…

谢谢你的帮助!

添加BraedenP已经写的内容:

  • 应该是if( isset($_POST['Submit'])){
  • 你的代码是开放的SQL注入

你有两个问题…

首先,重定向页面,你实际上需要发送一个标题;现在,您只是设置了一个$redirect变量。您需要替换以下行:

$redirect = "winner.php";

:

header("location: winner.php");

其次,在您已经向页面写入内容之后,您不能重定向页面,因为写入内容将最终发送回浏览器的标头。重定向将需要发生在页面的顶部与您的其余代码。

在更改了上面的行之后,您应该将表下面的所有PHP移到页面顶部的内容末尾。这样做之后,应该就可以正常工作了。

您可能应该重新排列您的代码。

<?php
  // 1. Update if POST contains data
  // 2. query database here
  // 3. output result
?>

这样你就不需要任何重定向,因为如果你改变了一些东西,你在再次读取数据库之前就做了。

您是否尝试过更改:在id = $ id ($ i)":在id = $ i"

另外,这里有一个SQL注入的链接,它可能会给你一些你还没有的信息:http://www.unixwiz.net/techtips/sql-injection.html

将代码按以下顺序放置在页面上:

SELECT查询首先获得行数,因为UPDATE查询需要此信息。UPDATE查询SELECT再次查询($result=mysql_query($sql);)以更新页面数据,否则页面将只显示预先更新的数据。

不是很干净,但它工作…

我注意到代码来自PHPEasystep.com网站。