当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Why isn't my coding connecting to my database?

问题描述:

<?php

include("connect.php");

session_start();

$name = "";

$fname = "";

$lname = "";

$pass = "";

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

$name = $_POST['name'];

$fname = $_POST['fname'];

$lname = $_POST['lname'];

$pass = $_POST['pass'];

if(empty($name) && empty($fname) && empty($lname) && empty($pass)){

echo "Please fill in all the fields";

}else {

$query_nam = mysqli_query($con,"SELECT * FROM users WHERE username = '$name'");

$query_pass = mysqli_query($con,"SELECT * FROM users WHERE password = '$pass'");

count_nam = mysql_num_count($query_nam);

count_pass = mysql_num_count($query_pass);

if($count_nam === 0 and $count_pass ==0){

mysqli_query($con,"INSERT INTO users(id,username,fname,lname,password)VALUES('','$name','$fname','$lname','$pass')");

echo "success!";

$_SESSION['$name'] = $sesson_name;

header("Location: index.php");

}else{

echo "Sorry that username/password is taken";

}

}

}

?>

<html>

<form action="practice.php"method="POST" >

First Name: <input type="text" name="fname"></br>

Last Name: <input type="text" name="lname"></br>

Username : <input type="text" name="name"></br>

Password : <input type="password" name="pass"></br>

<input type ="submit" name="submit">

</form>

</html>

The code hit a wall when it reached $query_nam, I don't why, when I did the mysql_error, it outputted that there "aren't any databases selected", however I included the connection.php, which made a bridge toward the db. I have repply as the database and users as the table.

Here's the connection.php

 <?php

$con = mysql_connect("localhost","root","") or die("Couldn't connect !");

mysql_select_db("repply") or die("Couldnt find db");

if (mysqli_connect_errno()) {

echo "Failed to connect to MySQL: " . mysqli_connect_error();

}else {

}

?>

please help. Thanks in advance. I know that this code is not secure. I'm not planning on publishing it. It's just for practice.

网友答案:

You should use mysqli_connect. Don't mix mysql with mysqli

Change

$con = mysql_connect("localhost","root","") or die("Couldn't connect !"); 
  mysqli_select_db("repply") or die("Couldnt find db"); 

to

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

I also think that in the include, it should be:

 include("connection.php");

In the insert, you forgot to put the connection. It should be

mysqli_query($con, "INSERT INTO users(id,username,fname,lname,password)VALUES('','$name','$fname','$lname','$pass')");
网友答案:

You are using mysqli_query() please use mysqli connection :

<?php
  $con = mysqli_connect("host","user","password","db");

  // Check connection
  if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>

then write your query :

<?php 

mysqli_query($con, "INSERT INTO users( id, username, fname, lname, password) VALUES('', '$name', '$fname', '$lname', '$pass')") or die(mysqli_error($con));

?>
网友答案:

that is not safe at all you use pdo for connection:

    $dsn = 'mysql:host=127.0.0.1;dbname=test';
        $username = 'root';
        $password = 'pass';
        $options = array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
        ); 
        $dbh = new PDO($dsn, $username, $password, $options);
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
and for using you can:

    $stmt = $dbh->query(SELECT * FROM users WHERE username = $name");
网友答案:

The thing is simple, check your "include'connect'" statement you actually defined "connection.php" not "connect.php" change that and see.

分享给朋友:
您可能感兴趣的文章:
随机阅读: