插入MySQL数据库,查询未运行

时间:2014-03-27 17:11:17

标签: php mysql

我正在为学校作业进行简单的注册/表格。 出于某种原因,我无法在当前表格中创建新列。 $ _Get的所有信息都准确无误。我想象它的语法错误我没有看到。任何帮助都会很棒。谢谢。

 if ( $_GET['action'] == "create" )

    {

        print('test');
        // -----------------------
        // PERFORM DATABASE UPDATE
        $fn = $_GET['fn'];
        $ln = $_GET['ln'];
        $id = $_GET['id'];
        $user = $_GET['user'];
        $tel = $_GET['tel_num'];
        $email = $_GET['email'];
        $bday = $_GET['birthday'];
        $password = $_GET['password'];
        $address = $_GET['address'];

        print('test1');

        mysql_select_db("advweb2");


        $sql="INSERT INTO `account` (`user`, `password` , `email` , `first_name` , `last_name` , `address` , `tel_num` , `birthday`)
        VALUES ('$user', '$password', '$email', '$fn', '$ln', '$address', '$tel', '$bday')"; 
        print_r($sql);



        print("<div style='color:green'>update successful</div>");

        // -----------------------

        $action = "signin";
    }

3 个答案:

答案 0 :(得分:0)

请不要使用mysql_query,切换到mysqli或PDO

$dbh = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':value', $value);


$name = 'one';
$value = 1;
$stmt->execute();

$dbh = null;

答案 1 :(得分:0)

您需要执行查询。

您应该使用MySQLi或PDO详细here,因为不推荐使用mysql_query。

mysqli的例子:

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

$stmt = $mysqli->prepare(
  "INSERT INTO `account` (`user`, `password` , `email` , `first_name` , `last_name` , `address` , `tel_num` , `birthday`)
    VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
);
$stmt->bind_param('ssssssss', $user, $password, $email, $fn, $ln, $address, $tel, $bday);
$stmt->execute();

/* ... */

$stmt->close()
?>

在插入数据库之前,您需要确保清理$_GET变量以防止SQL注入。好读:how to prevent SQL injection

答案 2 :(得分:0)

如其他人所解释的那样使用Mysqli或PDO,但如果你坚持执行这样的查询:

$sql=mysql_query("INSERT INTO `account` (`user`, `password` , `email` , `first_name` , `last_name` , `address` , `tel_num` , `birthday`)
        VALUES ('$user', '$password', '$email', '$fn', '$ln', '$address', '$tel', '$bday')"); 

因为您准备了查询并将其分配给变量但却错过了执行它。