我收到错误说
注意:未定义的变量:C:\ xampp \ htdocs \ songdb \ edit.php中的错误 在第158行
我已经尝试将isset()
放在$error
中,但它仍然无效。
编辑: 我已经删除!=只是使用isset($ error)但是给我这个
解析错误:语法错误,意外' {'在第158行的C:\ xampp \ htdocs \ songdb \ edit.php
edited2:
我添加了$ error ='&#39 ;;并使用isset($error)
但是当我单击提交按钮而不键入所有字段时,它可以正常工作,但它没有像我希望的那样显示错误消息。
编辑3:
我在$ connect和$ database之后添加了函数renderForm($ songid,$ title,$ artist,$ genre,$ language,$ lyrics,$ update),$ error ='错误:请填写所有必填字段!&#39 ;;并在if($ row)之后但它给了我这个错误
解析错误:语法错误,第218行的C:\ xampp \ htdocs \ songdb \ edit.php中的文件意外结束
如果没有这个代码的解决方案,你能给我一个替代/另一个编码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>
答案 0 :(得分:4)
只需使用isset($error)
(在第158行删除!=
)
答案 1 :(得分: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>';
}
答案 2 :(得分:1)
您收到错误,因为PHP没有变量($error
),所以它显示未定义的变量。
所以在脚本的开头定义。
$error='';