0% found this document useful (0 votes)
23 views

PHP

The document shows PHP code for connecting to a database and inserting form data into a database table. It includes code to handle file uploads and store images in a directory. The form allows users to submit their name, email, password, gender, phone number, and a photo.

Uploaded by

Shamim Hosen
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

PHP

The document shows PHP code for connecting to a database and inserting form data into a database table. It includes code to handle file uploads and store images in a directory. The form allows users to submit their name, email, password, gender, phone number, and a photo.

Uploaded by

Shamim Hosen
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

<?

php
$servername = "localhost"; // Change this to your database server name
$username = "your_username"; // Change this to your database username
$password = "your_password"; // Change this to your database password
$dbname = "your_database_name"; // Change this to your database name

// Create a database connection


$conn = new mysqli($servername, $username, $password, $dbname);

// Check the connection


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

// Check if the form is submitted


if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$email = $_POST["email"];
$password = $_POST["password"];
$gender = $_POST["gender"];
$phone = $_POST["phone"];

// SQL query to insert data into the "users" table


$sql = "INSERT INTO users (name, email, password, gender, phone)
VALUES ('$name', '$email', '$password', '$gender', '$phone')";

if ($conn->query($sql) === TRUE) {


echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}

// Close the database connection


$conn->close();
?>

<!DOCTYPE html>
<html>
<head>
<title>Simple Form</title>
</head>
<body>
<h2>Simple Form</h2>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="name">Name:</label>
<input type="text" name="name" id="name" required><br><br>

<label for="email">Email:</label>
<input type="email" name="email" id="email" required><br><br>

<label for="password">Password:</label>
<input type="password" name="password" id="password" required><br><br>

<label>Gender:</label>
<input type="radio" name="gender" value="Male" id="male"> <label for="male">Male</label>
<input type="radio" name="gender" value="Female" id="female"> <label
for="female">Female</label><br><br>

<label for="phone">Phone Number:</label>


<input type="tel" name="phone" id="phone" required><br><br>

<input type="submit" value="Submit">


</form>
</body>
</html>
<?php
$servername = "localhost"; // Change this to your database server name
$username = "your_username"; // Change this to your database username
$password = "your_password"; // Change this to your database password
$dbname = "your_database_name"; // Change this to your database name

// Create a database connection


$conn = new mysqli($servername, $username, $password, $dbname);

// Check the connection


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

// Check if the form is submitted


if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$email = $_POST["email"];
$password = $_POST["password"];
$gender = $_POST["gender"];
$phone = $_POST["phone"];

// Handle photo upload


$targetDirectory = "uploads/";
$targetFile = $targetDirectory . basename($_FILES["photo"]["name"]);
$imageFileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));

if (move_uploaded_file($_FILES["photo"]["tmp_name"], $targetFile)) {
// File uploaded successfully
$photo = $targetFile;

// SQL query to insert data into the "users" table


$sql = "INSERT INTO users (name, email, password, gender, phone, photo)
VALUES ('$name', '$email', '$password', '$gender', '$phone', '$photo')";

if ($conn->query($sql) === TRUE) {


echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
} else {
echo "Error uploading photo.";
}
}

// Close the database connection


$conn->close();
?>

<!DOCTYPE html>
<html>
<head>
<title>Simple Form with Photo Upload</title>
</head>
<body>
<h2>Simple Form with Photo Upload</h2>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-
data">
<label for="name">Name:</label>
<input type="text" name="name" id="name" required><br><br>

<label for="email">Email:</label>
<input type="email" name="email" id="email" required><br><br>

<label for="password">Password:</label>
<input type="password" name="password" id="password" required><br><br>

<label>Gender:</label>
<input type="radio" name="gender" value="Male" id="male"> <label for="male">Male</label>
<input type="radio" name="gender" value="Female" id="female"> <label
for="female">Female</label><br><br>

<label for="phone">Phone Number:</label>


<input type="tel" name="phone" id="phone" required><br><br>

<label for="photo">Upload Photo:</label>


<input type="file" name="photo" id="photo" accept="image/*" required><br><br>

<input type="submit" value="Submit">


</form>
</body>
</html>

You might also like