php - 错误 : Inserting data into mySQL

标签 php mysql

我不知道我做错了什么,但它没有插入我希望它放入数据库的数据。除了我创建的 mysql 错误之外,没有给我任何代码错误,例如“错误插入”和类似的东西。我也做了很多关于登录源的谷歌搜索,并试图看看有什么区别,但没有用。我找不到问题所在!请帮忙。

<?php
include_once('connect.php')

$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];

if (isset($_POST['dalol'])){
    test();
}

function test(){
    $sql = "INSERT INTO members (username, email, password) VALUES ('".$GLOBALS['username']."','".$GLOBALS['email']."','".$GLOBALS['password']."')";
    if (mysql_query($sql)){
        echo '<p>Successfully Executed!</p>';
    } else {
        echo '<p>Failed!</p>';
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Insert Test</title>
</head>
<body>
    <form>
        Username: <input type="text" name="username"/><br>
        Email: <input type="email" name="email"/><br>
        Password: <input type="password" name="password"/><br>
        <input type='submit' name='dalol'/>
    </form>
</body>

最佳答案

你可以使用全局变量!

就是这样:

<?php
function test(){
global $username,$email,$password;
$sql = "INSERT INTO members (username, email, password) VALUES ('".$username."','".$email."','".$password."')";
    if (mysql_query($sql)){
        echo '<p>Successfully Executed!</p>';
    } else {
        echo '<p>Failed!</p>';
    }
} ?>

Prevent sql injection

 <?php
    $username = mysql_real_escape_string($_POST['username']);
    $email = mysql_real_escape_string($_POST['email']);
    $password = mysql_real_escape_string($_POST['password']);
     ?>

关于php - 错误 : Inserting data into mySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28536254/

相关文章:

php - RouteCollection.php 中的 MethodNotAllowedHttpException 第 200 行 :

php - 使用 POST 数据值作为参数时出现 PDO 错误

php - 如何在 symfony 1.4 中手动创建一个新 session

php - 使用 STR_TO_DATE 时的更新问题

java - 从 Java 调用 PHP 时的 Json 格式问题

java - 是否可以在谷歌应用引擎上部署 cakephp 应用程序?

mysql - 如何分离这两个过程?

php - laravel 5.2 将登录 session 数据插入自定义身份验证的数据库

php - 从不同的Mysql表中获取具有相同名称的列

PHP 和 MySQL : php is not finding the image path given by mysql