2017-01-03 15 views
-2

已經定義$的錯誤,我得到一個錯誤說

Notice: Undefined variable: error in C:\xampp\htdocs\songdb\edit.php on line 158

我已經試圖把isset()$error的,但它仍然無法正常工作。

編輯: 我已經刪除=在只使用isset($錯誤),但給我這個

解析錯誤:語法錯誤,意外 '{' 在C:\ XAMPP \ htdocs中\ songdb \編輯。 php on line 158

edited2:

我已經添加了$ error ='';並使用isset($error),但是當我點擊提交按鈕而沒有在所有的字段中輸入,它的工作原理,但它並沒有顯示錯誤信息,就像我想要的那樣。

編輯3:

我已經加入功能renderForm($ songid,$標題,$藝術家,$流派,$語言,$歌詞,$更新)後$連接和$數據庫,$錯誤='後錯誤:請填寫所有必填字段!';後如果($行),但它給了我這個錯誤

解析錯誤:語法錯誤,在C意外的文件結尾:\ XAMPP \ htdocs中\ songdb \ edit.php上線218

如果有沒有解決這個代碼,你可以給我一個替代/另一個編碼的edit.php文件的解決方案?非常感謝你。對於以前的成員答覆,非常感謝您的幫助。

<?php 
// connect to the database 
$connect = mysql_connect('localhost','root',''); 
$database = mysql_select_db('songdb'); 

function renderForm($songid, $title, $artist, $genre, $language, $lyrics, $update) 
// check if the form has been submitted. If it has, process the form and save it to the database 

if (isset($_POST['submit'])) { 
    // confirm that the 'id' value is a valid integer before getting the form data 
    if (is_numeric(isset($_POST['songid']))) { 
     // get form data, making sure it is valid 
     $error = ''; 
     $id = $_POST['songid']; 
     $title = isset($_POST['title']) ? $_POST['title'] : ""; 
     $artist = isset($_POST['artist']) ? $_POST['artist'] : ""; 
     $genre = isset($_POST['genre']) ? $_POST['genre'] : ""; 
     $language = isset($_POST['language']) ? $_POST['language'] : ""; 
     $lyrics = isset($_POST['lyrics']) ? $_POST['lyrics'] : ""; 
     $update = isset($_POST['update']) ? $_POST['update'] : ""; 

     $edit = "UPDATE songs SET title='.$title.',artist='.$artist.',genre='.$genre.',language='.$language.',lyrics='.$lyrics.',update='.$update.' where songid=$songid"; 

     // check that fields are filled in 

     if ($title == '' || $artist == '' || $genre == '' || $language == '' || $lyrics == '' || $update == '') { 
      // generate error message 
      $error = 'ERROR: Please fill in all required fields!'; 
      renderForm($songid, $title, $artist, $genre, $language, $lyrics, $update); 
     } else { 
      // save the data to the database 
      mysql_query($edit) or die(mysql_error()); 
      // once saved, redirect back to the view page 
      header("Location: view.php"); 
     } 
    } 
} else { 
    // if the form hasn't been submitted, get the data from the db and display the form 
    // get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0) 

    if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0) { 
     // query db 
     $songid = $_GET['songid']; 
     $result = mysql_query("SELECT * FROM songs WHERE songid=$songid") 
     or die(mysql_error()); 
     $row = mysql_fetch_array($result); 
     // check that the 'id' matches up with a row in the databse 

     if($row) { 
      // get data from db 
      $title = $row['title']; 
      $artist = $row['artist']; 
      $genre = $row['genre']; 
      $language = $row['language']; 
      $lyrics = $row['lyrics']; 
      $update = $row['update']; 

      renderForm($songid, $title, $artist, $genre, $language, $lyrics, $update); 
     } else { 
      // if no match, display result 
      echo "No results!"; 
     } 
    } else { 
     // if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error 
    } 
} 
?> 

<html> 
    <head> 
     <title>Edit Record</title> 
    </head> 
    <body> 
    <?php 
    if (isset($error)) { 
     echo '<div style="padding:4px; border:1px solid red;  color:red;">'.$error.'</div>'; 
    } 
    ?> 
     <form action="edit.php" method="post"> 
      <input type="hidden" name="id" value="<?php echo $id; ?>"/> 
      <table style="margin-left:auto; margin-right:auto; width:400px;"> 
       <tbody> 
        <tr style="text-align:center"> 
         <td colspan="2"><h2 style="color:#00008b;">Edit song into Music Database</h2><label style="color:#FF0000;"></label></td> 
        </tr> 

        <tr> 
         <td>Title<label style="color:#FF0000;"></label></td> 
         <td><input type="text" name="title"></td> 
        </tr> 
        <tr> 
         <td>Artist<label style="color:#FF0000;"></label></td> 
         <td><input type="text" name="artist"></td> 
        </tr> 
        <tr> 
         <td>Genre<label style="color:#FF0000;"></label></td> 
         <td><input type="text" name="genre"></td> 
        </tr> 
        <tr> 
         <td>Language<label style="#FF0000;"></label></td> 
         <td><input type="text" name="language"></td> 
        </tr> 
        <tr> 
          <td>Lyrics: <label style="#FF0000;"></label></td> 
          <td><textarea name="lyrics" rows="5" cols="50"></textarea></td> 
         </tr> 
         <tr> 
          <td>Updated by<label style="#FF0000;"></label></td> 
          <td><input type="text" name="update"></td> 
         </tr> 
         <tr style="text-align:center"> 
          <td colspan="2"><input type="submit" name="submit" value="Submit"></td> 
         </tr> 
        </tbody> 
       </table> 
      </form> 
    </body> 
</html> 
+1

之前'的$ id = $ _ POST [ 'songid'];'把'$誤差= '';'和而不是'isset($錯誤)!= '''寫'如果(!空($ error))' –

+0

isset($ error)!=''這是什麼? –

+0

@KrisRoofe它將工作沒有問題 –

回答

4

只需使用isset($error)(刪除!=管線158)

1

我想你需要定義$error = '' befor $id = $_POST['songid'];

事情是這樣的: -

if (is_numeric(isset($_POST['songid']))) 

{ 

    // get form data, making sure it is valid 
    $error = ''; 
    $id = $_POST['songid']; 

    ...... 
    ...... 

,改變它。

if(!empty($error)) 
{ 
    echo '<div style="padding:4px; border:1px solid red;  color:red;">'.$error.'</div>'; 

} 
1

由於PHP沒有變量($error),所以您收到錯誤,因此它顯示未定義的變量。

所以在腳本開始處定義。

$error='';

+0

其他人告訴別的東西? –