您的位置:首页 > 编程语言 > PHP开发

php之连接到MySQL,并将数据储存在MySQL中的实例

2016-08-03 20:07 513 查看
这个小型项目要求:发布一个网页寻找自己被劫持到外星球的狗狗,现在希望知情人能提供一些线索。网友要填的信息为:

名字
什么时候去的外星球
在那呆了多久
是否看到我的狗狗
您的邮箱
将网友的信息储存到数据库MySQL中

具体实现如下

首先是web网页;

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="xungouqishi.php" method="post" target="_blank">
<label for="name">您的名字是:</label>
<input type="text" id="name" name="name"/><br><br>
<label for="time">您什么时候去的外星球:</label>
<input type="text" id="time" name="time"/><br><br>
<label for="howlong">您在那儿呆了多长时间:</label>
<input type="text" id="howlong" name="howlong"/><br><br>
<label for="description">请您描述一下您所见到的外星人:</label>
<input type="text" id="description" name="description"/><br><br>
<label for="email">您的email是:</label>
<input type="text" id="email" name="email"/><br><br>
<label for="dog">您看到我可爱的狗了吗?</label>
<label for="dog">Yes </label><input type="radio" id="dog" name="dog" value="yes" />
<label for="dog">No</label><input type="radio" id="dog" name="dog" value="no" />
<button>提交</button>
<button>重置</button>
</form>
</body>
</html>


php文件如下:

<html>

<head>

<meta charset="utf-8">

<title>寻狗启示</title>

</head>

<body>

<?php

$name=$_POST["name"];

$time=$_POST["time"];

$howlong=$_POST["howlong"];

$description=$_POST["description"];

$dog=$_POST["dog"];

$email=$_POST["email"];

echo"Thanks for submitting the form!<br>";

echo "you are abducted is:".$time."<br>";

echo "you are there:".$howlong."<br>";

echo "your description is:".$description."<br>";

echo "you have saw my dog?:".$dog."<br>";

echo "your email is:".$email."<br>";

$link=mysql_connect("127.0.0.1","root","root")

or die("连接失败!");

mysql_select_db("php",$link);

$ct="insert into xungouqishi(name ,time ,howlong,description ,dog,email)"."value('$name','$time','$howlong','$description','$dog','$email')";

$result=mysql_query($ct,$link)

or die("建立数据表失败!".mysql_error());

mysql_close($link);

//问题总结:1,MySQL识别不了中文:D:PHPstudy/mysql/my.ini:character-set-server=gbk->utf8;

// 2,要严格按照此思路来操作MySQL。

// 3,提交一次表单后,其他人提交不了。提示的错误是:数据表已经存在:
不能在此处建表,否则就会重复建表,肯定会出错

?>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql php 数据库 web