Wednesday, April 4, 2018

Select Insert Update Delete In Php MySQL Example - MySQL


following are the database and table design


Select-Insert-Update-Delete-In-Php-MySQL-Example - MySQL


<?php

$con=mysqli_connect('localhost','root','','my_db');

if(isset($_POST['insert']))

{

extract($_POST);

$insert="insert into stud(no,name,city) values('".$no."','".$name."','".$city."')";

$insert_data=mysqli_query($con,$insert);

if($insert_data==true)

{

echo "recored added";

}

else

{

echo "recored not added";

}

}

$select="select * from stud";

$select_data=mysqli_query($con,$select);

if(isset($_GET['delete']))

{

$delete= "delete from stud where id=".$_GET['delete'];

$delete_data=mysqli_query($con,$delete);

if($delete_data==true)

{

echo "recored deleted";

}

else

{

echo "recored not deleted";

}

}

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

{

$view = mysqli_fetch_array(mysqli_query($con,'select * from stud where id='.$_GET['edit']));

}

if(isset($_POST['update']) && isset($_GET['edit']))

{

$update_query = 'update stud set no="'.$_POST['no'].'",name="'.$_POST['name'].'",city="'.$_POST['city'].'"  where id='.$_GET['edit'];

//echo $update_query;

if(mysqli_query($con,$update_query))

{

echo "recored updated";

}

else

{

echo mysqli_error($con);

exit();

}

}

mysqli_close($con);

?>

    <!DOCTYPE html>

    <html>

    <head>

        <title>bhavesh</title>

    </head>

    <body>

        <form action="" method="post">

            <div>No:
                <input type="number" name="no" value="<?php echo(isset($_GET['edit']) && isset($view['no']))? $view['no'] : ''; ?>">
            </div>

            <div>Name:
                <input type="text" name="name" value="<?php echo(isset($_GET['edit']) && isset($view['name']))? $view['name'] : ''; ?>">
            </div>

            <div>city:
                <input type="text" name="city" value="<?php echo(isset($_GET['edit']) && isset($view['city']))? $view['city'] : ''; ?>">
            </div>

            <div>
                <input type="submit" name="<?php echo (isset($_GET['edit']) && isset($view['no'])) ? 'update' : 'insert'; ?>" value="<?php echo (isset($_GET['edit']) && isset($view['no'])) ? 'update' : 'insert'; ?>">

                <input type="reset" name="select" value="RESET">
            </div>

        </form>

        <div>

            <table>

                <tr>
                    <th>no</th>
                    <th>name</th>
                    <th>city</th>
                </tr>

                <?php while ($row= mysqli_fetch_assoc($select_data)) {

?>

                    <tr>
                        <td>
                            <?php echo $row['no'] ?>
                        </td>

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

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

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

                            <a href="insert.php?delete=<?php echo $row['id'];?>">Delete</a></td>

                    </tr>

                    <?php }?>

            </table>

        </div>

    </body>

    </html>


Following are the output of the above program.



Select-Insert-Update-Delete-In-Php-MySQL-Example - MySQL

No comments:

Post a Comment

Generate Even Numbers in a Range In PHP.

$start = 1; $end = 20; for ($i = $start; $i <= $end; $i++) {     if ($i % 2 == 0) {         echo $i . " is even.<br>";   ...