Article sections

    Using Prepared Statement

    SQL Injection is best prevented through the use of parameterized queries.

    The purpose of these code samples is to demonstrate to the web developer how to avoid SQL Injection when building database queries within a web application using ASP.NET, Java and PHP web languages.


    dotnet-logo

    string sql = "SELECT * FROM Customers WHERE CustomerId = @CustomerId"; SqlCommand command = new SqlCommand(sql); command.Parameters.Add(new SqlParameter("@CustomerId", System.Data.SqlDbType.Int)); command.Parameters["@CustomerId"].Value = 1;

    java-logo

    String custname = request.getParameter("customerName"); String query = "SELECT account_balance FROM user_data WHERE user_name = ? "; PreparedStatement pstmt = connection.prepareStatement( query ); pstmt.setString( 1, custname); ResultSet results = pstmt.executeQuery( );

    php-logo

    $stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)"); $stmt->bindParam(':name', $name); $stmt->bindParam(':value', $value);

    Audilitics

    Euriun LMS

    in Web Security