Posts

PHP Upload PDF file

  To dynamically upload a PDF file in a folder using PHP, you can use the following program: php Copy code <?php $target_dir = "uploads/" ; // specify the directory where you want to upload the file $target_file = $target_dir . basename ( $_FILES [ "fileToUpload" ][ "name" ]); // get the name of the uploaded file $uploadOk = 1 ; $fileType = strtolower ( pathinfo ( $target_file ,PATHINFO_EXTENSION)); // Check if file already exists if ( file_exists ( $target_file )) { echo "Sorry, file already exists." ; $uploadOk = 0 ; } // Allow only pdf files if ( $fileType != "pdf" ) { echo "Sorry, only PDF files are allowed." ; $uploadOk = 0 ; } // Check if $uploadOk is set to 0 by an error if ( $uploadOk == 0 ) { echo "Sorry, your file was not uploaded." ; // if everything is ok, try to upload file } else { if ( move_uploaded_file ( $_FILES [ "fileToUpload" ][ "

PHP PROGRAM TO STORE VALUE IN DATABASE USING TEXT BOX

  Sure, here's a simple PHP program that allows you to store data into a database using a text box: php Copy code <?php // Establish database connection $servername = "localhost" ; $username = "username" ; $password = "password" ; $dbname = "database_name" ; $conn = mysqli_connect ( $servername , $username , $password , $dbname ); // Check connection if (! $conn ) { die ( "Connection failed: " . mysqli_connect_error ()); } // Check if form is submitted if ( $_SERVER [ "REQUEST_METHOD" ] == "POST" ) { // Get input values from form $name = $_POST [ "name" ]; $email = $_POST [ "email" ]; $message = $_POST [ "message" ]; // Prepare SQL statement to insert data into database $sql = "INSERT INTO messages (name, email, message) VALUES (' $name ', ' $email ', ' $message ')" ; // Execute SQL statement a

PHP Database Connectivity

  To connect to a database using PHP, you can use the PHP Data Objects (PDO) extension or the MySQLi extension. Here are the basic steps for connecting to a MySQL database using each of these extensions: Using PDO: Create a new PDO object with the database connection details: php Copy code $db = new PDO ( 'mysql:host=localhost;dbname=mydatabase' , 'username' , 'password' ); Replace "mydatabase", "username", and "password" with your actual database name, username, and password. (Optional) Set the error mode to throw exceptions for any errors: php Copy code $db -> setAttribute (PDO:: ATTR_ERRMODE , PDO:: ERRMODE_EXCEPTION ); Use the PDO object to execute SQL queries: bash Copy code $stmt = $db ->query( 'SELECT * FROM mytable' ); Using MySQLi: Create a new MySQLi object with the database connection details: php Copy code $db = new mysqli ( 'localhost' , 'username' , 'password' , 'mydatabas