简体   繁体   中英

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 
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 . Use this to create db connection:

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

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

There is a single quote missing from your "n"

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

It should be:

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

There might be issues from your html however, you can try these:

  1. Make sure your fields are coming from the HTML from, you can do this with

     print_r($_POST); 

    If you see all the input, then the fault is not from the HTML.

  2. In this case, there's really no need for you to have two PHP tags in one file, instead of

     <?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(); in your 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. If you're feeling too comfortable with it, you can use the mysqli_ functions instead. I makes all the difference ;)

There is 'n single quote missing {$name}'

Also if id is auto increment it can be removed from the INSERT statement

Correct Way:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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