无法使用PHP-MySQLi

时间:2016-06-15 15:14:50

标签: php mysql mysqli

编辑:问题被标记为重复,询问反对和其他事情?这不是我的意图。我想知道为什么这个特定的代码没有正确执行。

已解决:需要添加mysqli_query($mysql)以及$sql = "INSERT INTO userinfo (email, username, password, ip) VALUES ('$email', '$username', '$password', '$ip')";谢谢!

我的目标是创建一个注册页面,其中PHP接受输入,并通过MySQLi将数据添加到MySQL表。

表的名称是userinfo。我还检查了config.php并验证了密码,用户名和数据库都是正确的。

$db位于config.php

相关的PHP代码如下:

<?php
include("config.php");
session_start();

if($_SERVER["REQUEST_METHOD"] == "POST") {

  // username and password sent from form
  $username = mysqli_real_escape_string($db, $_POST['username']);
  $password = mysqli_real_escape_string($db, $_POST['password']);
  $email = mysqli_real_escape_string($db, $_POST['email']);
  $ip = mysqli_real_escape_string($db, "N/A");

  //checks for different ips (no masking allowed unless u tryhard)
  if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
     $ip = $_SERVER['HTTP_CLIENT_IP'];
  } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
      $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  } else {
      $ip = $_SERVER['REMOTE_ADDR'];
  }
  //inserts info into tables
  $sql = "INSERT INTO userinfo (email, username, password, ip) VALUES ($email, $username, $password, $ip);";

mysqli_query($mysql);
}
?>

表格中的所有输入都是基于文本的。

           <form action = "" method = "post">
              <label>Email: </label><input type = "text" name = "email" class = "box" /><br/><br />
              <label>Create UserName: </label><input type = "text" name = "username" class = "box"/><br /><br />
              <label>Create Password: </label><input type = "password" name = "password" class = "box" /><br/><br />
              <label>Verify password: </label><input type = "password" name = "vpassword" class = "box"/><br/><br/>
              <input type = "submit" value = " Submit "/><br />
           </form>

3 个答案:

答案 0 :(得分:1)

我没有看到任何执行查询的代码。使用mysqli_query($db, $sql)执行INSERT语句。如果这不是问题,请提供更多关于实际出错的信息。

修改同时检查您的对帐单$sql。变量不会被解析,而是保存为字符串。写VALUES ($email,...)VALUES ('" . $email . "',...)

,而不是撰写VALUES ('{$email}',...)

答案 1 :(得分:0)

$sql = "INSERT INTO userinfo (email, username, password, ip) VALUES ('$email', '$username', '$password', '$ip')";

答案 2 :(得分:-1)

试试这个:

{
    "_id" : ObjectId("57616e718ed5a017089143f2"),
    "subitems" : {
        "1" : "a",
        "2" : "b",
        "3" : "c"
    }
}