2011-04-27 102 views
-1

感谢您的帮助球员,现在重定向到winner.php但不更新DB ...PHP和MySQL更新记录,但页面显示不显示新的数据

这里是我现在有:

// 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的...慢慢的...

感谢您的帮助!

+0

'mm_jumpmenu()'啊,怀旧... :-) – Capsule 2011-04-27 15:40:41

回答

2

添加什么BraedenP已经写道:

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

听起来OP对于PHP开发来说相当新颖。有关SQL注入的一些链接以及如何防止它可能会被广泛接受。 – 2011-04-27 15:54:29

1

您这里有两个问题...

首先,重定向的页面,你实际上需要发送一个报头;现在你只需设置一个$ redirect变量。您将需要更换行:

$redirect = "winner.php"; 

有:

header("location: winner.php"); 

其次,你已经写好内容后,您不能重定向的页面,因为写的内容定型发回了头浏览器。重定向将需要在您的代码的其余部分发生在页面的顶部。

更改上面的代码后,您应该将表格下方的所有PHP移动到页面顶部的东西的末尾。你这样做后,它应该都可以正常工作。

+0

感谢您的帮助 – Peter 2011-04-27 15:47:47

+0

您是否得到它的工作? – BraedenP 2011-04-27 15:51:36

+0

重定向的作品,但现在的数据库不更新与新的数据...我编辑我的原始问题,以显示我到目前为止。 – Peter 2011-04-27 15:59:04

1

你或许应该重新安排你的代码。

<?php 

    // 1. Update if POST contains data 

    // 2. query database here 

    // 3. output result 

?> 

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

1

你有没有试图改变: WHERE ID = '$ ID [$ i]' “ 这样: WHERE ID = '$ I'”

而且,这里是一个链接到SQL注入,这可能会给你你不已经有一些信息有:第一

SELECT查询来获得行数为UPDATE查询需要这样的信息:http://www.unixwiz.net/techtips/sql-injection.html

0

将代码放在网页上的顺序。 UPDATE查询 SELECT查询($result=mysql_query($sql);)再次更新页面数据,否则页面将只显示预先更新的数据。

不是很干净,但它的工作原理...

我注意到代码从PHPEasystep.com网站发起。

相关问题