编辑php文件只显示错误

时间:2017-01-09 08:05:39

标签: php mysql

我制作了一个edit.php文件。它似乎工作但它只显示来自回声线的错误(行尾)。我似乎无法找到错误输入或未输入编码的地方。

This is what it shows.

我该怎么做才能解决这个问题?

<?php
/*
EDIT.PHP
Allows user to edit specific entry in database
*/
// creates the edit record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable

 function renderForm($songid, $title, $artist, $genre, $lyrics, $language, $error)

{

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>

  <head>

    <title>Edit Record</title>

  </head>

  <body>

  <?php

    // if there are any errors, display them

    if ($error != '')

    {

      echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';

    }

   ?>

    <form action="" method="post">

     <input type="hidden" name="songid" value="<?php echo $songid; ?>"/>

      <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 style="text-align:center">
    <td colspan="2"><input type="submit" name="submit" value="Submit"></td>
    </tr>

    <input type="submit" name="submit" value="Submit">



 </form>

 </body>

 </html>

 <?php

  } // continue end of function of renderform 


 // connect to the database

  include('connect-db.php');


  // 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($_POST['songid']))

 {

 // get form data, making sure it is valid

        $songid = (isset($_POST['songid']) ? $_POST['songid'] : null);

        $title = (isset($_POST['title']) ? $_POST['title'] : null);
        $artist = (isset($_POST['artist']) ? $_POST['artist'] : null);
        $genre = (isset($_POST['genre']) ? $_POST['genre'] : null);
        $lyrics = (isset($_POST['lyrics']) ? $_POST['lyrics'] : null);
        $language = (isset($_POST['language']) ? $_POST['language'] : null);



   // check that firstname/lastname fields are both filled in

   if ($title == '' || $artist == '' || $genre == '' || $lyrics == '' || $language == '')

   {

   // generate error message

    $error = 'ERROR: Please fill in all required fields!';

   //error, display form

    renderForm($songid, $title, $artist, $genre, $lyrics, $language, $error);

    }

   else

   {

    // save the data to the database

    mysql_query("UPDATE players SET title='$title', artist='$artist', genre='$genre', lyrics='$lyrics', language='$language' WHERE songid='$songid'")

    or die(mysql_error());

   // once saved, redirect back to the view page

    header("Location: view.php");

    }

}

else

{

// if the 'id' isn't valid, display an error

 echo 'Error!';

}

 }

  else

 // if the form hasn't been submitted, get the data from the db and display the form
  {

  // get the 'songid' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)

if (isset($_GET['songid']) && is_numeric($_GET['songid']) && $_GET['songid'] > 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'];

        $lyrics = $row['lyrics'];

        $language= $row['language'];


        // show form

        renderForm($songid, $title, $artist, $genre, $lyrics, $language, $error);

    }

else

    // if no match, display result

{

    echo "No results!";

}

}

else

// if the 'songid' in the URL isn't valid, or if there is no 'songid' value, display an error

{

echo 'Error!';

}

}

?>

3 个答案:

答案 0 :(得分:0)

您可以定义函数但不执行。请检查一下:

<?php
/*
EDIT.PHP
Allows user to edit specific entry in database
*/
// creates the edit record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable

renderForm();

 function renderForm($songid, $title, $artist, $genre, $lyrics, $language, $error)

{...}

答案 1 :(得分:0)

根据您的第一个屏幕截图,您在网址中使用id而不是songid

尝试将id更改为songid,例如localhost/songdb/edit.php?songid=14

答案 2 :(得分:0)

你的代码有一些语法错误,但我修复了一些注意事项是我整理你的代码并移动一些东西也注意到你在使用MySQL时不推荐使用MySQL所以我把它作为MySQLI我给你的方式更改标题位置时也连接到您的数据库,建议您放置exit();然后停止运行

的其余PHP代码
 <?php
include('connect-db.php');
function renderForm($songid, $title, $artist, $genre, $lyrics, $language, $error){

}
if (isset($_POST['submit'])){
    if (is_numeric($_POST['songid'])){
        $songid = (isset($_POST['songid']) ? $_POST['songid'] : null);
        $title = (isset($_POST['title']) ? $_POST['title'] : null);
        $artist = (isset($_POST['artist']) ? $_POST['artist'] : null);
        $genre = (isset($_POST['genre']) ? $_POST['genre'] : null);
        $lyrics = (isset($_POST['lyrics']) ? $_POST['lyrics'] : null);
        $language = (isset($_POST['language']) ? $_POST['language'] : null);
        if ($title == null || $artist == null || $genre == null || $lyrics == null || $language == null){
            $error = 'ERROR: Please fill in all required fields!';
        } else {
            $conn = new mysqli('host', 'username', 'password', 'DB');// This will be located in your connect.php but the refrence variable("$conn") is important if you are going to use this php
            $conn->query("UPDATE players SET '$title', artist='$artist', genre='$genre', lyrics='$lyrics', language='$language' WHERE songid='$songid''");
            header("Location: view.php");
            exit();

        }
    } else {
        $error = "Song Id is not valid";
    }
}
if (isset($_GET['songid']) && is_numeric($_GET['songid']) && $_GET['songid'] > 0){
    $songid = $_GET['songid'];
    "SELECT * FROM songs WHERE songid=$songid"
    $result = $conn->query("UPDATE players SET '$title', artist='$artist', genre='$genre', lyrics='$lyrics', language='$language' WHERE songid='$songid''");
    $row = $result->fetch_assoc();
    if(mysqli_num_rows($result) > 0){
        $title = $row['title'];
        $artist = $row['artist'];
        $genre = $row['genre'];
        $lyrics = $row['lyrics'];
        $language= $row['language'];
        renderForm($songid, $title, $artist, $genre, $lyrics, $language, $error); // Still not quite sure what you are doing with this function because there is no function made on this current php script

    } else {
        $error = "No results!";
    }
} else {
    $error = "Song id in URL is not valid";
}
?>
<html>
  <head>
    <title>Edit Record</title>
  </head>
  <body>
  <?php
    if ($error){
      echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
    }
   ?>
    <form action="" method="post">
     <input type="hidden" name="songid" value="<?php echo $songid; ?>"/>
      <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 style="text-align:center">
    <td colspan="2"><input type="submit" name="submit" value="Submit"></td>
    </tr>

    <input type="submit" name="submit" value="Submit">
 </form>
 </body>
 </html>