23 thoughts on “PHP CRUD (Create, Update, Delete) with MySQL on a single page

  1. Parse error: syntax error, unexpected end of file in C:xampphtdocsphp_crudindex.php on line 47


  2. My save button when I insert data is not doing anything but it displays "Address saved" in green at the top but it just does not show the info inserted after and it does not tell me i have any problems at the top either… please help.

    this is my code of my index.php
    <?php include('server.php'); ?>


    if (isset($_GET['edit'])) {

    $id = $_GET['edit'];

    $update = true;

    $record = mysqli_query($db, "SELECT * FROM info WHERE id=$id");

    if (count($record) == 1 ) {

    $n = mysqli_fetch_array($record);

    $name = $n['name'];

    $address = $n['address'];




    <!DOCTYPE html>



    <title>CRUD: CReate, Update, Delete PHP MySQL</title>

    <link rel="stylesheet" type="text/css" href="style.css">



    <?php if (isset($_SESSION['message'])): ?>

    <div class="msg">


    echo $_SESSION['message'];




    <?php endif ?>

    <?php $results = mysqli_query($db, "SELECT * FROM info"); ?>






    <th colspan="2">Action</th>



    <?php while ($row = mysqli_fetch_array($results)) { ?>


    <td><?php echo $row['name']; ?></td>

    <td><?php echo $row['address']; ?></td>


    <a href="index.php?edit=<?php echo $row['id']; ?>" class="edit_btn" >Edit</a>



    <a href="server.php?del=<?php echo $row['id']; ?>" class="del_btn">Delete</a>



    <?php } ?>


    <form method="post" action="server.php" >

    <input type="hidden" name="id" value="<?php echo $id; ?>">

    <div class="input-group">


    <input type="text" name="name" value="<?php echo $name; ?>">


    <div class="input-group">


    <input type="text" name="address" value="<?php echo $address; ?>">


    <div class="input-group">

    <?php if ($update == true): ?>

    <button class="btn" type="submit" name="update" style="background: #556B2F;" >update</button>

    <?php else: ?>

    <button class="btn" type="submit" name="save" >Save</button>

    <?php endif ?>





    this is my code for server.php


    $db = mysqli_connect('localhost', 'root', ' ', 'test');

    // initialize variables

    $name = "";

    $address = "";

    $id = 0;

    $update = false;

    if (isset($_POST['save'])) {

    $name = $_POST['name'];

    $address = $_POST['address'];

    mysqli_query($db, "INSERT INTO info (name, address) VALUES ('$name', '$address')");

    $_SESSION['message'] = "Address saved";

    header('location: index.php');


    // update records

    if (isset($_POST['update'])) {

    $id = $_POST['id'];

    $name = $_POST['name'];

    $address = $_POST['address'];

    mysqli_query($db, "UPDATE info SET name='$name', address='$address' WHERE id=$id");

    $_SESSION['message'] = "Address updated!";

    header('location: index.php');


    // retreive records

    if (isset($_GET['del'])) {

    $id = $_GET['del'];

    mysqli_query($db, "DELETE FROM info WHERE id=$id");

    $_SESSION['message'] = "Address deleted!";

    header('location: index.php');

  3. If anyone is having issues with the edited entry not being edited but you get the successfully updated message like it is, just take out the mysqli_real_escape_string() function. That's what worked for me.

    Also, this code and the code on his website is not the same. I learned that the hard way yesterday. Also, I'm fairly certain that the code on the website needs updated because it didn't work for me.

    Here is the code from the video that helped me create this project for school. https://drive.google.com/drive/folders/1jOkadHa-e90VYhFAH5KeToDv-J90M-ry?usp=sharing
    my database name was: vaycayplus
    my table name was: bookings

    I used this for a Senior Capstone Project. Thanks to Awa Melvine for making this video. Hopefully my version of the code here will help somebody out.

  4. i'm having syntax error, unexpected '$results' (T_VARIABLE) in C:xampphtdocsphp_crudserver.php on line 34
    how do i solve this???

  5. Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, bool given in /opt/lampp/htdocs/PhpCrud/index.php on line 65 Please help

  6. PHP Code Builder http://www.phpcodebuilder.com/ is a Crud generator website for auto generating CRUD(Create Read Update and Delete) code in PHP for FREE by just filling forms.

    You can generate crud with all popular frameworks like :-

    Cake PHP
    Core PHP

    View it in action at :- https://youtu.be/07mORL71gPo

  7. Hi, i keep receiving an error that states " unidentified index: id ". The error states the code is <a href="studentsPage.php?edit=<?php echo $row['id']; ?>" class="edit_btn" >Edit</a> was wondering if you could help. Thank u

  8. i know this question may sound stupid but… how… how did you copy code from the html file and copied it to the css file and it was all set just by copying it?

    Once again, I know it may sound stupid but… how? 😵

  9. Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in line 18 << help me>>

  10. i wna minor updates if rows have same data then delete option shows on both rows
    if (rany row==any row){
    then delete option will show both rows and delete those rows
    kindly rlp yyyy mee about thissss

  11. it didn't help me at all, I am using wamp no connection between wamp and database…. so no like, no subscription…. good day!

Leave a Reply

Your email address will not be published. Required fields are marked *