当按下sumbit按钮时,如何让我的表单发布到mysql并链接回我的主页

时间:2015-01-03 12:20:55

标签: forms mysqli

这是我到目前为止的代码。我已经尝试将动作事件放在提交按钮和表单上。但出于某种原因,当我将其恢复到索引页面时,它不会在我的表格中放置任何内容。     

?>
<?php
include('../includes/conn.inc.php');
$stmt = $mysqli->prepare("INSERT INTO registration (firstName, secondName, age, email, username,  password, mailingList) VALUES (?,?,?,?,?,?,?)");
$stmt->bind_param('ssisssb', $_POST['firstname'], 
$_POST['surname'],
$_POST['age'],
$_POST['email'],
$_POST['username'],
$_POST['password'],
$_POST['list']);
$stmt->execute();  
$stmt->close();
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Register</title>
<link href="../styles/stylesheet.css" rel="stylesheet" type="text/css"/>
<script src="scripts/jquery-1.11.1.min.js"></script>
<script src="scripts/jquery.easing.1.3.js"></script>
</head>
<body>
<div id="container">
<header>
<h1>Register Now</h1>
</header>
<body>
<section>
<form id = "registrationForm" method="post" action = "../checkregistration.php">

<fieldset>
    <p>
        <label for="firstname">First name</label>
        <input type="text" name="firstname" id="firstname" /> 
        <span id="surnameReq">*Value Required</span>            
    </p>   
    <p>
        <label for="surname">Surname</label>
        <input type="text" name="surname" id="surname" />
        <span id="surnameReq">*Value Required</span>   
    </p>
    <p>
        <label for="Age">Age</label>
        <input type="text" name="age" id="age" />
        <span id="ageReq">*Value Required</span>   
    </p>      
    <p>
        <label for="Email">Email</label>
        <input type="email" name="email" id="Email" />
        <span id="emailReq">*Value Required</span>   
    </p> 
    <p>
        <label for="Username">Username</label>
        <input type="text" name="username" id="username" />
        <span id="usernameReq">*Value Required</span>   
    </p>  
    <p>
        <label for="Password">Password</label>
        <input type="text" name="password" id="password" />
        <span id="passwordReq">*Value Required</span>   
    </p>   
    <p>
        <label for="mailingList">Join mailing list</label>
        <input type="checkbox" name="list" id="mailingList" value="1" />
    </p>
    <input type="submit" value="Send" class="sendButton" action = "../checkregistration.php" />

</fieldset>
</form>
</section>
</body>
<footer>
</footer>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

我可能会这样做,而不是使用你的mysql方法:

$conn = mysqli_connect(host, user, password, database);

$query = "INSERT INTO registration (firstName, secondName, age, email, username,  password, mailingList) VALUES (".$_POST['firstname'].", 
    ".$_POST['surname'].",
    ".$_POST['age'].",
    ".$_POST['email'].",
    ".$_POST['username'].",
    ".$_POST['password'].",
    ".$_POST['list'].");"

mysqli_query($conn,$query)

mysqli_close($conn);

(放入../checkregistration.php文件)

您还可以添加一些错误处理,并且可能散列数据是一个好主意(我现在不会解释,网上有很多地方都在解释)。

之后,链接回主页:

header("Location: /index.php"); // Or whatever the main page is