使用DashDB的PHP登录表单
我正在尝试使用PHP和Bluemix的DashDB服务创建一个登录表单,我真的不知道这段代码有什么问题,我非常感谢您的帮助!我的代码由五部分组成:ConexionDB.php、checklogin.php、login_success.php、logout.php和main_login.php DashDB数据库:使用DashDB的PHP登录表单,php,login,db2,ibm-cloud,dashdb,Php,Login,Db2,Ibm Cloud,Dashdb,我正在尝试使用PHP和Bluemix的DashDB服务创建一个登录表单,我真的不知道这段代码有什么问题,我非常感谢您的帮助!我的代码由五部分组成:ConexionDB.php、checklogin.php、login_success.php、logout.php和main_login.php DashDB数据库: CREATE TABLE MEMBERS ( ID INT NOT NULL, USERNAME VARCHAR(65) NOT NULL DEFAUL
CREATE TABLE MEMBERS (
ID INT NOT NULL,
USERNAME VARCHAR(65) NOT NULL DEFAULT,
PASSWORD VARCHAR(65) NOT NULL DEFAULT,
PRIMARY KEY (ID)
);
ConexionDB.php
<?php
// Parse VCAP
if( getenv("VCAP_SERVICES") ) {
$json = getenv("VCAP_SERVICES");
}
// No DB credentials
else {
throw new Exception("No Database Information Available.", 1);
}
// Decode JSON and gather DB Info
$services_json = json_decode($json,true);
$bludb_config = $services_json["dashDB"][0]["credentials"];
// Create DB connect string
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=".
$bludb_config["db"].
";HOSTNAME=".
$bludb_config["host"].
";PORT=".
$bludb_config["port"].
";PROTOCOL=TCPIP;UID=".
$bludb_config["username"].
";PWD=".
$bludb_config["password"].
";";
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Application Name</title>
</head>
<body>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>
<?php
//Include DB conexion
require('includes/ConexionDB.php');
$tbl_name="MEMBERS"; // Table name
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// Connect to BLUDB
$conn = db2_connect($conn_string, '', '');
if ($conn) {
// Prepare, execute SQL and iterate through resultset
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$stmt = db2_prepare($conn, $sql);
$result = db2_execute($stmt);
//$result=db2_query($sql);
?>
<?php
// DB2_num_row is counting table row
$count=db2_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
<?php
db2_close($conn);
}
?>
main\u login.php
<?php
// Parse VCAP
if( getenv("VCAP_SERVICES") ) {
$json = getenv("VCAP_SERVICES");
}
// No DB credentials
else {
throw new Exception("No Database Information Available.", 1);
}
// Decode JSON and gather DB Info
$services_json = json_decode($json,true);
$bludb_config = $services_json["dashDB"][0]["credentials"];
// Create DB connect string
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=".
$bludb_config["db"].
";HOSTNAME=".
$bludb_config["host"].
";PORT=".
$bludb_config["port"].
";PROTOCOL=TCPIP;UID=".
$bludb_config["username"].
";PWD=".
$bludb_config["password"].
";";
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Application Name</title>
</head>
<body>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>
<?php
//Include DB conexion
require('includes/ConexionDB.php');
$tbl_name="MEMBERS"; // Table name
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// Connect to BLUDB
$conn = db2_connect($conn_string, '', '');
if ($conn) {
// Prepare, execute SQL and iterate through resultset
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$stmt = db2_prepare($conn, $sql);
$result = db2_execute($stmt);
//$result=db2_query($sql);
?>
<?php
// DB2_num_row is counting table row
$count=db2_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
<?php
db2_close($conn);
}
?>
应用程序名称
会员登录
用户名
:
密码
:
checklogin.php
<?php
// Parse VCAP
if( getenv("VCAP_SERVICES") ) {
$json = getenv("VCAP_SERVICES");
}
// No DB credentials
else {
throw new Exception("No Database Information Available.", 1);
}
// Decode JSON and gather DB Info
$services_json = json_decode($json,true);
$bludb_config = $services_json["dashDB"][0]["credentials"];
// Create DB connect string
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=".
$bludb_config["db"].
";HOSTNAME=".
$bludb_config["host"].
";PORT=".
$bludb_config["port"].
";PROTOCOL=TCPIP;UID=".
$bludb_config["username"].
";PWD=".
$bludb_config["password"].
";";
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Application Name</title>
</head>
<body>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>
<?php
//Include DB conexion
require('includes/ConexionDB.php');
$tbl_name="MEMBERS"; // Table name
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// Connect to BLUDB
$conn = db2_connect($conn_string, '', '');
if ($conn) {
// Prepare, execute SQL and iterate through resultset
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$stmt = db2_prepare($conn, $sql);
$result = db2_execute($stmt);
//$result=db2_query($sql);
?>
<?php
// DB2_num_row is counting table row
$count=db2_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
<?php
db2_close($conn);
}
?>
请描述您的错误,您可以使用 cf日志[您的应用程序名称](添加--recent以检索最后一个应用程序的转储)
您需要使用包含db2客户机的备用CloudFoundry构建包 如果您还没有完成,请下载Cloud Foundry命令行工具和PHP应用程序启动代码(您可以从Bluemix UI应用程序窗口右侧的“开始编码”链接下载) 运行“cf登录” 运行以下命令,使用带有db2的php构建包推送应用程序:
cf push <app-name> -b https://github.com/ibmdb/db2heroku-buildpack-php
cf push您的代码和构建中存在一些问题:)
1) 改变
$tbl_name=“成员”
与
$tbl_name=“SCHEMA.MEMBERS”
其中,在您的案例中,“SCHEMA”是“DASH103758”(如data_henrik所建议)
2) 您必须使用自定义构建包推送,因为您想要使用db2client(正如adasilva所建议的那样)。CF命令行包括:
cf login-u您的用户名-o您的组织-s您的空间
cf push YOUAPPNAME-b
-p您的应用程序\u本地\u路径
3) 函数db2_num_rows(..);不接受布尔值。您正在传递一个布尔值$result。db2_执行,实际上,返回一个布尔值()
因此,我建议更改“checklogin.php”中的查询字符串:
$sql=“从$tbl_name中选择count(*),其中username='$myusername'和
密码=“$mypassword”
支票是这样的:
//DB2_num_行是计数表行
//$count=db2_num_行($result)
//若结果匹配$myusername和$mypassword,则表行必须为1
划船
$count=-1
如果($result){
$rowcount=db2_fetch_数组($stmt);
$count=$rowcount[0];
}
如果($count==1){
我希望它能有用
关于如果您提供有关出错原因的更多详细信息,将会有所帮助。您的问题还不够,如果有错误,请添加错误,或者如果代码未按照您的理解工作,请添加错误的部分。表“MEMBERS”的架构名称是什么?如果可能的话,您应该在代码中使用SCHEMA.TABLENAME。感谢您的回答,问题是当我单击login时,我被重定向到checklogin.php页面,只显示一个空白页面,因此,我认为checklogin.pho代码没有正确检查数据库,它只是没有做任何事情。架构名称将是DASH103758.MEMBERS,我在代码中以同样的方式进行了测试,但我得到了相同的结果。感谢您的回答,问题是当我单击login时,我被重定向到checklogin.php页面并显示一个空白页面,因此,我认为checklogin.pho代码没有正确地检查数据库,它只是没有做任何事情。架构名称将是DASH103758.MEMBERS、 我在代码中以同样的方式进行了测试,但我得到了相同的结果。如果你得到一个空白页,很可能你的代码中有错误。请检查应用程序日志,如上所述