Small PHP database problems

Source: Internet
Author: User
PHP database user verification

$ Con = mysql_connect ("localhost", "root", "123456 ");
If (! $ Con)
{
Die ('could not connect: '. mysql_error ());
}
Mysql_query ("set names 'utf8'"); // sets the character SET
Mysql_select_db ("manage", $ con); // select a database
$ Sql1 ="
Select count (*)
From user
Where username = "". $ _ POST ['name']. "" and password = "". $ _ POST ['password']. ""
LIMIT 1
";
$ Result = mysql_query ($ sql1, $ con );
An error is reported in red. The predecessors have two problems: first, how should we change the current format to make it correct. Second, what do you do when verifying user information? Which is relatively simple.


Reply to discussion (solution)

$sql1="select count(*)from userwhere username='".$_POST['name']."' and password='".$_POST['password']."'LIMIT 1";


Then, use mysql_num_rows to determine whether the row is equal to 1.

Username = '". mysql_real_escape_string ($ _ POST ['name'])."' and password = '". mysql_real_escape_string ($ _ POST ['password'])."

It is best to take a simple anti-injection measure.

Http://php.net/manual/en/function.mysql-real-escape-string.php

Php5.5 or later, use mysqli_real_escape_string () or PDO: quote ()

$sql1 = "select count(*)from userwhere username='".$_POST['name']."' and password=='".$_POST['password']."'";

Or
$sql1 = "select count(*)from userwhere username='$_POST[name]' and password='$_POST[password]'";

LIMIT 1 is not required
Because no group clause exists, the clustering function only obtains one record.

It is necessary to perform escape processing on the incoming data, so that it can be done at the entrance of the program.
$ _ POST = array_map ('MySQL _ real_escape_string ', $ _ POST );

$ _ POST = array_map ('MySQL _ real_escape_string ', $ _ POST );

If $ _ POST is empty, will an error be reported?

If you use '$ _ POST [name]' directly in an SQL statement, will it not cause errors ..
Generally, you should first judge it, such as ISSET.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.