简体   繁体   English

在数据库中找不到我的注册详细信息

[英]Can't find my registered details in the database

I'm a newbie to php and have been having issues on a project.I created a database and a table,created the register page and everything seems ok.however,all the details entered on the register page aren't appearing in the database.please can anyone help debug?the php code is as shown below 我是php的新手,并且在一个项目上遇到了问题。我创建了一个数据库和一个表,创建了注册页面,一切似乎都还不错。但是,在注册页面上输入的所有详细信息都没有出现在数据库中。任何人都可以帮助调试吗?php代码如下所示

<?php 
session_start(); 

require_once('config.php'); 

?> 

<?php 
if (isset($_POST['register'])){ 
$name = $_POST['name']; 
$email = $_POST['email']; 
$password = $_POST['password']; 
$cpassword = $_POST['confirm_password']; 

$sql = $con->query("INSERT into   'users' (id, name, email, 
password,confirm_password) VALUES     ('', {$name}', '{$email}', 
'{$password}', '{$cpassword}')"); 
die ("cannot connect to database;"); 
} 

?> 

CONFIG FILE: 
<?php 

$con = mysqli_connect("localhost",       "root", "","knux"); 

?> 

You are mixing procedural and object oriented approaches. 您正在混合过程和面向对象的方法。 mysqli_connect returns handle, not object so you cannot use $con->something . mysqli_connect返回句柄,而不是对象,因此您不能使用$con->something Use this to create db connection: 使用此命令创建数据库连接:

$con = new mysqli('localhost', 'root', '', 'knux');

More: http://php.net/manual/en/mysqli.construct.php 更多: http : //php.net/manual/en/mysqli.construct.php

There is a single quote missing from your "n" 您的“ n”中缺少单引号

('', {$name}', '{$email}', '{$password}', '{$cpassword}')

It should be: 它应该是:

('', '{$name}', '{$email}', '{$password}', '{$cpassword}')

There might be issues from your html however, you can try these: html中可能存在问题,但是您可以尝试以下方法:

  1. Make sure your fields are coming from the HTML from, you can do this with 确保您的字段来自HTML,您可以使用

     print_r($_POST); 

    If you see all the input, then the fault is not from the HTML. 如果看到所有输入,则故障不是来自HTML。

  2. In this case, there's really no need for you to have two PHP tags in one file, instead of 在这种情况下,实际上不需要一个文件中有两个PHP标记,而不是

     <?php session_start(); require_once('config.php'); ?> <?php if (isset($_POST['register'])){ $name = $_POST['name']; ... ?> 

You can have: 你可以有:

   <?php 
     session_start(); 

     require_once('config.php'); 

     if (isset($_POST['register'])){ 
     $name = $_POST['name'];
     ...
    ?>
  1. Use backticks when referring to the database elements like the table, instead of: 当引用诸如表之类的数据库元素时,请使用反引号,而不是:

      INSERT into 'users' (id, name, email, password,confirm_password) 

    do: 做:

     INSERT into `user` (`id`, `name`, `email`, `password`,`confirm_password`) 
  2. Use mysql_error(); 使用mysql_error(); in your or die(); 在您or die(); so you'll have: 因此您将拥有:

     or die(mysql_error()); 

This should tell you exactly what the issue is. 这应该告诉您确切的问题是什么。

Since you're still learning however, ill recommend PDO instead of PHP's mysql_ functions. 但是,由于您仍在学习,因此不推荐使用PDO而不是PHP的mysql_函数。 If you're feeling too comfortable with it, you can use the mysqli_ functions instead. 如果您对它感到不舒服,可以使用mysqli_函数代替。 I makes all the difference ;) I让一切变得不同;)

There is 'n single quote missing {$name}' 'n单引号缺少{$name}'

Also if id is auto increment it can be removed from the INSERT statement 另外,如果id是自动递增的,则可以将其从INSERT语句中删除

Correct Way: 正确方法:

$sql = $con->query("INSERT into   `users` (name, email, 
password,confirm_password) VALUES     ('{$name}', '{$email}', 
'{$password}', '{$cpassword}')"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM