{"id":5127,"date":"2024-05-15T01:19:02","date_gmt":"2024-05-15T01:19:02","guid":{"rendered":"https:\/\/isophal.com\/?p=5127"},"modified":"2024-05-15T02:14:22","modified_gmt":"2024-05-15T02:14:22","slug":"crud-operation-in-php-using-mysql","status":"publish","type":"post","link":"https:\/\/isophal.com\/news\/2024\/05\/15\/5127.html\/","title":{"rendered":"CRUD Operation in PHP using MySQL"},"content":{"rendered":"\n<p><h2 style=\"margin: 0px; padding: 0px; box-sizing: border-box; font-family: Roboto, sans-serif; font-weight: 400; line-height: 1.1; color: rgb(33, 33, 33); font-size: 24px; white-space-collapse: collapse;\">Create database and tables in MySQL using the WAMP\/XAMPP server<\/h2><\/p>\n\n\n\n<p><strong>Step&nbsp;1.&nbsp;<\/strong>Launch the WAMP\/XAMPP control panel and start the Apache and MySQL services.<\/p>\n\n\n\n<p>Open phpMyAdmin:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"700\" src=\"https:\/\/isophal.com\/wp-content\/uploads\/2024\/05\/image.png\" alt=\"\" class=\"wp-image-5128\" srcset=\"https:\/\/isophal.com\/news\/wp-content\/uploads\/2024\/05\/image.png 700w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2024\/05\/image-100x100.png 100w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2024\/05\/image-600x600.png 600w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2024\/05\/image-300x300.png 300w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2024\/05\/image-585x585.png 585w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/figure>\n\n\n\n<p><strong>Step 2.&nbsp;<\/strong>Open your web browser and navigate to&nbsp;<strong>http:\/\/localhost\/phpmyadmin.<\/strong>&nbsp;This will open the phpMyAdmin interface.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"700\" src=\"https:\/\/isophal.com\/wp-content\/uploads\/2024\/05\/image-1.png\" alt=\"\" class=\"wp-image-5129\" srcset=\"https:\/\/isophal.com\/news\/wp-content\/uploads\/2024\/05\/image-1.png 700w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2024\/05\/image-1-100x100.png 100w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2024\/05\/image-1-600x600.png 600w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2024\/05\/image-1-300x300.png 300w, https:\/\/isophal.com\/news\/wp-content\/uploads\/2024\/05\/image-1-585x585.png 585w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/figure>\n\n\n\n<p><strong>Step 3.&nbsp;<\/strong>In the phpMyAdmin interface, click on the&nbsp;<strong>&#8220;Databases&#8221;<\/strong>&nbsp;tab. Enter a name for your database (<strong>e.g., &#8220;student&#8221;<\/strong>) in the&nbsp;<strong>&#8220;Create database&#8221;<\/strong>&nbsp;field.<\/p>\n\n\n\n<p>Click the&nbsp;<strong>&#8220;Create&#8221;<\/strong>&nbsp;button to create the database.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.c-sharpcorner.com\/article\/crud-operation-in-php-using-mysql\/Images\/img3.jpg\" alt=\"CRUD Operation in PHP using MySQL\"\/><\/figure>\n\n\n\n<p><strong>Step 4.&nbsp;<\/strong>After creating the database, click on its name in the left sidebar to select it. Click on the&nbsp;<strong>&#8220;SQL&#8221;<\/strong>&nbsp;tab to run SQL queries. Create a&nbsp;<strong>&#8220;students&#8221;<\/strong>&nbsp;Table: In the SQL tab, enter the following SQL query&nbsp;to create the<strong>&nbsp;&#8220;students&#8221;<\/strong>&nbsp;table:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE students (\n    id INT PRIMARY KEY AUTO_INCREMENT,\n    name VARCHAR(50) NOT NULL,\n    age INT,\n    email VARCHAR(50),\n    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>Click the&nbsp;<strong>&#8220;Go&#8221;<\/strong>&nbsp;button to execute the query and create the table.<\/p>\n\n\n\n<p>That&#8217;s it! You have created a student database with a &#8220;students&#8221; table.&nbsp;You can now perform CRUD operations on these tables using PHP and MySQL.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">CRUD operations on these tables using PHP and MySQL<\/h2>\n\n\n\n<p>You can create a &nbsp;project folder like the following structure.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.c-sharpcorner.com\/article\/crud-operation-in-php-using-mysql\/Images\/img4.jpg\" alt=\"CRUD Operation in PHP using MySQL\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">How to Create a MySQL Database Connection?<\/h2>\n\n\n\n<p>The following code acts as the connection between the webpage and the database where the data from the webpage will be stored.<\/p>\n\n\n\n<p>The name of the file is&nbsp;<strong>dbconfig.php.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$servername = \"localhost\";\n$username = \"root\";\n$password = \"\";\n$dbname = \"student\";\n$conn = new mysqli($servername, $username, $password, $dbname);\nif ($conn-&gt;connect_error) {\n    die(\"Connection failed: \" . $conn-&gt;connect_error);\n}\n?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How to Create Records?<\/h2>\n\n\n\n<p>The name of the file is&nbsp;<strong>create-student.php.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;title&gt;Student Database&lt;\/title&gt;\n&lt;body&gt;\n&lt;h2&gt;Student Form&lt;\/h2&gt;\n&lt;form action=\"\" method=\"POST\"&gt;\n  &lt;fieldset&gt;\n    &lt;legend&gt;Student information:&lt;\/legend&gt;\n    Name:&lt;br&gt;\n    &lt;input type=\"text\" name=\"name\"&gt; &lt;br&gt;\n    Age:&lt;br&gt;\n    &lt;input type=\"text\" name=\"age\"&gt; &lt;br&gt;\n    Email:&lt;br&gt;\n    &lt;input type=\"email\" name=\"email\"&gt;&lt;br&gt;\n    &lt;br&gt;&lt;br&gt;\n    &lt;input type=\"submit\" name=\"submit\" value=\"submit\"&gt;\n  &lt;\/fieldset&gt;\n&lt;\/form&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n\n&lt;?php\ninclude \"dbconfig.php\";\n  if (isset($_POST&#91;'submit'])) {\n    $name = $_POST&#91;'name'];\n    $age = $_POST&#91;'age'];\n    $email = $_POST&#91;'email'];\n    $sql = \"INSERT INTO `students`(`name`, `age`, `email`) VALUES ('$name','$age','$email')\";\n    $result = $conn-&gt;query($sql);\n    if ($result == TRUE) {\n      echo \"New record created successfully.\";\n      header('Location: view-student.php');\n    }else{\n      echo \"Error:\". $sql . \"&lt;br&gt;\". $conn-&gt;error;\n    }\n    $conn-&gt;close();\n  }\n?&gt;<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><img decoding=\"async\" alt=\"CRUD Operation in PHP using MySQL\" class=\"\" src=\"https:\/\/www.c-sharpcorner.com\/article\/crud-operation-in-php-using-mysql\/Images\/img5.jpg\"><\/h3>\n\n\n\n<h2 class=\"wp-block-heading\">How to View Records?<\/h2>\n\n\n\n<p>The name of the file is&nbsp;<strong>view-student.php.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\ninclude \"dbconfig.php\";\n?&gt;\n&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;Student Database&lt;\/title&gt;\n&lt;link rel=\"stylesheet\" href=\"https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/3.4.0\/css\/bootstrap.min.css\"&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\n    &lt;div class=\"container\"&gt;\n        &lt;h2&gt;Student Details&lt;\/h2&gt;\n&lt;table class=\"table\"&gt;\n    &lt;thead&gt;\n        &lt;tr&gt;\n        &lt;th&gt;ID&lt;\/th&gt;\n        &lt;th&gt;Name&lt;\/th&gt;\n        &lt;th&gt;Age&lt;\/th&gt;\n        &lt;th&gt;Email&lt;\/th&gt;\n        &lt;th&gt;Action&lt;\/th&gt;\n\n    &lt;\/tr&gt;\n    &lt;\/thead&gt;\n    &lt;tbody&gt;\n        &lt;?php\n                $sql = \"SELECT * FROM students\";\n                $result = $conn-&gt;query($sql);\n                if ($result-&gt;num_rows &gt; 0) {\n                while ($row = $result-&gt;fetch_assoc()) {\n        ?&gt;\n                    &lt;tr&gt;\n                    &lt;td&gt;&lt;?php echo $row&#91;'id']; ?&gt;&lt;\/td&gt;\n                    &lt;td&gt;&lt;?php echo $row&#91;'name']; ?&gt;&lt;\/td&gt;\n                    &lt;td&gt;&lt;?php echo $row&#91;'age']; ?&gt;&lt;\/td&gt;\n                    &lt;td&gt;&lt;?php echo $row&#91;'email']; ?&gt;&lt;\/td&gt;\n                    &lt;td&gt;&lt;a class=\"btn btn-info\" href=\"update-student.php?id=&lt;?php echo $row&#91;'id']; ?&gt;\"&gt;Edit&lt;\/a&gt;\n                     &amp;nbsp;\n                     &lt;a class=\"btn btn-danger\" href=\"delete-student.php?id=&lt;?php echo $row&#91;'id']; ?&gt;\"&gt;Delete&lt;\/a&gt;\n                    &lt;\/td&gt;\n                    &lt;\/tr&gt;\n        &lt;?php       }\n            }\n        ?&gt;\n    &lt;\/tbody&gt;\n&lt;\/table&gt;\n    &lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>PHPCopy<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><img decoding=\"async\" alt=\"\" class=\"\" src=\"https:\/\/www.c-sharpcorner.com\/article\/crud-operation-in-php-using-mysql\/Images\/img6.jpg\"><\/h3>\n\n\n\n<h2 class=\"wp-block-heading\">How to Update&nbsp;Records?<\/h2>\n\n\n\n<p>The name of the file is&nbsp;<strong>update-student.php.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\ninclude \"dbconfig.php\";\n    if (isset($_POST&#91;'update'])) {\n        $stu_id = $_POST&#91;'stu_id'];\n        $name = $_POST&#91;'name'];\n        $age = $_POST&#91;'age'];\n        $email = $_POST&#91;'email'];\n        $sql = \"UPDATE `students` SET `name`='$name',`age`='$age',`email`='$email' WHERE `id`='$stu_id'\";\n        $result = $conn-&gt;query($sql);\n        if ($result == TRUE) {\n            echo \"Record updated successfully.\";\n            header('Location: view-student.php');\n        }else{\n            echo \"Error:\" . $sql . \"&lt;br&gt;\" . $conn-&gt;error;\n        }\n\n    }\n\nif (isset($_GET&#91;'id'])) {\n    $stu_id = $_GET&#91;'id'];\n    $sql = \"SELECT * FROM students WHERE id='$stu_id'\";\n    $result = $conn-&gt;query($sql);\n    if ($result-&gt;num_rows &gt; 0) {\n        while ($row = $result-&gt;fetch_assoc()) {\n            $id = $row&#91;'id'];\n            $name = $row&#91;'name'];\n            $age = $row&#91;'age'];\n            $email = $row&#91;'email'];\n        }\n    ?&gt;\n\n        &lt;h2&gt;Student details Update Form&lt;\/h2&gt;\n        &lt;form action=\"\" method=\"post\"&gt;\n          &lt;fieldset&gt;\n            &lt;legend&gt;Personal information:&lt;\/legend&gt;\n            Name:&lt;br&gt;\n            &lt;input type=\"text\" name=\"name\" value=\"&lt;?php echo $name; ?&gt;\"&gt;\n            &lt;input type=\"hidden\" name=\"stu_id\" value=\"&lt;?php echo $id; ?&gt;\"&gt;\n            &lt;br&gt;\n            Age:&lt;br&gt;\n            &lt;input type=\"text\" name=\"age\" value=\"&lt;?php echo $age; ?&gt;\"&gt;\n            &lt;br&gt;\n            Email:&lt;br&gt;\n            &lt;input type=\"email\" name=\"email\" value=\"&lt;?php echo $email; ?&gt;\"&gt;\n            &lt;br&gt;&lt;br&gt;\n            &lt;input type=\"submit\" value=\"Update\" name=\"update\"&gt;\n          &lt;\/fieldset&gt;\n        &lt;\/form&gt;\n        &lt;\/body&gt;\n        &lt;\/html&gt;\n\n\n    &lt;?php\n    } else{\n        header('Location: view-student.php');\n    }\n}\n?&gt;<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.c-sharpcorner.com\/article\/crud-operation-in-php-using-mysql\/Images\/img7.jpg\" alt=\"CRUD Operation in PHP using MySQL\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">How to Delete Records?<\/h2>\n\n\n\n<p>The name of the file is&nbsp;<strong>delete-student.php.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u200b&lt;?php\ninclude \"dbconfig.php\";\nif (isset($_GET&#91;'id'])) {\n    $stu_id = $_GET&#91;'id'];\n    $sql = \"DELETE FROM students WHERE id ='$stu_id'\";\n     $result = $conn-&gt;query($sql);\n     if ($result == TRUE) {\n        echo \"Record deleted successfully.\";\n        header('Location: view-student.php');\n    }else{\n        echo \"Error:\" . $sql . \"&lt;br&gt;\" . $conn-&gt;error;\n    }\n}\n?&gt;<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n","protected":false},"excerpt":{"rendered":"<p>Create database and tables in MySQL using&hellip;<\/p>\n","protected":false},"author":1,"featured_media":5129,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[133,75],"tags":[570,134],"class_list":["post-5127","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","category-web-design","tag-crud","tag-php"],"_links":{"self":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts\/5127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/comments?post=5127"}],"version-history":[{"count":3,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts\/5127\/revisions"}],"predecessor-version":[{"id":5134,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts\/5127\/revisions\/5134"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/media\/5129"}],"wp:attachment":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/media?parent=5127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/categories?post=5127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/tags?post=5127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}