文章目录
零、简介
Web数据库访问是互联网应用中的一个重要组成部分,它允许用户通过Web浏览器与远程数据库进行交互。这种交互通常涉及到数据的查询、插入、更新和删除操作。为了实现这些功能,需要使用一系列的技术和方法。本文将介绍Web数据库访问的基本概念、相关技术以及安全性问题。
- 基本概念
Web数据库访问是指通过Web应用程序与数据库进行通信的过程。这个过程通常包括以下步骤:
- 用户通过Web浏览器发送请求(如查询、插入、更新或删除数据)。
- Web服务器接收到请求后,将其转发给应用程序服务器。
- 应用程序服务器处理请求,生成相应的SQL语句。
- 应用程序服务器将SQL语句发送给数据库服务器。
- 数据库服务器执行SQL语句,并将结果返回给应用程序服务器。
- 应用程序服务器将结果转换为HTML或其他格式,并将其发送回Web服务器。
- Web服务器将结果返回给用户的浏览器。
- 相关技术
为了实现Web数据库访问,需要使用一些相关的技术和方法。以下是一些常见的技术和方法:
-
CGI(Common Gateway Interface):CGI是一种早期的Web数据库访问技术,它允许Web服务器与外部程序进行通信。当用户通过Web浏览器发送请求时,Web服务器会调用相应的CGI程序来处理请求。CGI程序可以与数据库进行交互,并将结果返回给Web服务器。
-
ASP(Active Server Pages):ASP是Microsoft推出的一种Web应用程序开发技术。它允许开发人员使用VBScript或JScript等脚本语言编写Web应用程序。ASP应用程序可以与数据库进行交互,并将结果返回给Web服务器。
-
PHP(Hypertext Preprocessor):PHP是一种广泛使用的开源脚本语言,特别适用于Web开发。PHP可以与各种数据库系统(如MySQL、Oracle、SQL Server等)进行交互。开发人员可以使用PHP编写Web应用程序,实现对数据库的访问和操作。
-
JSP(JavaServer Pages):JSP是Sun Microsystems推出的一种Web应用程序开发技术。它允许开发人员使用Java语言编写Web应用程序。JSP应用程序可以与数据库进行交互,并将结果返回给Web服务器。
-
ADO.NET:ADO.NET是Microsoft推出的一种新型的数据访问技术。它提供了一组对象模型,用于在.NET应用程序中访问和操作数据。ADO.NET支持多种数据源,如SQL Server、Oracle、MySQL等。开发人员可以使用ADO.NET编写Web应用程序,实现对数据库的访问和操作。
- 安全性问题
Web数据库访问涉及到敏感数据的传输和存储,因此需要关注安全性问题。以下是一些常见的安全性问题及解决方案:
-
SQL注入攻击:攻击者通过构造恶意的输入,试图在应用程序中注入SQL语句,从而实现对数据库的非法访问。为了防止SQL注入攻击,开发人员应该使用参数化查询或预编译语句,避免直接拼接SQL语句。
-
跨站脚本攻击(XSS):攻击者通过在Web页面中插入恶意脚本,试图窃取用户的敏感信息。为了防止XSS攻击,开发人员应该对用户的输入进行严格的验证和过滤,避免在页面中直接显示未经处理的用户输入。
-
数据泄露:由于数据库服务器的安全漏洞或配置不当,可能导致敏感数据的泄露。为了防止数据泄露,应该对数据库服务器进行定期的安全检查和更新,确保其安全性。
总之,Web数据库访问是互联网应用中的一个重要组成部分,它涉及到数据的查询、插入、更新和删除操作。为了实现这些功能,需要使用一系列的技术和方法。同时,还需要关注安全性问题,确保数据的安全和完整。
一、 实验目的和要求
中文五号宋体,英文五号Times new roman字体,1.25倍行距
用中文描述本次实验的目的和要求,可参考实验指导书相关描述。
二、实验环境(实验设备)
中文五号宋体,英文五号Times new roman字体,1.25倍行距
硬件:微型计算机
软件:Windows 操作系统、浏览器
三、实验原理及内容
(1)Html首页(index.html):
<html>
<head>
<title>Welcome to book seller</title>
</head>
<body>
<form action = "process.php" method = "post">
<h2>Welcome!</h2>
<table>
<tr>
<td>please input your name</td>
<td><input type = "text" name = "name" size = "30" /></td>
</tr>
<tr>
<td>please input your address</td>
<td><input type = "text" name = "address" size = "30" /></td>
</tr>
<tr>
<td>please input your zip</td>
<td><input type = "text" name = "zip" size = "30" /></td>
</tr>
</table>
<p>please fiil in the quantify field of the following form</p>
<table border = "border">
<tr>
<th>book</th>
<th>publisher</th>
<th>price</th>
<th>quantitiy</th>
</tr>
<tr>
<td>Web technology</td>
<td>Springer press</td>
<td>$5.0</td>
<td><input type = "text" name = "web_t" size = "7" /></td>
</tr>
<tr>
<td>mathematics</td>
<td>ACM press</td>
<td>$6.2</td>
<td><input type = "text" name = "math" size = "7" /></td>
</tr>
<tr>
<td>principle of OS</td>
<td>Science press</td>
<td>$10</td>
<td><input type = "text" name = "os" size = "7" /></td>
</tr>
<tr>
<td>Theory of matrix</td>
<td>High education press</td>
<td>$7.8</td>
<td><input type = "text" name = "matrix" size = "7" /></td>
</tr>
</table>
<h3>Payment method</h3>
<p>
<input type = "radio" name = "payment" value = "cash" chexked = "unchecked" />
cash <br/>
<input type = "radio" name = "payment" value = "cheque" chexked = "unchecked" />
cheque <br/>
<input type = "radio" name = "payment" value = "credit card" chexked = "unchecked" />
credit card <br/>
<input type = "submit" value = "submit" />
<input type = "reset" value = "reset" />
</p>
</form>
</body>
</html>
(2)数据库连接:
<!-- -->
<html>
<head>
<title>This is the output of process.php</title>
</head>
<body>
<?php
$name = $_POST["name"];
$address = $_POST["address"];
$zip = $_POST["zip"];
$web_t = $_POST["web_t"];
$math = $_POST["math"];
$os = $_POST["os"];
$matrix = $_POST["matrix"];
$payment = $_POST["payment"];
if($web_t == "") $web_t = 0;
if($math == "") $math = 0;
if($os == "") $math = 0;
if($matrix == "") $matrix = 0;
$web_t_cost = 5.0*$web_t;
$math_cost = 6.2*$math;
$os_cost = 10*$os;
$matrix_cost = 7.8*$matrix;
$total_num = $web_t+$math+$os+$matrix;
$total_price = $web_t_cost+$math_cost+$os_cost+$matrix_cost;
$con = mysql_connect("localhost" , "root", "");
if(!$con)
{
die('Could not connect:'.mysql_error());
}
mysql_select_db("myEx", $con);
$sql = "insert into customers values('$name', '$address', '$zip')";
if(!mysql_query($sql, $con))
{
die('ERROR:'.mysql_error());
}
if($web_t != 0)
{
$sql = "insert into books values('Web technology', 'Springer press', 5)";
if(!mysql_query($sql, $con))
{
die('ERROR:'.mysql_error());
}
$sql = "insert into orders values('$name', 'Web technology', $web_t)";
if(!mysql_query($sql, $con))
{
die('ERROR:'.mysql_error());
}
}
if($math != 0)
{
$sql = "insert into books values ('mathematics', 'ACM press', 6.2)";
if(!mysql_query($sql, $con))
{
die('ERROR:'.mysql_error());
}
$sql = "insert into orders values ('$name', 'mathematics', $math)";
if(!mysql_query($sql, $con))
{
die('ERROR:'.mysql_error());
}
}
if($os != 0)
{
$sql = "insert into books values ('principle of OS', 'Science press', 10)";
if(!mysql_query($sql, $con))
{
die('ERROR:'.mysql_error());
}
$sql = "insert into orders values ('$name', 'principle of OS', '$os')";
if(!mysql_query($sql, $con))
{
die('ERROR:'.mysql_error());
}
}
if($matrix != 0)
{
$sql = "insert into books values ('Theory of matrix', 'High education press', 7.8)";
if(!mysql_query($sql, $con))
{
die('ERROR:'.mysql_error());
}
$sql = "insert into orders values ('$name', 'Theory of matrix' , '$matrix')";
if(!mysql_query($sql, $con))
{
die('ERROR:'.mysql_error());
}
}
echo'END';
?>
</body>
</html>
(3)Sql语句:
<html>
<head>
<title>porcess_query.php form</title>
</head>
<body>
<?php
$user_name = $_POST["user_name"];
$con = mysql_connect("localhost", "root", "root");
if(!$con)
{
die('Could not connect:'.my_sqlerror());
}
mysql_select_db('myEx', $con);
$result = mysql_query("select name, orders.book, publisher, quantity from orders, books where orders.book = books.book and name = '$user_name'"); //
echo"<table border = '1'>
<tr>
<th>name</th>
<th>book</th>
<th>publisher</th>
<th>quantity</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo"<tr>";
echo"<td>".$row['name']."</td>";
echo"<td>".$row['book']."</td>";
echo"<td>".$row['publisher']."</td>";
echo"<td>".$row['quantity']."</td>";
echo"</tr>";
}
echo"</table>";
mysql_close($con);
?>
</body>
</html>
四、实验小结(包括问题和解决方法、心得体会、意见与建议等)
(一)实验中遇到的主要问题及解决方法
连接数据库时,找不到库函数,配置php ini文件后解决。
(二)实验心得
学习了前端通过php和数据库的连接