2013-03-08 72 views
0

我得到一個「未定義抵消:1」,「未定義抵消:2」 ......等錯誤,當我執行以下transportreg_edit_n.php

地方錯誤點突出顯示...

「我之前得到了未定義的變量行」錯誤,然後我將它初始化爲$ 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> 
+0

數組是空的問題,還是你收到關於它的錯誤消息? – 2013-03-08 22:01:39

+0

@SamDufel我得到的錯誤「未定義偏移量:..」,它指向我使用$行[...]的每一行 – I2Believe 2013-03-08 22:06:34

+0

@ I2Believe - 是的,你在你的問題中說過。你不清楚你是否期望數據在'$ row'中,或者你是否期望它是空的,只是想擺脫錯誤信息。 – 2013-03-08 22:31:14

回答

1

如果沒有設置$_GET['edit']$row不是一個數組而是一個空字符串,並導致該錯誤。

爲了解決這個問題,處處要使用它,你就需要一個結構,如:

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

或者你定義$row與很多你的所有表中的字段,而不是一個空字符串數組空字符串...

+0

我試過你的方法來定義$行爲一個有很多空字符串的數組$ row = array('','','','','','','');並且它很快擺脫了那個錯誤tnx ...並且每個人都4你的時間和精力感激它... – I2Believe 2013-03-08 22:24:26