Click here to Skip to main content
14,387,819 members
Rate this:
Please Sign up or sign in to vote.
See more:
How to fix that? i have checked all spelling and all same.

What I have tried:

<?php

require 'db.php';

if(isset($_POST['submit'])){





$DepName = $_POST['DepName'];
$Address = $_POST['Address'];
$PIC = $_POST['PIC'];
$Contact = $_POST['Contact'];
$RecordVisit = $_POST['RecordVisit'];
$Outlet = $_POST['Outlet'];
$JobTitle = $_POST['JobTitle'];
$Sales = $_POST['Sales'];
$JobDate = $_POST['JobDate'];
$Note = $_POST['Note'];
}

$result = $mysqli->query("SELECT * FROM villadata WHERE VillaID='VillaID'") or die($mysqli->error());

if ( $result->num_rows > 0 ) {
    
    $_SESSION['message'] = 'User already exists! Please try other username!';
    echo "User exist";
}

else{
$sql="INSERT INTO villadata(DepName,Address,PIC,Contact,RecordVisit,Outlet,JobTitle,Sales,JobDate,Note)
VALUES('$DepName','$Address','$PIC','$Contact','$RecordVisit','$Outlet','$JobTitle','$Sales','$JobDate','$Note')";

 if ( $mysqli->query($sql) ){

        $_SESSION['logged_in'] = true; 
        header("location: tables-data.php");

    }

  else {
        $_SESSION['message'] = 'Registration failed!';
        echo "Registration failed.";
    }

}
?>
Posted
Updated 18-Jan-19 11:54am
Rate this:
Please Sign up or sign in to vote.

Solution 1

Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
DROP TABLE MyTable;
A perfectly valid "delete the table" command
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Once you have gone through your whole app fixing this, you may well find the problem you noticed has gone away ...
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

$sql="INSERT INTO villadata(DepName,Address,PIC,Contact,RecordVisit,Outlet,JobTitle,Sales,JobDate,Note)
VALUES('$DepName','$Address','$PIC','$Contact','$RecordVisit','$Outlet','$JobTitle','$Sales','$JobDate','$Note')";

Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100