Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 关于PHP数据库的小问题

关于PHP数据库的小问题

Jun 23, 2016 pm 02:15 PM

PHP 数据库 用户验证

$con=mysql_connect("localhost","root","123456");
if(!$con)
{
die('Could not connect:'.mysql_error());
}
mysql_query("SET NAMES 'utf8'"); //设置字符集
mysql_select_db("manage",$con);   //选择数据库
$sql1="
select count(*)
from user
where username=="".$_POST['name']."" and password=="".$_POST['password'].""
LIMIT 1
";
$result=mysql_query($sql1,$con);
红色部分报错。前辈们,在下有两个问题:第一,目前这种格式,应该怎么改,使其正确。第二,你们在验证用户信息时,是这么做的呢?比较简单的那种。

回复讨论(解决方案)

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


然后用mysql_num_rows 判断行是否等于1就行。

username='".mysql_real_escape_string($_POST['name'])."' and password='".mysql_real_escape_string($_POST['password'])."

最好做个简单的防注入措施。

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

php5.5以上版本,用mysqli_real_escape_string() 或者 PDO::quote()

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


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

不需要 LIMIT 1
因为没有 group 子句,聚类函数只会取得一条记录

对传入的数据做一下转义处理是必须的,在程序入口处这样就可以了
$_POST = array_map('mysql_real_escape_string', $_POST);

$_POST = array_map('mysql_real_escape_string', $_POST);

如果某$_POST为空,会不会报错?

 直接在sql 里使用'$_POST[name]'  之类的不会出错吗..
一般都要 先判断一下吧比如 ISSET之类的.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles