Rumah > Artikel > pembangunan bahagian belakang > php接收post数据并写入mysql有关问题
php接收post数据并写入mysql问题
求专家解答:php如何接收远程post过来的txt文本内容并同时写入mysql数据库呢?求完整代码!合适可加分!
txt文本内容字段格式:
学生id、学生名字、学生邮箱、学生地址
------解决思路----------------------
数据表:
<br />CREATE TABLE `student` (<br /> `id` int(10) unsigned NOT NULL auto_increment,<br /> `xid` varchar(20) NOT NULL,<br /> `name` varchar(20) NOT NULL,<br /> `email` varchar(100) NOT NULL,<br /> `address` varchar(100) NOT NULL,<br /> PRIMARY KEY (`id`)<br />) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;<br />
<br /><?php<br />// 连接数据库 <br />[email protected]_connect("数据库ip","帐号","密码") or die(mysql_error()); <br />@mysql_select_db('数据库名',$conn) or die(mysql_error()); <br /><br />$action = isset($_REQUEST['action'])? $_REQUEST['action'] : '';<br /><br />if($action=='add'){<br /> $xid = isset($_POST['xid'])? mysql_escape_string($_POST['xid']) : '';<br /> $name = isset($_POST['name'])? mysql_escape_string($_POST['name']) : '';<br /> $email = isset($_POST['email'])? mysql_escape_string($_POST['email']) : '';<br /> $address = isset($_POST['address'])? mysql_escape_string($_POST['address']) : '';<br /><br /> if($xid=='' <br><font color='#FF8000'>------解决思路----------------------</font><br> $name=='' <br><font color='#FF8000'>------解决思路----------------------</font><br> $email=='' <br><font color='#FF8000'>------解决思路----------------------</font><br> $address==''){<br /> echo 'please input data';<br /> exit();<br /> }<br /><br /> $sqlstr = "insert into student(xid,name,email,address) values('".$xid."','".$name."','".$email."','".$address."')";<br /> mysql_query($sqlstr) or die(mysql_error());<br /><br /> header('location:postdemo.php');<br /><br />}else{<br />?><br /><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><br /><html><br /> <head><br /> <meta http-equiv="content-type" content="text/html; charset=utf-8"><br /> <title>demo</title><br /> </head><br /><br /> <body><br /> <form name="form1" method="post" action="postdemo.php"><br /> <p>学生id:<input type="text" name="xid"></p><br /> <p>学生名字:<input type="text" name="name"></p><br /> <p>学生邮箱:<input type="text" name="email"></p><br /> <p>学生地址:<input type="text" name="address"></p><br /> <p><input type="hidden" name="action" value="add"><input type="submit" name="b1" value="提交"></p><br /> </form><br /><br /> <?php<br /> $sqlstr = "select * from student order by id";<br /> $query = mysql_query($sqlstr) or die(mysql_error());<br /> $result = array();<br /> while($thread=mysql_fetch_assoc($query)){<br /> $result[] = $thread;<br /> }<br /><br /> if($result){<br /> echo '<table>';<br /> echo '<th>NO</th><th>学生id</th><th>学生名字</th><th>学生邮箱</th><th>学生地址</th>';<br /> foreach($result as $row){<br /> echo '<tr>';<br /> echo '<td>'.$row['id'].'</td>';<br /> echo '<td>'.$row['xid'].'</td>';<br /> echo '<td>'.$row['name'].'</td>';<br /> echo '<td>'.$row['email'].'</td>';<br /> echo '<td>'.$row['address'].'</td>';<br /> echo '</tr>';<br /> }<br /> echo '</table>';<br /> }<br /><br /> ?><br /> <br /> </body><br /></html><br /><?php<br />}<br />?><br />