0

当我执行以下 transportreg_edit_n.php 时,出现“未定义偏移量:1”、“未定义偏移量:2”...等错误

错误指向的地方突出显示...

“我之前得到未定义的变量行”错误然后我将其初始化为 $row="" ;然后我得到了这个..我应该怎么做才能摆脱这个?

我不确定我是否正确使用了 isset(.....) 属性,因为我还没有对这一点进行错误检查:) 如果是这样,请忍受我的新手错误:) tnx 提前很多.

<!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" />
<title>Edit Page</title>

<style type="text/css">
body,td,th {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 16px;
    text-align: left;
}
body {
        background: url(Image/bg.jpg) no-repeat center center fixed; 
        -webkit-background-size: cover;
        -moz-background-size: cover;
        -o-background-size: cover;
        background-size: cover;
}

</style>
<link href="CSS/transportreg_editStyleSheet.css" rel="stylesheet" type="text/css" />
</head>



<?php

        $dbhost="localhost";
        $dbuser="root";
        $dbpass="rootout";
        $dbdb="test";


        $connect = mysql_connect($dbhost,$dbuser,$dbpass);
        if (!$connect)
        {
            echo "Failed To Connect To MySql".mysql_error();
            echo "<br /><br /><a href='transportreg.php'>Go Back </a>";
        }


        $db = mysql_select_db($dbdb,$connect);
        if (!$db)
        {
            echo "Failed To Connect To The Database".mysql_error();
            echo "<br /><br /><a href='transportreg.php'>Go Back </a>";
        }


        $row = ""; //added this because of undefined variable row error


        if(isset($_GET['edit']))
        {
            $id = $_GET['edit'];
            $sql = "SELECT * FROM transportpublic WHERE transportpublicid='$id' ";
            $res = mysql_query($sql)or die($sql."<br/><br/>".mysql_error());
            $row = mysql_fetch_array($res,MYSQL_BOTH);
        }

        if(isset($_POST['edittxttravelType_public'],$_POST['edittxtCostPerKm_public'],$_POST['edittxtAddress_public'],$_POST['edittxtPhoneNumber1_public'],$_POST['edittxtweblink_public'],$_POST['edittxtDescription_public']))
        {
            $id = $_POST['edittxtid_public'];
            $new_txttravelType_public = $_POST['edittxttravelType_public'];
            $new_txtCostPerKm_public = $_POST['edittxtCostPerKm_public'];
            $new_txtAddress_public = $_POST['edittxtAddress_public'];
            $new_txtPhoneNumber1_public = $_POST['edittxtPhoneNumber1_public'];
            $new_txtweblink_public = $_POST['edittxtweblink_public'];
            $new_txtDescription_public = $_POST['edittxtDescription_public'];

            $sql = "UPDATE transportpublic SET transportType='$new_txttravelType_public'  AND costPerKm='$new_txtCostPerKm_public'  AND address='$new_txtAddress_public'  AND teleNo='$new_txtPhoneNumber1_public'  AND webLink='$new_txtweblink_public'  AND description='$new_txtDescription_public'  WHERE transportpublicid ='$id' ";

            $res = mysql_query($sql) or die("Error Occured Update Unseccessful! ".mysql_error());


            echo "<br /><a href='transportreg_showdata.php'>View Current Data</a>";

        }


?>

