我有一个包含用户及其电子邮件的数据库,但是无法从代码中添加新用户。我在C:\ xampp \ htdocs中有一个表单,但是当尝试通过该表单添加用户时,没有任何反应。我只能在pma中进行更改。
<?php
$pdo = new PDO('mysql:host=localhost;port=3306;dbname=misc',
'fred', 'zap');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
<?php
require_once "pdo.php";
if ( isset($_POST['name']) && isset($POST['email'])
&& isset($_POST['password'])) {
$sql = "INSERT INTO users (name, email, password)
VALUES (:name, :email, :password)";
echo("<pre>\n".$sql."\n</pre>\n");
$stmt = $pdo->prepare($sql);
$stmt->execute(array(
':name' => $_POST['name'],
':email' => $_POST['email'],
':password' => $_POST['password']));
}
?>
<html>
<head></head><body>
<p>Add a New User</p>
<form method="post">
<p>Name:
<input type="text" name="name" size="40"></p>
<p>Email:
<input type="text" name="email"></p>
<p>Password:
<input type="password" name="email"</p>
<p><input type="submit" value="Add New"/></p>
</form>
</body>
答案 0 :(得分:-1)
签出<input type="password" name="email"
。这里是name='password'
而不是name="email"
。
isset($_POST['password']))
返回null,如果条件不满足,则查询不执行>
答案 1 :(得分:-1)
尝试低于该代码。
<?php
//require_once "pdo.php";
//my own db connection
$pdo = new PDO (
'mysql:host=localhost;dbname=misc;charset=utf8',
'fred', // username
'zap' // password
);
if ( isset($_POST['name']) && isset($POST['email'])
&& isset($_POST['password'])) {
$sql = $pdo->prepare("INSERT INTO users(name, email, password)
values(:name, :email, :password) ");
$sql->execute(array(
':name' => $_POST['name'],
':email' => $_POST['email'],
':password' => $_POST['password']
));
if(sql){
echo "success";
}else{
echo "failed";
}
}
?>
<html>
<head></head><body>
<p>Add a New User</p>
<form method="post">
<p>Name:
<input type="text" name="name" id="name" size="40"></p>
<p>Email:
<input type="text" name="email" id="email"></p>
<p>Password:
<input type="password" name="password" id="password"></p>
<p><input type="submit" value="Add New"></p>
</form>
</body>