html中表单post方法与php还有sql交互的最简单demo

来源:互联网 发布:网络推广代理公司 编辑:程序博客网 时间:2024/04/30 20:14

index.php

<!DOCTYPE HTML><html><head><meta charset="utf-8"><title>主页</title></head><body><form action="index.handle.php" method="post">名字: <input type="text" name="fname"><br/>年龄: <input type="text" name="age"><br/><input type="submit" value="提交"></form></body></html>

config.php

<?php    header("Content-type: text/html; charset=utf-8");    define('HOST', 'xxx');    define('USERNAME', 'xxx');    define('PASSWORD', 'xxx');?>

connect.php

<?php    require_once('config.php');    //连库    if(!($con = mysql_connect(HOST, USERNAME, PASSWORD))){        echo mysql_error();    }    //选库    if(!mysql_select_db('test')){        echo mysql_error();    }    //字符集    if(!mysql_query('set names utf8')){        echo mysql_error();    }?>

index.handle.php

<?php    require_once('connect.php');    if((empty($_POST['fname'])) || (empty($_POST['age']))){        echo "<script>alert('均不能为空');window.location.href='index.php';</script>";    }     $username = $_POST['fname'];    $passwd = $_POST['age'];    $insertsql = "insert into login values('$username', '$passwd')";    if(mysql_query($insertsql)){        echo "<script>alert('成功');window.location.href='index.php';</script>";    }else{        echo "<script>alert('失败');window.location.href='index.php';</script>";    }?>

数据库 名称为test

create table login(varchar(10) username primary key,varchar(10) password);
0 0
原创粉丝点击