简体   繁体   English

MySqli向数据库添加多行

[英]MySqli adding multiple rows to db

I'm sending data to a db from a web form but every time I run it the data is stored in the db in six new rows rather than just one. 我是从Web表单向db发送数据,但是每次运行它时,数据都会在数据库中存储六行,而不仅仅是一行。

The form is just a standard form with inputs for email/password and a submit button. 该表格只是一个标准表格,其中包含电子邮件/密码输入和一个提交按钮。 The action of the form runs this: 表单的操作运行以下命令:

<?php
// connect to db
$link = new mysqli("localhost", "root", "", "db_name");
if (!$link) {die('Database Error: Could not connect: ' . mysql_error());}

// username and password sent from form
$email = $_POST['email'];
$password = ($_POST['password']);

// encrypt
$salt = substr(base64_encode(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM)), 0, 16);
$em = crypt($email, '$6$'.$salt);
$pw = crypt($password, '$6$'.$salt);

// insert to db
$insert = "INSERT INTO users (email, password) VALUES ('$em', '$pw')";
$link -> query($insert);

// check succes/fail
if ($link->query($insert) === TRUE) {
    echo "New record created successfully";}
else {
    echo "Error: " . $sql . "<br>" . $link->error;
}

// close the db connection
$link->close();

I know this brings up a question about sanitizing inputs with encryption/salting. 我知道这带来了一个有关使用加密/盐洗输入内容的问题。 This page says that it's a reasonable method. 页面说这是一种合理的方法。 Please feel free to bring that up, but I'm not really looking for an argument over best practices for sanitizing user inputs. 请随意提出来,但我并不是真的在寻求关于最佳化用户输入的最佳实践的论据。

I'm wondering if anyone cal tell me why the data would be stored 6 times instead of just once. 我想知道是否有人告诉我为什么将数据存储6次而不是一次。 I changed the $6$ to $1$ but it still added 6 rows. 我将$6$更改为$1$但仍然添加了6行。

您的网站上有一些愚蠢的seo友好链接实现,使其在每次请求时都运行php代码,并在html中提供了5个指向不存在资源的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM