Use this code
<?php
if(isset($_REQUEST['submit_btn']))
{
echo "<div>";
$name = $_POST["names"];
echo "</div>";
$ins="insert into tablename (fieldname)values('$name') ";
$conn->query($ins);
?>
<script>
alert('inserted successfully');
</script>
<?php
$sel="select * from tablename";
$r=$conn->query($sel);
while($u=$r->fetch_object())
{
echo $u->fieldname;
}
}
?>
<form action="" method="POST">
<input type="text" name="names" id="names">
<input type="submit" value="submit" name="submit_btn">
</form>
Hope this will help you