从提交的html页面存储mysql中的数据

时间:2013-06-18 20:16:09

标签: php html mysql store

因此,我在引用数据库以存储我的网站的新用户信息时遇到问题。这是我们正在使用的html的示例。我也提供了php表单。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd"
    >
<html lang="en">
<head>
    <title>Log In</title>
</head>
<body>
<form action= "form_process.php" method="post">

//Inputting user information: first name, last name, password, gender.
First Name: <input type="text" name="firstname"> <br/>
Last Name: <input type="text" name="lastname"> <br/>
Password <input type="password" name="password"> <br/>
Are you male or female?<br/>
Male<input type="radio" name="Male" value="Male"> <br/>
Female<input type="radio" name="Female" value="Female"> <br/>
<input type="submit" value="submit"> 
</form> 
</body>
</html>

Here is the php form that I am using to confirm the data passed.

<?php
$first = $_POST['first'];
$last = $_POST['last'];
$email = $_POST['email'];

echo("First: ". $first);
echo("<br/>Last: " . $last);
echo("<br/>Email: " . $email);
?>

让我知道如何将信息存储到mysql。我使用Go Daddy作为mysql服务器。

谢谢!

1 个答案:

答案 0 :(得分:1)

假设您的表格非常简单,其字段与您通过HTTP POST收集的内容相匹配,您将执行以下操作:

// open database connection
$db = new PDO('mysql:host=hostname;dbname=dbname', 'username', 'password');

// prepare SQL statement
$sql = $db->prepare("INSERT INTO table (first, last, email) VALUES (?, ?, ?)");

// execute prepared statement and insert into db
$sql->execute(array($first, $last, $email));

对于一个非常好的PDO初学者教程,我会查看:http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers