我正经历着噩梦般的一天,并试图修改 html 和 php 以将注册表单的信息插入 mysql。下面是html表单和php提交页面。我已经开始使用与数据库的 sql 连接,并为添加函数设置了一些变量。
今天是星期一,我想我有点迷失了——任何帮助都会很棒:-)
<?php
$dbcnx = @mysql_connect('localhost', 'user', 'pass');
if (!$dbcnx) {
exit('<p>Unable to connect to the ' . 'database server at this time.</p>');
}
if (!@mysql_select_db('signp',$dbcnx)) {
exit('<p>Unable to locate the file ' . 'database at this time.</p>');
}
if ($action == "add") {
/* ****************************** Do this if we are adding a new record */
$query = "INSERT INTO connect VALUES ()";
$result = mysql_query( $query );
if(!$result){
die ("could not query database: <br />".mysql_error());
}
$d_id = $inserted_id = mysql_insert_id();
$dname = "";
$demail = "";
$dpassword = "";
}
// Error reporting:
error_reporting(E_ALL^E_NOTICE);
// This is the URL your users are redirected,
// when registered succesfully:
$redirectURL = 'http://ip/index.html';
$errors = array();
// Checking the input data and adding potential errors to the $errors array:
if(!$_POST['name'] || strlen($_POST['name'])<3 || strlen($_POST['name'])>50)
{
$errors['name']='Please fill in a valid name!<br />Must be between 3 and 50 characters.';
}
if(!$_POST['email'] || !preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $_POST['email']))
{
$errors['email']='Please fill in a valid email!';
}
if(!$_POST['pass'] || strlen($_POST['pass'])<5)
{
$errors['pass']='Please fill in a valid password!<br />Must be at least 5 characters long.';
}
// Checking whether the request was sent via AJAX
// (we manually send the fromAjax var with the AJAX request):
if($_POST['fromAjax'])
{
if(count($errors))
{
$errString = array();
foreach($errors as $k=>$v)
{
// The name of the field that caused the error, and the
// error text are grouped as key/value pair for the JSON response:
$errString[]='"'.$k.'":"'.$v.'"';
}
// JSON error response:
die ('{"status":0,'.join(',',$errString).'}');
}
// JSON success response. Returns the redirect URL:
echo '{"status":1,"redirectURL":"'.$redirectURL.'"}';
exit;
}
// If the request was not sent via AJAX (probably JavaScript
// has been disabled in the visitors' browser):
if(count($errors))
{
echo '<h2>'.join('<br /><br />',$errors).'</h2>';
exit;
}
// Directly redirecting the visitor:
header("Location: ".$redirectURL);
?>
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>sign up</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<FORM ACTION="submit.php" METHOD=get>
<div id="carbonForm">
<h1>Signup</h1>
<form action="submit.php" method="post" id="signupForm">
<div class="fieldContainer">
<div class="formRow">
<div class="label">
<label for="name">Name:</label>
</div>
<div class="field">
<input type="text" name="name" id="name" />
</div>
</div>
<div class="formRow">
<div class="label">
<label for="email">Email:</label>
</div>
<div class="field">
<input type="text" name="email" id="email" />
</div>
</div>
<div class="formRow">
<div class="label">
<label for="pass">Password:</label>
</div>
<div class="field">
<input type="password" name="pass" id="pass" />
</div>
</div>
</div> <!-- Closing fieldContainer -->
<div class="signupButton">
<input type="submit" name="submit" id="submit" value="Signup" />
</div>
</form>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</body>
</html>