我正试图让我的 PHP/HTML 重新开始,我已经开始设计我自己的小新闻/任何系统。为了提高效率,我想做的是通过一个开关($x)从一个 process.php 文件运行添加/编辑/删除所有操作,但由于某种原因它不会插入任何数据,它不会'不要给我任何错误。我完全不知道在这里做什么。如果有人能帮我解决这两个文件的代码如下。
进程.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>
最佳答案
这段代码:
if (!isset($_GET['x'])) {
$x = $_GET[x];
应该是:
if (isset($_GET['x'])) {
$x = $_GET['x'];
您进行了反向测试,因此在设置参数时您没有进入开关。
关于php - 不向数据库中插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19614021/