<body>
<form id="form1" name="form1" method="post" action="transportreg_edit_n.php">
  <table width="597" border="0" align="center" cellpadding="0">
    <tr>
      <th colspan="2" bgcolor="#000000" class="mainTitle" scope="row">Edit Form</th>
    </tr>
    <tr bgcolor="#c1e9ea">
      <td width="324" scope="row"><blockquote>
        <p><span class="classlabels">Transportation Type/Name :</span></p>
      </blockquote></td>
      <td width="270">
      <label for="edittxttravelType_public"></label>

      //here $row[1] causes the error "Undefined offset: 1"

     <input name="edittxttravelType_public" type="text" value="<?php echo $row[1]; ?>" id="edittxttravelType_public2" size="45" maxlength="60" />

      //here $row[0] causes an error "Undefined offset: 0"

     <input name="edittxtid_public" type="hidden" value="<?php echo **$row[0]**; ?>" id="edittxttravelType_public" size="45" maxlength="60" />
     </td>
    </tr>
    <tr bgcolor="#dbf4f6">
      <td scope="row"><blockquote>
        <p>Cost per kilometer :</p>
      </blockquote></td>
      <td>
      <label for="edittxtCostPerKm_public"></label>

       //here $row[2] causes an error "Undefined offset: 2"

      <input name="edittxtCostPerKm_public" value="<?php echo $row[2]; ?>" type="text" id="edittxtCostPerKm_public" size="45" maxlength="60" /></td>
    </tr>
    <tr>
      <td bgcolor="#c1e9ea" scope="row"><blockquote>Address :</blockquote></td>
      <td bgcolor="#c1e9ea">
      <label for="edittxtAddress_public"></label>

     //here $row[3] causes an error "Undefined offset: 3"

    <textarea name="edittxtAddress_public" id="edittxtAddress_public" cols="32" rows="3"><?php echo $row[3]; ?> </textarea></td>
    </tr>
    <tr bgcolor="#dbf4f6">
      <td bgcolor="#dbf4f6" scope="row"><blockquote>Telephone Number :</blockquote></td>
      <td>
      <label for="edittxtPhoneNumber1_public"></label>

    //here $row[4] causes an error "Undefined offset: 4"

    <input name="edittxtPhoneNumber1_public" type="text"  value="<?php echo $row[4]; ?>" id="edittxtPhoneNumber1_public" size="45" maxlength="15" /></td>
    </tr>
    <tr bgcolor="#c1e9ea">
      <td bgcolor="#c1e9ea" scope="row"><blockquote>Official WebSite Link :</blockquote></td>
      <td>
      <label for="edittxtweblink_public"></label>

   //here $row[5] causes an error "Undefined offset: 5"

    <input name="edittxtweblink_public" type="text" value="<?php echo $row[5]; ?>" id="edittxtweblink_public" size="45" maxlength="300" />

     </td>
    </tr>
    <tr bgcolor="#dbf4f6">
      <td scope="row"><blockquote>Brief  Description :</blockquote></td>
      <td><label for="edittxtDescription_public"></label>


    //here $row[6] causes an error "Undefined offset: 6"

    <textarea name="edittxtDescription_public" id="edittxtDescription_public" cols="32" rows="10"><?php echo $row[6]; ?></textarea></td>
    </tr>
    <tr bgcolor="#dbf4f6">
      <td colspan="2" bgcolor="#c1e9ea" scope="row"><blockquote>
        <blockquote>
          <blockquote>
            <blockquote>
              <blockquote>
                <blockquote>
                  <p>
                    <input name="btnupdatet_public" type="submit" class="submit" id="btnupdatet_public" value="Update" />
                  </p>
                </blockquote>
              </blockquote>
            </blockquote>
          </blockquote>
        </blockquote>
      </blockquote></td>
    </tr>
    <tr bgcolor="#dbf4f6">
      <td colspan="2" bgcolor="#c1e9ea" scope="row"><table width="601" border='0' bordercolor='#000000' class="tb">
                  <tr> 
                      <td width="340" height="20"><a href='transportreg_showdata.php'>View Updated Public Transportation Data</a>
                      </td>
                      <td width="272"><a href='transportreg.php'>Go Back To Registration Page</a>
                      </td>
      </table></td>
    </tr>
  </table>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
</form>
<?php
        mysql_close($connect);
?>

</body>
</html>
4

1 回答 1

1

如果$_GET['edit']未设置,$row则不是数组而是空字符串,这会导致该错误。

为了解决这个问题,在任何你想使用它的地方,你都需要一个像这样的结构:

if (isset($row[1]))
{
  echo $row[1];
}
// etc.

Or you define $row as an array with lots of empty strings for all your table fields instead of an empty string...

于 2013-03-08T22:03:22.990 回答