我正在努力让我的PHP / HTML重新开始,我开始设计自己的小新闻/任何系统。我想要提高效率的是通过一个开关($ x)从一个process.php文件中运行Add / Edit / Delete all,但由于某种原因它不会插入任何数据,它赢了给我任何错误。我完全迷失在这里做什么。如果有人可以帮我解决这两个文件的代码如下。
process.php
<?php
include("config.php");
if (!isset($_GET['x'])) {
$x = $_GET[x];
switch($x) {
case "add":
$title = $_POST['title'];
$text = $_POST['text'];
$date = $_POST['date'];
$author = $_POST['author'];
mysql_query("INSERT INTO posts(id, title, text, date, author) VALUES(null, '$title', '$text', '$date', '$author')") or die(mysql_error());
echo("Article inserted. Click <a href=\"index.php\" />here</a> to return.");
break;
case "gohome":
echo("Looks like you've taken a wrong turn. Click <a href=\"index.php\">here</a> to return.");
default:
echo("Go home.");
break;
}
} else {
$x = 'gohome';
}
?>
index.php(添加数据)
<html>
<head>
<link rel="stylesheet" type="text/css" href="includes/style.css" />
</head>
<body>
<div align="center" /><font size="20px;" />test</font></div>
<?php include("includes/navigation.php"); ?>
<div align="center" />
<fieldset><legend>Submit an article</legend>
<form action="includes/process.php?x=add" method="post" />
<input name="title" type="text" value="Title" onfocus="if(this.value=='Title') this.value='';" onblur="if(this.value=='') this.value='Title';"/><br />
<input name="date" type="text" value="Date" onfocus="if(this.value=='Date') this.value='';" onblur="if(this.value=='') this.value='Date';"/><br />
<textarea rows="4" cols="50" name="text" /></textarea><br />
<input name="author" type="text" value="Author" onfocus="if(this.value=='Author') this.value='';" onblur="if(this.value=='') this.value='Author';"/><br />
<input type="submit" />
</form>
</fieldset>
</body>
</html>
答案 0 :(得分:3)
此代码:
if (!isset($_GET['x'])) {
$x = $_GET[x];
应该是:
if (isset($_GET['x'])) {
$x = $_GET['x'];
你向后测试,所以当设置参数时你没有进入开关。