html表单 用PHP 连接 Mysql,并且设置Cookie。
这个是我做的表单,现要求用PHP连接html的Form,并且设置Cookie。以下是我的代码,麻烦哪个高手帮个忙。<!DOCTYPEhtmlPUBLIC"-//W3C//...
这个是我做的表单,现要求用PHP连接html 的Form, 并且设置Cookie。以下是我的代码,麻烦哪个高手帮个忙。
<!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 content="text/html; charset=utf-8" http-equiv="Content-Type" />
<link href="formstyle.css" rel="stylesheet" type="text/css"/>
<title>PHP Form Assignment</title>
</head>
<body>
<form name="phpAssn" method="post" action="connectionToDatabase.php" id="phpAssn">
<h1>PHP Form Assignment</h1>
<table>
<tr>
<td>
<label for="fullname">Full Name:</label>
<input type="text" name="fullname" id="fullname" class="text"/>
</td>
</tr>
<tr>
<td>
<label for="email">Email:</label>
<input type="text" name="email" id="email" class="text"/>
</td>
</tr>
<tr>
<td>
<label for="subject">Subject:</label>
<select name="Select1">
<option>Account Balance Inquiry</option>
<option>Submit Payment</option>
<option>Close Account</option>
</select>
</td>
</tr>
<tr>
<td>
<label for="comments">Comments:</label>
<textarea name="comments" id="comments" cols="45" rows="5" class="text"></textarea>
</td>
</tr>
<tr>
<td>
<input type="submit" name="send" id="send" value="Submit Query"/>
<input type="reset" name="reset" id="clear" value="Reset"/>
</td>
</tr>
</table>
</form>
</body>
</html> 展开
<!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 content="text/html; charset=utf-8" http-equiv="Content-Type" />
<link href="formstyle.css" rel="stylesheet" type="text/css"/>
<title>PHP Form Assignment</title>
</head>
<body>
<form name="phpAssn" method="post" action="connectionToDatabase.php" id="phpAssn">
<h1>PHP Form Assignment</h1>
<table>
<tr>
<td>
<label for="fullname">Full Name:</label>
<input type="text" name="fullname" id="fullname" class="text"/>
</td>
</tr>
<tr>
<td>
<label for="email">Email:</label>
<input type="text" name="email" id="email" class="text"/>
</td>
</tr>
<tr>
<td>
<label for="subject">Subject:</label>
<select name="Select1">
<option>Account Balance Inquiry</option>
<option>Submit Payment</option>
<option>Close Account</option>
</select>
</td>
</tr>
<tr>
<td>
<label for="comments">Comments:</label>
<textarea name="comments" id="comments" cols="45" rows="5" class="text"></textarea>
</td>
</tr>
<tr>
<td>
<input type="submit" name="send" id="send" value="Submit Query"/>
<input type="reset" name="reset" id="clear" value="Reset"/>
</td>
</tr>
</table>
</form>
</body>
</html> 展开
3个回答
2010-11-26
展开全部
据我对你的问题所了解,只能提供以下代码!
connectionToDatabase.php:
<?php
session_start();
//整理用户提交的数据
$fullname=trim($_POST['fullname']);
$email=trim($_POST['email']);
$select1=trim($_POST['Select1']);
$comments=trim($_POST['comments']);
if ($fullname=='' || $email=='' || $select1=='' || $comments=='')
{
echo "未完成填写!";
exit;
}
if(!eregi('^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$',$email)
{
echo "Email不是有效地址!";
exit;
}
//设置Cookie
$_SESSION['user']=$fullname;
//连接Mysql的数据库PHPFormAssignment
$db=new mysqli('localhost','user','password','PHPFormAssignment');
if (mysqli_connect_errno())
{
echo "数据库异常,请重试!";
exit;}
//写入表单phpAssn
$query="insert into phpAssn values(),
('$fullname','$email','$select1','$comments')";
$result=$db->query($query);
if ($result==false)
{
echo "数据库异常,请重试!";
exit;
}
else echo "成功写入数据库";
$result->free();
$db->close();
//From J.L
?>
connectionToDatabase.php:
<?php
session_start();
//整理用户提交的数据
$fullname=trim($_POST['fullname']);
$email=trim($_POST['email']);
$select1=trim($_POST['Select1']);
$comments=trim($_POST['comments']);
if ($fullname=='' || $email=='' || $select1=='' || $comments=='')
{
echo "未完成填写!";
exit;
}
if(!eregi('^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$',$email)
{
echo "Email不是有效地址!";
exit;
}
//设置Cookie
$_SESSION['user']=$fullname;
//连接Mysql的数据库PHPFormAssignment
$db=new mysqli('localhost','user','password','PHPFormAssignment');
if (mysqli_connect_errno())
{
echo "数据库异常,请重试!";
exit;}
//写入表单phpAssn
$query="insert into phpAssn values(),
('$fullname','$email','$select1','$comments')";
$result=$db->query($query);
if ($result==false)
{
echo "数据库异常,请重试!";
exit;
}
else echo "成功写入数据库";
$result->free();
$db->close();
//From J.L
?>
展开全部
<?
$db_conn = mysql_connect('服务器', '数据库用户名', '数据库密码');
if (!$db_conn ){
echo "连接失败";
}
mysql_select_db("数据库名", $db_conn);
$fullname = $_POST[fullname];
$email = $_POST[email];
//....其它类似这样
setcookie("fullname",$fullname);
?>
$db_conn = mysql_connect('服务器', '数据库用户名', '数据库密码');
if (!$db_conn ){
echo "连接失败";
}
mysql_select_db("数据库名", $db_conn);
$fullname = $_POST[fullname];
$email = $_POST[email];
//....其它类似这样
setcookie("fullname",$fullname);
?>
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼下的回答的就很好
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询