我做了一個edit.php文件。它似乎工作,但它只顯示來自回波線(行尾)的錯誤。我似乎無法找到錯誤輸入或沒有輸入編碼的地方。編輯php文件只顯示錯誤
我應該怎麼做來解決這個問題?
<?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!';
}
}
?>
你在你的URL中添加'songid'參數停止PHP代碼的其餘部分?它是一個有效的數字('is_numeric')並且高於0? –
瀏覽器控制檯中是否有錯誤? –
@Frank M是的,我添加了它,是的它高於0 – Zirah