编辑php文件只显示错误
问题描述:
我做了一个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!';
}
}
?>
答
显然你定义了函数但没有执行。请检查:
<?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)
{...}
答
你的代码有一些语法错误,但我固定它也是东西要注意的是我收拾你的代码,还可以将一些东西,周围也注意到你正在使用MySQL时,它是不赞成,所以我做了MySQLI,我也给你连接数据库的方式,当改变标题位置时,建议你把exit();事后从运行
<?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>
你在你的URL中添加'songid'参数停止PHP代码的其余部分?它是一个有效的数字('is_numeric')并且高于0? –
浏览器控制台中是否有错误? –
@Frank M是的,我添加了它,是的它高于0 – Zirah