+ -
当前位置:首页 → 问答吧 → 简易电话本

简易电话本

时间:2008-02-27

来源:互联网

实在想不到能写点什么,就做了个简易在线电话本.
比较简洁150行代码.
演示地址: http://203.81.53.84/~wang/test/phone.php
数据库:
id:  联系人编号
name:联系人名称
phone: 电话号码
qq: QQ号码
six: 性别 0-男, 1-女
复制PHP内容到剪贴板
PHP代码:

<?php
#数据库配置
$config['host'] = 'localhost';
$config['username'] = 'webtable';
$config['password'] = '******';
#连接数据库
mysql_connect($config['host'],$config['username'],$config['password']);
#使用数据库
mysql_select_db('wang_web');

#post数据处理
$search_arr = array("/ union /i","/ select /i","/ update /i","/ outfile /i","/ or /i");
$replace_arr = array(' union ',' select ',' update ',' outfile ',' or ');

$name = strip_sql($_POST['username']);
$phone = intval($_POST['phone'])==0?'':intval($_POST['phone']);
$qq = intval($_POST['qq'])==0?'':intval($_POST['qq']);
$six = intval($_POST['six']);

if($_GET['type'] == 'seach'){//搜索

    $querys = mysql_query("
            SELECT *
                FROM ph_phonebook
                WHERE
                     name  LIKE '%{$name}%' and
                     phone  LIKE '%{$phone}%' and
                     qq  LIKE '%{$qq}%' and
                     six  LIKE '%{$six}%'");

}elseif($_GET['type'] == 'adduser'){//添加
    if(empty($name) || empty($phone)){
        exit('联系人名称和电话不能为空<a href="phone.php">点击这里返回重写</a>');
    }
    mysql_query("
            INSERT INTO  ph_phonebook 
            SET name='{$name}', phone='{$phone}', qq='{$qq}', six='{$six}'");
    mysql_close();
    header("Location: phone.php");

}elseif($_GET['type'] == 'deluser'){//删除
    $id = intval($_GET['id']);
    mysql_query("DELETE FROM ph_phonebook WHERE id={$id}");
    mysql_close();
    header("Location: phone.php");
}else{
    $querys = mysql_query('SELECT * FROM ph_phonebook');
}

function strip_sql($string){#过滤字符串(防止SQL语句注入)
    global $search_arr,$replace_arr;
    return is_array($string) ? array_map('strip_sql', $string) : preg_replace($search_arr, $replace_arr, $string);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" c />
<title>电话本</title>
</head>
<body>
<script language="javascript" type="text/javascript">
    function seach(){
        document.getElementById('win').action = '';
        document.getElementById('win').action =  './phone.php?type=seach';
        document.getElementById('win').submit();
    }
    function adduser(){
        document.getElementById('win').action = '';
        document.getElementById('win').action =  './phone.php?type=adduser';
        document.getElementById('win').submit();    
    }
    function deluser(id){
        window.location.href='./phone.php?type=deluser&id='+id;
    }
</script>
<table width="100%" height="100%" border="1" cellpadding="0" cellspacing="0">
    <tr height="100px">
        <td>
            <form action="phone.php" method="post" id="win">
            <table>
                <tr>
                    <td>
                        名称:
                        <input type="text" name="username" maxlength="50" />
                    </td>
                    <td>
                        手机:
                        <input type="text" name="phone" maxlength="11" />
                    </td>
                    <td>
                        QQ:
                        <input type="text" name="qq" maxlength="9" />
                    </td>
                    <td>
                        性别:
                        <label for="six_man">
                        <input type="radio" name="six" id="six_man"  value="0" checked="checked">男
                        </label>
                        <label for="six_woman">
                        <input type="radio" name="six" id="six_woman" value="1" />女
                        </label>
                    </td>
                </tr>
                <tr>
                    <td colspan="4" align="center">
                        <input type="button" value="添加" >
                        <input type="button" value="搜索" >
                        <input type="button" value="显示全部" >
                        <input type="reset" value="重新填写">
                    </td>
                </tr>            
            </table>
            </form>
        </td>
    </tr>
    <tr height="520px">
        <td valign="top">
            <table border="1"  cellpadding="0" cellspacing="0" width="100%">
                <tr>
                <td>名字</td>
                <td>电话</td>
                <td>QQ</td>
                <td>性别</td>
                <td width="20px;"> </td>
                </tr>
<?php
                while($row = mysql_fetch_assoc($querys)){
                echo "<tr>";
                    echo "<td>{$row['name']} </td>";
                    echo "<td>{$row['phone']} </td>";
                    echo "<td>{$row['qq']} </td>";
                    $six = $row['six'] == 0?'男':'女';
                    echo "<td>{$six} </td>";
                    echo "<td><input type='button' value='删除这个人' onclick='deluser({$row['id']})'></td>";
                echo "</tr>";
                }
?>
            </table>            
        </td>
    </tr>
</table>
</body>
</html>
[ 本帖最后由 wangyl 于 2008-2-27 16:05 编辑 ]

作者: wangyl   发布时间: 2008-02-27

鼓励一下!!!

作者: luzhou   发布时间: 2008-02-27

电话的数据长度不够哦

作者: 111   发布时间: 2008-02-28

手机:
<input type="text" name="phone" maxlength="11" />  <<----这里改就是了,设定11位.

作者: wangyl   发布时间: 2008-02-28

读者应该学会举一反三。

作者: luzhou   发布时间: 2008-02-28

热门下载

更多