OneCompiler

DS28

236

Q. 1) Write a PHP script using AJAX concept, to check user name and password are valid or Invalid (use
database to store user name and password). [Marks 15]
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL
);

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Login</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("#loginBtn").click(function(){ var username = $("#username").val(); var password = $("#password").val();
    $.ajax({
        url: 'check_login.php',
        method: 'POST',
        data: {username: username, password: password},
        success: function(response) {
            $("#result").html(response);
        }
    });
});

});
</script>

</head> <body> <h2>Login</h2> <form> <label for="username">Username:</label><br> <input type="text" id="username" name="username"><br><br> <label for="password">Password:</label><br> <input type="password" id="password" name="password"><br><br> <input type="button" id="loginBtn" value="Login"> </form> <div id="result"></div> </body> </html> <?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "database_name";

// Create connection
conn=newmysqli(conn = new mysqli(servername, username,username, password, $dbname);

// Check connection
if (conn->connect_error) { die("Connection failed: " . conn->connect_error);
}

username=username = _POST['username'];
password=password = _POST['password'];

sql="SELECTFROMusersWHEREusername=sql = "SELECT * FROM users WHERE username = 'username' AND password = 'password";password'"; result = conn>query(conn->query(sql);

if ($result->num_rows > 0) {
echo "<p style='color: green;'>Login successful. Username and password are valid.</p>";
} else {
echo "<p style='color: red;'>Login failed. Invalid username or password.</p>";
}

$conn->close();
?>

Q. 2 ) Build a simple linear regression model for Car Dataset
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

Assuming you have a CSV file named 'car_dataset.csv'

df = pd.read_csv('car_dataset.csv')

Display the first few rows of the dataset

print(df.head())

Select features (independent variables) and target variable (dependent variable)

X = df[['Feature1', 'Feature2', ...]] # Features
y = df['Price'] # Target variable

Split the data into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

Create a linear regression model

model = LinearRegression()

Train the model using the training sets

model.fit(X_train, y_train)

Make predictions on the testing set

y_pred = model.predict(X_test)

Calculate and print metrics

print('Mean Squared Error:', mean_squared_error(y_test, y_pred))
print('Coefficient of Determination (R^2):', r2_score(y_test, y_pred))

Example prediction

new_data = [[value1, value2, ...]] # Provide values for the features
predicted_price = model.predict(new_data)
print('Predicted Price:', predicted_price)