PHP错误 undefined variable : result1 help!

标签 php mysql

我正在尝试解决此代码问题,但收到此错误,但我不知道如何修复它: undefined variable :第 44 行 plainview.php 中的 result1

这是代码:

// 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);

//error reporting
error_reporting(E_ALL); ini_set('display_errors', '1');


//update
if(isset($_POST['Submit'])){
for($i=0;$i<$count;$i++){

$sql1 = "UPDATE $tbl_name SET 

available='".mysql_real_escape_string($_POST['available'][$i])."', 
rent='".mysql_real_escape_string($_POST['rent'][$i])."',  
corp_ready='".mysql_real_escape_string($_POST['corp_ready'][$i])."', 
down='".mysql_real_escape_string($_POST['down'][$i])."',  
gfs='".mysql_real_escape_string($_POST['gfs'][$i])."', 
dateTime = NOW()   
WHERE id='".$id[$i]."'";



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

//redirect
if($result1){
header("location: plainview.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 的业余爱好者,任何帮助将不胜感激......

最佳答案

$result1 仅在 if 语句内的 for 循环内设置。如果 if 失败,则它永远不会被定义,因此 if($result1) 会给出该错误。在 if 语句之前设置 $result1 = false。更好的是,将 if 语句移动到整个代码块周围,包括数据库连接和断开连接的内容。

关于PHP错误 undefined variable : result1 help!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5809543/

相关文章:

c# - 在 C# 中显示数据库 MySQL 中的数据被删除时的状态

php - mysql_select_db 失败

php - 两个纪元时间戳之间的 MYSQL 查询有问题吗?

php - Laravel错误: Class 'Collective\Html\HtmlServiceProvider' not found after running composer require laravelcollective/html in CMD

php - 如何根据opencart购物车页面上的产品类别计算折扣?

mysql - 如何在 MYSQL 中选择间隔和下一条记录?

php - PHP 脚本中的 If 语句

php - Laravel 动态下拉国家和州

PHP MySQL 排序规则问题 - 连接表

php - 注册/登录表单 PHP MySQL