码迷,mamicode.com
首页 > 移动开发 > 详细

安卓版php服务器的mysql数据库增删改查简单案例

时间:2016-04-28 00:33:56      阅读:331      评论:0      收藏:0      [点我收藏+]

标签:

index.php文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>手机网站主页</title>
<style type="text/css">
    body{margin:0px;padding:0px;font-size:40px;}
    .box{width:800px;height:1100px;border:solid 2px #808080;margin:80px auto 10px auto;}
    ul{margin:60px 50px; list-style-type:none;}
    h2{text-align:center;}
    .textStyle{color:red;}
    .a_under{float:left;margin:5px 0px 0px 250px;}
    </style>
    </head>
<body>
    <h2>手机网站主页</h2>
    <div class="box">
    <ul>
        <li><a href="phpinfo.php" target="_blank" >php信息</a></li>
        <li>&nbsp;</li>
        <li>&nbsp;</li>                                                   
        <li><a href="mysqlRead.php" target="_blank" >数据库读取(mysqlRead.php)</a></li>
        <li><a href="mysqlEdit.php" target="_blank" >数据库操作(mysqlEdit.php)</a></li>
        <li><a href="http://localhost:8080/phpMyAdmin" target="_blank" >phpMyAdmin数据库管理</a></li>
        <li>&nbsp;</li>
        <li>&nbsp;</li>
        <li><p class="textStyle">首次打开此页时,首先点击"创建数据库";然后点击"创建数据表";最后不可轻易操作以下内容,操作以下内容会影响数据库的结构!</p></li>
        <li><a href="createDatabase.php">创建数据库</a></li>
        <li><a href="createTable.php">创建数据表</a></li>
        <li><a href="dropTable.php">删除数据表</a></li>
    </ul>
    </div>
        <a href="http://alfanla.com/palapa-web-server/" target="_blank" class="a_under">帕垃帕安卓php服务器软件</a>
</body>
</html>

mysqlRead.php文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>手机网站</title>
<style>
body{}
    h1{text-align:center;}
    table{margin:40px auto;text-align:center;font-size:24px;border-collapse:collapse;}
    .box{width:100%;height:1200px;border:solid 1px #808080;}
    th{background:#5f9ea0;}
    a{font-size:36px;}
     .text_color{background:#ADD8E6;}
     .text_color_1{background:#E0FFFF;}
    </style>
     </head>
     <body>
     <h1>数据库读取</h1>
     <div class="box">
     
    <table border="1" width="90%">
     <tr>
       <th>ID</th>
         <th>姓名</th>
           <th>年龄</th>
             <th>简介</th>    
    </tr>
<?php
header("content-type:text/html;charset=utf-8");
include_once(‘mysqlAccount.php‘);
$conn = mysql_connect(constant("HOST"), constant("userName"), constant("password"));
mysql_query("set names ‘utf8‘");
mysql_select_db(constant("database_db"));
$sql = "select * from pw_luck";
$tag = true;

$result = mysql_query($sql, $conn);

while ($row = mysql_fetch_array($result)) {

    if ($tag) {

        echo "<tr><td class=‘text_color_1‘>" . $row["id"] .
            "</td><td class=‘text_color_1‘>" . $row["name"] .
            "</td><td class=‘text_color_1‘>" . $row["age"] .
            "</td><td class=‘text_color_1‘>" . $row["intro"] . "</td></tr>";
        $tag = false;

    } else {

        echo "<tr><td class=‘text_color‘>" . $row["id"] . "</td><td class=‘text_color‘>" .
            $row["name"] . "</td><td class=‘text_color‘>" . $row["age"] .
            "</td><td class=‘text_color‘>" . $row["intro"] . "</td></tr>";
        $tag = true;

    }

}
mysql_free_result($result);
?>
     </table>
   </div>
    <a href="mysqlEdit.php">mysql数据库操作</a>
    <a href="index.php" style="float:right;">返回主页</a>
</body>
</html>

mysqlEdit.php文件:

<?php

/*
*mysql数据库的增删改查操作。
*/
header("content-type:text/html;charset=utf-8");

$ID = @$_POST[‘userId‘];
$name = @$_POST[‘userName‘];
$age = @$_POST[‘userAge‘];
$intro = @$_POST[‘userIntro‘];
include_once (‘mysqlAccount.php‘);
$conn = mysql_connect(constant("HOST"), constant("userName"), constant("password"));
mysql_query("set names ‘utf8‘");
mysql_select_db(constant("database_db"));

if (@$_POST[‘add‘] == ‘增加‘) {
    /*添加数据*/

    $sql = "insert into pw_luck values ($ID,‘$name‘,$age,‘$intro‘);";
    if (!mysql_query($sql, $conn)) {
        $err = mysql_error();

        echo (‘错误:‘ . mysql_error() . "<script>var err=\"$err\";alert(‘错误:‘ + err);location.href=‘mysqlEdit.php‘</script>");

    }
    mysql_close($conn);

}

if (@$_POST[‘modify‘] == "修改") {
    /*修改数据*/
    $sql = "select id from pw_luck where id=$ID";
    $result = mysql_query($sql, $conn);
    $row = mysql_fetch_array($result);
    if ($row[‘id‘] <> $ID) {
        echo "<script>alert(‘修改的数据ID值不在数据库的范围内,修改没有成功!‘)</script>";
    }
    mysql_free_result($result);
    $sql = "update pw_luck set name=‘$name‘,age=$age,intro=‘$intro‘ where id=$ID;";

    if (!mysql_query($sql, $conn)) {

        die(‘错误: ‘ . mysql_error() . "<br/><br/><a href=‘mysqlEdit.php‘>返回重新输入</a>");
    }

    mysql_close($conn);
}

if (@$_POST[‘delet‘] == "删除") {
    /*删除数据*/
    $sql = "delete from pw_luck where id=$ID;";

    if (!mysql_query($sql, $conn)) {

        die(‘错误: ‘ . mysql_error() . "<br/><br/><a href=‘mysqlEdit.php‘>返回重新输入</a>");
    }

    mysql_close($conn);
}

if (@$_POST[‘select‘] == "查询") {
?>
<div class="box_out">
 
<table border="1" width="90%" class="tb_out">
 <tr>
   <th>ID</th>
     <th>姓名</th>
       <th>年龄</th>
         <th>简介</th>
      
</tr>
<?php
    echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
    echo "<html><head><title>Mysql数据库查询结果</title>";
    echo "<style>
table.tb_out{margin:40px auto;text-align:center;font-size:24px;border-collapse:collapse;}.box_out{width:100%;height:1200px;border:solid 1px #808080;}th{background:#5f9ea0;} .text_color{background:#ADD8E6;} .text_color_1{background:#E0FFFF;} a{font-size:36px;}</style>";
    echo "</head><body>";
    echo "<center><h1>Mysql数据库查询结果</h1></center>";
    $ID_sql = "id=‘$ID‘";
    /*注意:这里只能用双引号,否则查询时出错,并且变量必须用单引号括起来!*/
    $name_sql = "name LIKE ‘%$name%‘";
    $age_sql = "age=‘$age‘";
    $intro_sql = "intro LIKE ‘%$intro%‘";
    if ($ID == ‘‘ or $ID == ‘NULL‘)
        $ID_sql = true;
    if ($name == ‘‘)
        $name_sql = true;
    if ($age == ‘‘)
        $age_sql = true;
    if ($intro == ‘‘)
        $intro_sql = true;
    $sql = "select * from pw_luck where $ID_sql AND $name_sql AND $age_sql AND $intro_sql;";
    //echo $sql;  /*检查sql语句的格式是否正确*/
    $tag = true;

    $result = mysql_query($sql, $conn);

    while ($row = mysql_fetch_array($result)) {

        if ($tag) {

            echo "<tr><td class=‘text_color_1‘>" . $row["id"] .
                "</td><td class=‘text_color_1‘>" . $row["name"] .
                "</td><td class=‘text_color_1‘>" . $row["age"] .
                "</td><td class=‘text_color_1‘>" . $row["intro"] . "</td></tr>";
            $tag = false;

        } else {

            echo "<tr><td class=‘text_color‘>" . $row["id"] . "</td><td class=‘text_color‘>" .
                $row["name"] . "</td><td class=‘text_color‘>" . $row["age"] .
                "</td><td class=‘text_color‘>" . $row["intro"] . "</td></tr>";
            $tag = true;

        }

    }
    mysql_free_result($result);
?>
    </table>
    
</div>
    <a href="mysqlEdit.php">返回数据库操作界面</a>&nbsp;<a href="index.php">主页</a>
    </body>
    </html>
<?php
    exit();
    /*查询时截断下面的脚本显示,不显示下面的界面*/
}
?>

<!-------下面是界面代码,上面是后台处理代码------->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<head>

<title>Mysql数据库操作</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
    function addtext(){
             event.returnValue=confirm("确定增加数据吗?");
             if(event.returnValue){   
                    if(document.getElementById("userId").value==""){
                          alert("ID不能为空,请重新输入!");
                        event.returnValue=false;
                    }else if(document.getElementById("name").value==""){
                        alert("姓名不能为空,请重新输入!");
                        event.returnValue=false;
                    }else if(document.getElementById("age").value==""){
                        alert("年龄不能为空,请重新输入!");
                        event.returnValue=false;
                    }else if(document.getElementById("age").value==0){
                        alert("年龄不能为0岁,请重新输入!");
                        event.returnValue=false;
                    }else if(document.getElementById("age").value>100){
                        alert("年龄不能超过100岁,请重新输入!");
                        event.returnValue=false;
                    }else if(document.getElementById("intro").value==""){
                        event.returnValue=confirm("你确定不需要给此人一个简介吗?");
                    }
    
              }
     }
    

   function modifytext(){
            event.returnValue=confirm("确定修改数据吗?");
            if(event.returnValue){
                    var idValue=document.getElementById("userId").value;
                    if(idValue=="" || idValue=="NULL"){
                        alert("修改数据是基于ID值来判断的,因此ID值不能为空或为NULL!");
                        event.returnValue=false;
                    }else if(document.getElementById("name").value==""){
                        alert("姓名不能为空,请重新输入!");
                        event.returnValue=false;
                    }else if(document.getElementById("age").value==""){
                        alert("年龄不能为空,请重新输入!");
                        event.returnValue=false;
                    }else if(document.getElementById("age").value==0){
                        alert("年龄不能为0岁,请重新输入!");
                        event.returnValue=false;
                    }else if(document.getElementById("age").value>100){
                        alert("年龄不能超过100岁,请重新输入!");
                        event.returnValue=false;
                    }else if(document.getElementById("intro").value==""){
                        event.returnValue=confirm("你确定不需要给此人一个简介吗?");
                                           }
    

            }
        }
    

   function delettext(){
            var idValue=document.getElementById("userId").value;
            event.returnValue=confirm("确定删除数据吗?");
            if(event.returnValue){
                if(idValue=="" || idValue=="NULL"){
                    alert("删除数据是基于ID值来判断的,因此ID值不能为空或为NULL,其它不用填写!");
                    event.returnValue=false;
                }
            }
      }

function selecttext(){
         event.returnValue=confirm("确定查询数据吗?");    
    }
</script>
<style type="text/css">
    body{font-size:150%;}
    .box{border:solid 1px #808080;width:900px;height:900px; margin:100px auto;background:#5F9EA0;border-radius:30px;}
    h1{text-align:center;font-size:320%;}
    table.tb{width:100%;height:800px;font-size:50px;margin-top:50px;}    
    input{width:90%;height:80px;font-size:50px;}
    .left{text-align:right;width:200px;}
    .inp{background:#9932CC;}
    .inp:hover{background:#D2691E;}
    a{font-size:50px;}
    table.tb_out{margin:40px auto;text-align:center;font-size:24px;border-collapse:collapse;}
    .box_out{width:100%;height:1200px;border:solid 1px #808080;}
    th{background:#5f9ea0;}
    .text_color{background:#ADD8E6;}
    .text_color_1{background:#E0FFFF;}
</style>
</head>
<body>
     <h1>Mysql数据库操作</h1>
     <div class="box">
    <form action="" name="formPage" method="POST">
       <table border="0" class="tb">
            <tr>
                <td class="left">ID:</td>
                <td><input type="text" name="userId" id="userId" value="NULL" onfocus="javascript:if(this.value==‘NULL‘)this.value=‘‘;" onkeyup="this.value=this.value.replace(/\D/g,‘‘)" onafterpaste="this.value=this.value.replace(/\D/g,‘‘)"></td>
            </tr>
            <tr>
                <td class="left">姓名:</td>
                <td><input type="text" name="userName" id="name"></td>
            </tr>
            <tr>
                <td class="left">年龄:</td>
                 <td><input type="text" name="userAge" id="age" onkeyup="this.value=this.value.replace(/\D/g,‘‘)" onafterpaste="this.value=this.value.replace(/\D/g,‘‘)"></td>
            </tr>    
            <tr>
                <td class="left">简介:</td>
                <td><input type="text" name="userIntro" id="intro"></td>
            </tr>    
            <tr>
                <td></td>
                <td><input class="inp" type="submit" name="add" value="增加" onclick="addtext()"></td>
            </tr>
            <tr>    
                  <td></td>
                <td><input class="inp" type="submit" name="modify" value="修改" onclick="modifytext()"></td>
            </tr>    
            <tr>
                <td></td>
                <td><input class="inp" type="submit" name="delet" value="删除" onclick="delettext()"></td>
            </tr>
            <tr>
                <td></td>
                <td><input class="inp" type="submit" name="select" value="查询" onclick="selecttext()"></td>
            </tr>
        </table>
    </form>
    </div>
        <a href="mysqlRead.php">Mysql数据库读取</a>
        <a href="index.php" style="float:right;">返回主页</a>
</body>
</html>

createDatabase.php文件:

<?php
header("content-type:text/html;charset=utf-8");
include_once (‘mysqlAccount.php‘);
$conn = mysql_connect(constant("HOST"), constant("userName"), constant("password"));
mysql_query("set names ‘utf8‘");
$database_db = constant("database_db");
$sql = "create database $database_db";

if (!mysql_query($sql, $conn)) {

    die(‘Error: ‘ . mysql_error());
}
mysql_close($conn);
echo "创建数据库!";
echo "<script>location.href=‘index.php‘</script>";

?>

createTable.php文件:

<?php
header("content-type:text/html;charset=utf-8");
include_once(‘mysqlAccount.php‘);
$conn = mysql_connect(constant("HOST"), constant("userName"), constant("password"));
mysql_query("set names ‘utf8‘");

mysql_select_db(constant("database_db"));

$sql = "create table `pw_luck`(`id` int(10) not null auto_increment primary key,`name` varchar(10) not null,`age` int(10) not null,`intro` varchar(100));";

if (!mysql_query($sql, $conn)) {

    die(‘Error: ‘ . mysql_error());
}
mysql_close($conn);
echo "创建数据表!";
echo "<script>location.href=‘index.php‘</script>";

?>

dropTable.php文件:

<?php
header("content-type:text/html;charset=utf-8");

include_once (‘mysqlAccount.php‘);
$conn = mysql_connect(constant("HOST"), constant("userName"), constant("password"));
mysql_query("set names ‘utf8‘");

mysql_select_db(constant("database_db"));

$sql = "drop table pw_luck;";

if (!mysql_query($sql, $conn)) {

    die(‘Error: ‘ . mysql_error());
}
mysql_close($conn);
echo "删除数据表!";
echo "<script>location.href=‘index.php‘</script>";
?>

mysqlAccount.php配置文件:

<?php
define("HOST", "localhost");/*mysql数据库连接地址*/
define("userName", "root");/*mysql数据库用户名*/
define("password", "");/*mysql数据库密码*/
define("database_db", "test");/*mysql数据库的数据表名*/

?>

 

 

php安卓服务器软件下载:https://yunpan.cn/cPr8X7jvtkFNN  访问密码 4d7b

安卓版mysql数据库操作案例源码下载:https://yunpan.cn/cPrG5hJuzmYgU  访问密码 a195

 

安卓版php服务器的mysql数据库增删改查简单案例

标签:

原文地址:http://www.cnblogs.com/qingsong/p/5440982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!