- <?php
- header('Content-Type:text/html;charset=utf-8');
- $locahost="localhost";
- $name="root";
- $password="root";
- $dbname='test';
- $con=mysql_connect($locahost,$name,$password,$dbname);
- if (!$con) {
- die('链接失败'.mysql_error());
- }
- mysql_select_db("test",$con);
- if (isset($_POST['submit'])) {
- if (empty($_POST['title'])) {
- echo "<script>alert('请输入标题');</script>";
- exit(0);
- }
- if (empty($_POST['count'])) {
- echo "<script>alert('请输入计算');</script>";
- exit(0);
- }
- if (strlen($_POST['title'])<2||strlen($_POST['count'])>10) {
- echo "<script>alert('题目2到10之间');</script>";
- return true;
- }
- // 添加数据
- $sql="insert into ji(title,count)values('$_POST[title]','$_POST[count]')";
- $reqult=mysql_query($sql);
- if (!$reqult) {
- die('Error:'.mysql_error());
- }
- echo "添加成功";
- mysql_close($con);
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- </head>
- <body>
- <form action="jifen.php" method="post">
- 标题:<input type="text" name="title">
- 计算:<input type="text" name="count">
- <input type="submit" name="submit">
- </form>
- </body>
- </html>
- //该片段来自于http://www.codesnippet.cn/detail/1010201513775.html
来源: http://www.codesnippet.cn/detail/1010201513775.html