Home > Backend Development > PHP Tutorial > 求php实现代码:php 接收post 数据并保存到mysql数据库

求php实现代码:php 接收post 数据并保存到mysql数据库

WBOY
Release: 2016-06-23 13:46:42
Original
1444 people have browsed it


我需要用C++ 通过libcurl post 数据到服务器, C++ 代码已经实现,现需要实现 服务器端接收代码,服务器端代码需要用php编写(php 小白,完全不会,故求助), 数据有两个字段, 一个是 content, 一个是url  服务器接收到数据保存到本地mysql 数据库里。


table 结构
CREATE TABLE `tcontent` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `url` varchar(1000) NOT NULL,
   ‘content’ mediumtext  NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

数据库名称 mydatabase, 用户名 root 密码  123456 ,localhost


回复讨论(解决方案)

$conn=@mysql_connect("localhost","root","123456")  or die(mysql_error());   // localhost是添你db的host,如果不是本地的请修改这里
@mysql_select_db('mydatabase',$conn) or die(mysql_error()); 

$url= $_POST["url"];
$content = $_POST["content"];
echo "url:";
echo $url;
echo "
";

echo "content:";
echo $content;
echo "
";

if($url){
if($content)
{
    $sqlstr = "insert into tcontent(url,content) values('".$url."','".$content."')";
    mysql_query($sqlstr) or die(mysql_error());
    echo 'insert success';
}
}
mysql_close($conn);

?>
自己搞定了,只是依样画葫芦,不明白啥意思。

	$dsn = 'mysql:host=localhost;dbname=mydatabase';	$db = new PDO($dsn,'root','123456');	$sql = "insert into tcontent(url,content)values(?,?)";	$pp = $db->prepare($sql);	$pp->execute(array($_POST['url'],$_POST['content']));	//echo $db->lastInsertId();
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template