首先建立一个简陋的表单上传数据(issue.php)
1 <html> 2 <head> 3 </head> 4 <body> 5 <form action = "store.php" method = "post"> 6 content title:<input type = "text" name = "content_tytle" maxlength="30" size="30"/> 7 <br /> 8 content author:<input type = "text" name = "content_auther" maxlength="20" size="30"/> 9 <br /> 10 text_area:<textarea rows = "20" cols = "40" name="content_details"> </textarea> 11 <br /> 12 <input type="submit" value="confirm_isue" /> 13 </form> 14 </body> 15 </html>
测试一下代码是否有效。
创建数据库和表(contentdata.php)
<?php $con = mysql_connect("localhost","root",""); if(!$con) { die("could not connect!:".mysql_error()); } $create_database = "CREATE DATABASE Content"; mysql_query($create_database,$con) or die().mysql_error(); mysql_select_db("Content"); $create_table = "CREATE TABLE Con_info( Con_infoID int NOT NULL AUTO_INCREMENT, PRIMARY KEY(Con_infoID), title varchar(30), auther varchar(20), details varchar(500) )"; mysql_query($create_table) or die().mysql_error(); $content_tytle = $_POST["content_tytle"]; $content_auther = $_POST["content_auther"]; $content_details = $_POST["content_details"]; //echo "$content_tytle"; //echo "<br />"; //echo "$content_auther"; //echo "<br />"; $insert = "INSERT INTO Con_info(title,auther,details) VALUES ('$content_tytle','$content_auther','$content_details')"; mysql_query($insert) or die().mysql_error(); mysql_close($con); mysql_close($con); ?>
通过$_POST[]接收数据,然后将数据存放到数据库中。在这之前新建一个数据库,创建表,然后将数据插入到表中完成数据库的增操作。
我注释了几个echo语句,去掉注释看看是否值已经正确传递。
数据库的查操作(contentlist.php)
<html> <head> </head> <body> <center> <a href = "issue.php">issue content</a> <br /> <table cellpadding="50"> <caption>content list</caption> <tr> <th>auther</th> <th>title</th> </tr> <?php $con=mysql_connect("localhost","root",""); if(!$con) { die("could not connect!:".mysql_error()); } mysql_select_db("Content"); $obtain = mysql_query("SELECT * FROM Con_info") or die().mysql_errno(); while($show=mysql_fetch_array($obtain)) { echo "<tr>"; echo "<td>".$show['title']."</td>"; echo "<td>".$show['auther']."</td>"; echo "<td><a href='delete.php?id={$show['title']}'>delete</a></td>"; echo "</tr>"; } ?> </table> </center> </body> </html>
这段代码可以看作者和文章的题目,后面接了一个删除的超链接。使用url传值的方法将要删除的内容传到下一个页面,通过$_GET[]来取得url传递的值。
数据库的删除操作(delete.php)
<html> <head> </head> <body> <center> <?php $d=$_GET["id"]; $con=mysql_connect("localhost","root",""); if(!$con) { die("could not connect:".mysql_error()); } mysql_select_db("Content"); $delete="DELETE FROM Con_info WHERE title='".$d."'"; if(mysql_query($delete)) { echo "delete sucess!"; } else { mysql_query($delete).mysql_error(); } ?> <a href="contentlist.php">back to contentlist</a> </center> </body> </html>
这些代码都是很基础的操作,在做这些基础功能的时候,问题往往存在于没有接收到上一个页面传来的值。多用echo语句或者var_dump输出看看有没有接收到值。问题往往是变量拼写错误,或者变量没有加引号等错误。还有就是在做删除操作的时候用表单提交到下一个页面是不太实际的,采用url传值是一个不错的选择。