Home > php教程 > PHP源码 > PHP连接数据库,点击按钮查询数据,没点击的时候,如果有数据,那么按钮变红,没有数据则不变红。

PHP连接数据库,点击按钮查询数据,没点击的时候,如果有数据,那么按钮变红,没有数据则不变红。

PHP中文网
Release: 2016-05-25 16:58:09
Original
1588 people have browsed it

代码

<html>
<body>
<?php
echo "<input type=&#39;button&#39; id=&#39;anniu&#39; style=&#39;width:250px;height:25px;font-weight:bold;font-size:14px&#39; onclick=\"window.location=&#39;demo1.php&#39;\" value=&#39;查询逾期无吨位车辆信息&#39; />";
echo "<input type=&#39;button&#39; style=&#39;width:250px;height:25px;font-weight:bold;font-size:14px&#39;onclick=\"window.location=&#39;huizong.php&#39;\" value=&#39;查询汇总信息&#39; />";
    $mysql_server_name="localhost"; //数据库服务器名称
    $mysql_username="root"; // 连接数据库用户名
    $mysql_password="123456"; // 连接数据库密码
    $mysql_database="td_oa"; // 数据库的名字
    
    // 连接到数据库
    $conn=mysql_connect($mysql_server_name, $mysql_username,
                        $mysql_password);
//解决乱码 
mysql_query("SET NAMES &#39;UTF8&#39;"); 
mysql_query("SET CHARACTER SET UTF8"); 
        mysql_query("SET CHARACTER_SET_RESULTS=UTF8&#39;"); 
                        
     // 从表中提取信息的sql语句
$strsql="select a.field4 as &#39;车号&#39;,a.field8 as&#39;任务号&#39;,a.field5 as&#39;装货日期&#39; ,a.field6 as&#39;供应商名称&#39;,a.field7 as&#39;采购价格&#39; from `crm_storage` a left join `crm_order` b on a.field4=b.field2 and a.field8=b.field16
where (b.field8=0 or b.field8 is null)
and DATE_ADD(a.field5,INTERVAL 4 day)<CURDATE()";
    // 执行sql查询
    $result=mysql_db_query($mysql_database, $strsql, $conn);
    // 获取查询结果
    $row=mysql_fetch_row($result);
//没有查询结果则让$have=0;有查询结果$have=1;然后后面的JS根据$have的值来处理按钮变色
if(empty($row)){
$have=0;
}else{
$have=1;
}
?>
<script type="text/javascript">
window.onload=function()
{
have=<?php echo $have;?>;
if(have>0){
document.getElementById("anniu").style.backgroundColor="red";
}
}
</script>
</body>
</html>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template