Skip to content

Instantly share code, notes, and snippets.

@INDIAN2020
Created May 3, 2013 09:08
Show Gist options
  • Select an option

  • Save INDIAN2020/5508106 to your computer and use it in GitHub Desktop.

Select an option

Save INDIAN2020/5508106 to your computer and use it in GitHub Desktop.
crud
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<?php
//defining the values
$isValid = false;
$myname = $_POST['fullname'];
$hiddenID = $_POST['hiddenID'];
//database connection
$dbc = mysqli('localhost','','','');
// did user press the submit button
if (isset($_POST['submit'])) {
if (strlen($myname) <= 3) {
echo "You can not be that cool!!<br />";
}
} else {
if (empty($hiddenID)) {
$isValid = true;
//do what is SAVE NAME
//table=names
$insert = "insert into names(personname) values ($myname)";
$results = mysqli_query($dbc, $insert);
echo "Done<br />";
$myname = ''; //make the input field empty after the query execution
}
else {
//update
$update = "update names set personname='$myname' where id='$hiddenID'";
mysqli_query($dbc, $update);
}
}
//show all names from the database
$myQuery = "select * from names orderby id";
$results = mysqli_query($dbc, $myQuery);
if (!$isValid) {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="firstname">Enter Name: </label>
<input type="text" id="fullname" name="fullname" value="<?php echo $myname; ?>" />
<input type="hidden" name="hiddenID" value="<?php echo $idtoedit;?>" />
<input type="submit" name="submit" value="Save" />
</form>
<?php
}
//show the data READ
while($row = mysqli_fetch_array($results)){
echo "<p> ID: ".$row['id']." ".$row['personname']."<a href=\"".$_SERVER['PHP_SELF']."?idtodelete=".$row['id']."\">DELETE</a>"." "."<a href=\"".$_SERVER['PHP_SELF']."?idtoedit=".$row['id']."\">EDIT</a>";
}
mysqli_close($dbc);
?>
</body>
</html>
@chriscastanares
Copy link

thanks man :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment