2013-10-27 192 views
0

我试图让我的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

你是否从命令行运行查询?您是否验证过该查询是正确的? –

+1

[请不要在新代码中使用'mysql_ *'函数](http://bit.ly/phpmsql)。他们不再被维护[并被正式弃用](https://wiki.php.net/rfc/mysql_deprecation)。看到[红框](http://j.mp/Te9zIL)?学习[*准备的语句*](http://j.mp/T9hLWi),并使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [这篇文章](http://j.mp/QEx8IB)将帮助你决定哪个。如果你选择PDO,[这里是一个很好的教程](http://j.mp/PoWehJ)。 **你也开放[SQL注入](http://stackoverflow.com/q/60174)** –

+0

@JohnConde这只是通过XAMPP在我的电脑上运行,我已经锁定了一切。我只是试图让基础知识失效。感谢您的链接,顺便说一句! –

回答

3

此代码:

if (!isset($_GET['x'])) { 
    $x = $_GET[x]; 

应该是:

if (isset($_GET['x'])) { 
    $x = $_GET['x']; 

您已经测试过了,所以当参数设置完成后,您不会进入开关。