在此我创建了一个简单的HTML表单,其中从HTML表单输入的电子邮件和密码不保存在MYSQL数据库中!我做了哪些更改,以便可以在MYSQL数据库中保存数据。
这是我的HTML代码:
<form style="position: relative;" action="pass.php" method="post" >
<div style="position: absolute; left: 107px; top: 130px; text-align: center; width: 450px;">
<input type="email" name="email" style="border:none" size="45" placeholder="Email" /><br>
<br>
<input type="password" name="password" style="border:none" size="45" placeholder="Password" /><br /><br>
<button type="submit" class="inbutton"></button>
</div>
</form>
这是php代码:
<?php
if( $_POST )
{
$con = mysql_connect("mysqlhostname","username","password","databasename");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("databasename", $con);
$users_email = $_POST['email'];
$users_password = $_POST['password'];
$users_email = mysql_real_escape_string($users_email);
$users_password = mysql_real_escape_string($users_password);
$query = "INSERT INTO pass (`email`, `password`) VALUES ('$users_email', '$users_password');";
mysql_query($query);
mysql_close($con);
}
?>
答案 0 :(得分:3)
在Html文件中:给出提交按钮的名称属性
<html>
<body>
<form style="position: relative;" action="pass.php" method="post" >
<div style="position: absolute; left: 107px; top: 130px; text-align: center; width: 450px;">
<input type="email" name="email" style="border:none" size="45" placeholder="Email" /><br>
<br>
<input type="password" name="password" style="border:none" size="45" placeholder="Password" /><br /><br>
<button type="submit" class="inbutton" name="Save">Save</button>
</div>
</form>
</body>
</html>
这是php代码:
在您的PHP文件中进行小的更改:pass.php
<?php
if( isset( $_POST['Save'] ) )
{
$con = mysql_connect("mysqlhostname","username","password","databasename");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db( "databasename", $con );
$users_email = $_POST['email'];
$users_password = $_POST['password'];
$users_email = mysql_real_escape_string($users_email);
$users_password = mysql_real_escape_string($users_password);
$query = "INSERT INTO pass ( email, password ) VALUES ( '$users_email', '$users_password' )";
mysql_query( $query, $con );
mysql_close($con);
}
?>
答案 1 :(得分:0)
在离线讨论中,我们发现OP的代码不会进入外部if
语句。以下是我将如何构建它的示例代码。
需要注意的事项:
$_POST
要跑。祝你好运!
<form action="pass.php" method="post" >
<input type="email" name="email" size="45" placeholder="Email" />
<br>
<input type="password" name="password" size="45" placeholder="Password" />
<br>
<input type="submit" name="submit" value="Submit">
</form>
/*****************************************/
/**************** pass.php ***************/
/*****************************************/
<?php
// Please use PDO instead of mysql because mysql is deprecated and officially removed as of PHP 7.
// Read about it here: https://phpdelusions.net/pdo
if(isset($_POST['submit'])) {
// Wrap everything in a try/catch block so you can actually see the error.
try {
/*****************************************/
/************* DB CONNECTION *************/
/*****************************************/
// Change this to match your DB credentials
$host = '127.0.0.1';
$db = 'test';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
// This is your DB connection. Use it below.
$pdo = new PDO($dsn, $user, $pass, $opt);
/*****************************************/
/************ FORM PROCESSING ************/
/*****************************************/
// Get values from form.
// Do any validation you want here.
$email = $_POST['email'];
$password = $_POST['password'];
/*****************************************/
/*************** DB INSERT ***************/
/*****************************************/
$stmt = $pdo->prepare('INSERT INTO pass (email, password) VALUES (?, ?)');
$stmt->execute(array(
$email,
$password
));
/*****************************************/
/*********** OUTPUT ANY ERRORS ***********/
/*****************************************/
} catch (Exception $e) {
var_dump($e->getMessage());
}
}
答案 2 :(得分:0)
php文件if($_POST)
它应该是if( isset( $_POST ['Save'] ) )
和
<button type="submit" class="inbutton">
它应该是<button type="submit" class="inbutton" name="Save">
尝试下面的PHP代码
<?php
if( isset( $_POST['Save'] ) )
{
$servername = "mysqlhostname"; //localhost or your server name
$username = "username"; //root or your username
$password = "password"; //password or server hasn't password it should be `$password = "";`
$dbname = "databasename"; //your database name
// Create connection
$con = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($con->connect_error) {
die("Could not connect: " . $con->connect_error);
}
$users_email = $_POST['email'];
$users_password = $_POST['password'];
$sql = "INSERT INTO pass (email, password) VALUES ('$users_email', '$users_password')";
if ($con->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$con->close();
}
?>
我得到了结果