开发者

PHP Blank Values Inserted In Database

开发者 https://www.devze.com 2023-04-02 05:17 出处:网络
I\'m using PHP and Mysql Here I have used simple form to insert the Name, Email, Phone Number. I have validated all the data\'s: (PHP code after submit...)

I'm using PHP and Mysql

Here I have used simple form to insert the Name, Email, Phone Number.

I have validated all the data's: (PHP code after submit...)

$error = array(); 

if(isset($_POST['Name']) && $_POST['Name']=='') {
    $error[] = "Please enter the Name";
}

if(isset($_POST['Email']) && $_POST['Email']=='') {
    $error[] = "Please enter the Email";
}

if(isset($_POST['Phone']) && $_POST['Phone']=='') {
    $error[] = "Please enter the Phone";
}

Finally Insert the data using the below query..

if(count($error)==0) {
  $error= "";   
  And the query is
  insert into mytab ('Name','Email','Phone') Values (mysql_escape_string($_POST['Name']),mysql_escape_string($_POST['Email']),mysql_escape_string($_POST['Phone']));
  redircet to success.. 
} 

It's Works fine, But Some time i am geting Insert $error log like the below..

error Log :

 Time File name:myfile.php Insert $error: insert into mytab ('Name','Email','Phone') Values('','','');#~#

The log i was write using my db wrapper class (u开发者_JAVA百科sing mysql_$error())


Ideally you should abstract the database access from the rest of the code. You problem can be related to the mysql_escape_string() function. You should do that validation before the sql query, check if the value is valid and only then pass that value to the query to be executed.

Also, if the problem is inconsistent, you can do a stress test and log all the values you are trying to insert and all the return messages to see what kind of values cause the error.


In this part of your code

if(isset($_POST['Email']) && $_POST['Name'') {
    $error[] = "Please enter the Name";
}

if(isset($_POST['Phone']) && $_POST['Name'') {
    $error[] = "Please enter the Name";
}

There are heavy syntax errors. Perhaps here only though. Also, all of your conditions are evaluating, in the second statement about name,

I recommend validating this way.-

if (isset($_POST['Email']) && strlen($_POST['Email']))
    ...


Your validation should look like

if(!isset($_POST['Name']) || $_POST['Name'] =='')
 $error[] = "Please enter the Name";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号