Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.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 SQL插入故障_Php_Mysql - Fatal编程技术网

Php SQL插入故障

Php SQL插入故障,php,mysql,Php,Mysql,我在这个代码前坐了两个小时我不知道出了什么问题:( 我一直在尝试创建一个html表单,该表单调用php函数将表单中的信息插入数据库,但由于某些原因无法正常工作:/ 这是我的表单代码: <?php include 'connection.php'; ?> <html> <body> <form action="user_create.php" method="POST"> username: <input type="text" name="u

我在这个代码前坐了两个小时我不知道出了什么问题:(

我一直在尝试创建一个html表单,该表单调用php函数将表单中的信息插入数据库,但由于某些原因无法正常工作:/

这是我的表单代码:

<?php include 'connection.php'; ?>
<html>
<body>
<form action="user_create.php" method="POST">
username: <input type="text" name="username"/>
password: <input type="text" name="password"/>
email: <input type="text" name="email"/>
<input type="submit" name='submit' value="user_create"/>
</form>
</body>
</html>

用户名:
密码:
电邮:
数据库连接

<?php 

//Connecting databases 

$localhost  = ""; 
$dbuser = "";
$dbpass = "m"; 
$dbname = ""; 

$connect = mysql_connect($localhost, $dbuser, $dbpass);
mysql_select_db("$dbname", $connect);  

?>  

我的php函数

<?php include 'connection.php';?>
<?php
if (isset($_POST['submit']))
    {
    $username = $_POST['username']; 
    $password = $_POST['password']; 
    $email = $_POST['email']; 


$query = mysql_query("INSERT INTO users( username,password,email,type) 
VALUES ('$username', '$password', '$email','1')");
mysql_query($query); 
echo '<script type="text/javascript">alert("You have been registered");</script>';
}
else
    {
        echo '<script type="text/javascript">alert("jo");</script>';
    }

?> 

您连接正确吗

$dbuser=”“;

可能必须是“root”或其他用户

检查此部分:

//Connecting databases 

$localhost  = ""; 
$dbuser = "";
$dbpass = "m"; 
$dbname = "";

您应该使用PHP-PDO,以避免SQL注入攻击,它还将修复插入问题

<?php
/*** mysql hostname ***/
$hostname = 'localhost';

/*** mysql username ***/
$username = 'username';

/*** mysql password ***/
$password = 'password';

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=animals", $username, $password);
    /*** echo a message saying we have connected ***/
    echo 'Connected to database<br />';

    /*** INSERT data ***/
    $sql = "INSERT INTO users(username,
            password,
            email,
            type) VALUES (
            :username, 
            :password, 
            :email, 
            :type)";

    $stmt = $pdo->prepare($sql);

    $stmt->bindParam(':username', $_POST['username'], PDO::PARAM_STR);      
    $stmt->bindParam(':password', $_POST['password'], PDO::PARAM_STR);
    $stmt->bindParam(':email', $_POST['email'], PDO::PARAM_STR);
    $stmt->bindParam(':type', $_POST['type'], PDO::PARAM_INT);

$stmt->execute(); 
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
?>


它是如何工作的?你有没有收到错误?你收到了什么错误消息?你做了什么来解决这个问题?只希望你的一个用户没有“小Bobby Tables”的昵称或者,考虑解决SQL注入漏洞。1。你对SQL注入是开放的。2。停止使用<代码> MySQL< < /代码>。3。不要存储明文密码。4。你的第二个<代码> MySQLYQuase<代码>将失败,因为它使用了你的第一个< MySQLYQuase 5的结果。你的DB连接变量大部分都是空的-这是怎么回事?6.如果可以使用
“$var”
,请不要使用
“$var”
。您是否说它会在浏览器中显示PHP代码?如果发生这种情况,代码甚至不会运行。文件是否安装在安装了PHP的服务器上?您是否通过服务器运行该文件(即不只是在浏览器中打开它)?@spencer7593:这也会导致类似的情况。“我的代码不起作用,我懒得检查它是否真的起作用,请帮我修复它。”。此凭据和数据库名称很可能是为了增强安全性而删除的,而不是将其用户名和密码公开。事实上,如果此代码生效,我们可以通过简单的注入获得根访问权限。