当前位置: 代码迷 >> PHP >> php接收post数据并写入mysql有关问题
  详细解决方案

php接收post数据并写入mysql有关问题

热度:16   发布时间:2016-04-28 18:24:33.0
php接收post数据并写入mysql问题
求专家解答:php如何接收远程post过来的txt文本内容并同时写入mysql数据库呢?求完整代码!合适可加分!
txt文本内容字段格式:
学生id、学生名字、学生邮箱、学生地址

------解决思路----------------------
数据表:

CREATE TABLE `student` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `xid` varchar(20) NOT NULL,
  `name` varchar(20) NOT NULL,
  `email` varchar(100) NOT NULL,
  `address` varchar(100) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


postdemo.php

<?php
// 连接数据库  
[email protected]_connect("数据库ip","帐号","密码")  or die(mysql_error());  
@mysql_select_db('数据库名',$conn) or die(mysql_error());  

$action = isset($_REQUEST['action'])? $_REQUEST['action'] : '';

if($action=='add'){
    $xid = isset($_POST['xid'])? mysql_escape_string($_POST['xid']) : '';
    $name = isset($_POST['name'])? mysql_escape_string($_POST['name']) : '';
    $email = isset($_POST['email'])? mysql_escape_string($_POST['email']) : '';
    $address = isset($_POST['address'])? mysql_escape_string($_POST['address']) : '';

    if($xid=='' 
------解决思路----------------------
 $name=='' 
------解决思路----------------------
 $email=='' 
------解决思路----------------------
 $address==''){
        echo 'please input data';
        exit();
    }

    $sqlstr = "insert into student(xid,name,email,address) values('".$xid."','".$name."','".$email."','".$address."')";
    mysql_query($sqlstr) or die(mysql_error());

    header('location:postdemo.php');

}else{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title>demo</title>
 </head>

 <body>
  <form name="form1" method="post" action="postdemo.php">
  <p>学生id:<input type="text" name="xid"></p>
  <p>学生名字:<input type="text" name="name"></p>
  <p>学生邮箱:<input type="text" name="email"></p>
  <p>学生地址:<input type="text" name="address"></p>
  <p><input type="hidden" name="action" value="add"><input type="submit" name="b1" value="提交"></p>
  </form>

 <?php
    $sqlstr = "select * from student order by id";
    $query = mysql_query($sqlstr) or die(mysql_error());
    $result = array();
    while($thread=mysql_fetch_assoc($query)){
        $result[] = $thread;
    }

    if($result){
        echo '<table>';
        echo '<th>NO</th><th>学生id</th><th>学生名字</th><th>学生邮箱</th><th>学生地址</th>';
        foreach($result as $row){
            echo '<tr>';
            echo '<td>'.$row['id'].'</td>';
            echo '<td>'.$row['xid'].'</td>';
            echo '<td>'.$row['name'].'</td>';
            echo '<td>'.$row['email'].'</td>';
            echo '<td>'.$row['address'].'</td>';
            echo '</tr>';
        }
        echo '</table>';
    }

 ?>
  
 </body>
</html>
<?php
}
?>
  相关解决方案