Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将表单数据插入数据库_Php_Mysql - Fatal编程技术网

Php 如何将表单数据插入数据库

Php 如何将表单数据插入数据库,php,mysql,Php,Mysql,我正在尝试将表单数据插入mysql数据库。连接页面工作正常 但我不知道代码中有什么问题 我一遍又一遍地检查代码,我找不到错误,但它不起作用 在尝试注册之后,如何将数据插入mysql表 我在Windows中工作 这是我的源代码: <?php $con = mysql_connect("localhost", "root", "", "tut"); 你的弦是错的。要正确粘贴变量,必须使用其他语法。 但是我不建议修改和使用这种代码。您将遇到sql注入问题。尝试改用PDO对

我正在尝试将表单数据插入mysql数据库。
连接
页面工作正常

但我不知道代码中有什么问题

我一遍又一遍地检查代码,我找不到错误,但它不起作用

在尝试注册
之后,如何将
数据插入
mysql

我在
Windows
中工作

这是我的源代码:

<?php 

        $con = mysql_connect("localhost", "root", "", "tut"); 

你的弦是错的。要正确粘贴变量,必须使用其他语法。

但是我不建议修改和使用这种代码。您将遇到sql注入问题。尝试改用PDO对象。它们不仅可以保存,而且更易于使用。

您的字符串concat是错误的。要正确粘贴变量,必须使用其他语法。
但是我不建议修改和使用这种代码。您将遇到sql注入问题。尝试改用PDO对象。它们不仅省钱,而且更易于使用。

虽然它可能有助于解决您当前的问题,但有几件事您应该注意

1. 正确使用此方法后,您甚至可能不需要发布问题。此方法返回抛出的错误的
文本
消息,并可以提供详细信息,如“字段列表”中的
未知列“xx”

与当前设置一起使用可能类似于:

$sql = mysql_query("INSERT INTO users VALUES('', {$FName}','{$LName}','{$Email}','{$PW}')", $con);
if (!$sql) die(mysql_error($con));
这会让您知道您有一个语法错误,如这里所示:
值(''',{$FName}'
$cost]); $end=微时间(真); }while(($end-$start)<$timeTarget); 返回$cost; } /**getHash($pass) * */ 函数getHash($pass){ 返回密码\u散列($pass,password\u DEFAULT,['cost'=>getCost()]); } $hashPass=getHash($_POST['Password']);
虽然“的”可能有助于解决您当前的情况,但有几件事您应该注意

1. 正确使用此方法后,您甚至可能不需要发布问题。此方法返回抛出的错误的
文本
消息,并可以提供详细信息,如“字段列表”中的
未知列“xx”

与当前设置一起使用可能类似于:

$sql = mysql_query("INSERT INTO users VALUES('', {$FName}','{$LName}','{$Email}','{$PW}')", $con);
if (!$sql) die(mysql_error($con));
这会让您知道您有一个语法错误,如这里所示:
值(''',{$FName}'
$cost]); $end=微时间(真); }while(($end-$start)<$timeTarget); 返回$cost; } /**getHash($pass) * */ 函数getHash($pass){ 返回密码\u散列($pass,password\u DEFAULT,['cost'=>getCost()]); } $hashPass=getHash($_POST['Password']);
这可能很快就是你这可能很快就是你@danielkanyi没问题,你应该注意到,自从你发表了这条评论后,我用更多的信息更新了我的答案。我希望这一切都能帮到你!祝你好运是的,我将研究PDO文档并使用它it@danielkanyi没问题,你应该注意到,自从你发表了这篇评论后,我更新了我的答案,提供了更多的信息。我希望这一切都能帮到你!祝你好运是的,我将研究PDO文档并使用它
$mysqli = new mysqli("localhost", "root", 'ge7@P@s$w04D', "tut");
if (mysqli_connect_errno()) die("Connect failed: " . mysqli_connect_error());

if (!($stmt = $mysqli->prepare("INSERT INTO users (name_first, name_last, email, pass) VALUES (?, ?, ?, ?)"))) die("Preperation failed: " . mysqli_error($mysqli));

$FName= $_POST['First_name'];
$LName = $_POST['Last_name'];
$Email = $_POST['Email'];
$PW = $_POST['Password'];

if (!($bind = mysqli_stmt_bind_param($stmt, "ssss", $FName, $LName, $Email, $PW))) die("Bind failed: " . E_USER_ERROR);

if (!($exec = mysqli_stmt_execute($stmt))) die("Failed to execute query: " . mysqli_stmt_error($stmt));
/** 
 *  This code will benchmark the server to determine how high of a cost i can
 *  afford. I want to set the highest cost that I can without slowing down
 *  the server too much. 8-10 is a good baseline, and more is good if the servers
 *  are fast enough. The code below aims for ≤ 50 milliseconds stretching time,
 *  which is a good baseline for systems handling interactive logins.
 *  */
function getCost() {
   $timeTarget = 0.05; // 50 milliseconds
   $cost = 8;
   do {
       $cost++;
       $start = microtime(true);
       password_hash("test", PASSWORD_BCRYPT, ["cost" => $cost]);
       $end = microtime(true);
   } while (($end - $start) < $timeTarget);
   return $cost;
}

/** getHash($pass)
 *  */
function getHash($pass) {
    return password_hash($pass, PASSWORD_DEFAULT, [ 'cost' => getCost() ]);
}

$hashPass = getHash($_POST['Password']);