Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
pepiuox committed Aug 27, 2020
1 parent 8135f29 commit 839b5a3
Show file tree
Hide file tree
Showing 14 changed files with 1,343 additions and 357 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/nbproject/private/
511 changes: 511 additions & 0 deletions EasyCRUD.php

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# EasyCRUD
<p>This is an easy CRUD, essential for those who are learning MySql and PHP. practical for the creation of forms and lists of data visualization</p>
<p>Add or change the configuration in the file db.php</p>
<p>Now select any table in your database, and you can now view, add, edit or delete the contents.</p>
27 changes: 27 additions & 0 deletions announcement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
//This is temporal file only for add new row in announcement
if (isset($_POST['addrow'])) {
$Is_Active = $_POST["Is_Active"];
$Topic = $_POST["Topic"];
$Message = $_POST["Message"];
$Date_LastUpdate = $_POST["Date_LastUpdate"];
$Language = $_POST["Language"];
$Auto_Publish = $_POST["Auto_Publish"];
$Date_Start = $_POST["Date_Start"];
$Date_End = $_POST["Date_End"];
$Date_Created = $_POST["Date_Created"];
$Created_By = $_POST["Created_By"];
$Translated_ID = $_POST["Translated_ID"];

if (!empty($Is_Active) && !empty($Topic) && !empty($Message) && !empty($Date_LastUpdate) && !empty($Language) && !empty($Auto_Publish) && !empty($Date_Start) && !empty($Date_End) && !empty($Date_Created) && !empty($Created_By) && !empty($Translated_ID)) {
$query = "INSERT INTO `$tble`(`Is_Active` , `Topic` , `Message` , `Date_LastUpdate` , `Language` , `Auto_Publish` , `Date_Start` , `Date_End` , `Date_Created` , `Created_By` , `Translated_ID`) VALUES ('$Is_Active' , '$Topic' , '$Message' , '$Date_LastUpdate' , '$Language' , '$Auto_Publish' , '$Date_Start' , '$Date_End' , '$Date_Created' , '$Created_By' , '$Translated_ID')";
if ($link->query($query) == TRUE) {
echo "Record added successfully";
} else {
echo "Error added record: " . $link->error;
}
unlink("announcement.php");

}
}
?>
25 changes: 25 additions & 0 deletions balance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
//This is temporal file only for add new row in balance
if (isset($_POST['addrow'])) {
$user_id = $_POST["user_id"];
$currency = $_POST["currency"];
$balance = $_POST["balance"];
$awaiting_deposit = $_POST["awaiting_deposit"];
$reserved_in_orders = $_POST["reserved_in_orders"];
$est_USD = $_POST["est_USD"];
$deposit = $_POST["deposit"];
$withdrawal = $_POST["withdrawal"];
$history = $_POST["history"];

if (!empty($user_id) && !empty($currency) && !empty($balance) && !empty($awaiting_deposit) && !empty($reserved_in_orders) && !empty($est_USD) && !empty($deposit) && !empty($withdrawal) && !empty($history)) {
$query = "INSERT INTO `$tble`(`user_id` , `currency` , `balance` , `awaiting_deposit` , `reserved_in_orders` , `est_USD` , `deposit` , `withdrawal` , `history`) VALUES ('$user_id' , '$currency' , '$balance' , '$awaiting_deposit' , '$reserved_in_orders' , '$est_USD' , '$deposit' , '$withdrawal' , '$history')";
if ($link->query($query) == TRUE) {
echo "Record added successfully";
} else {
echo "Error added record: " . $link->error;
}
unlink("balance.php");

}
}
?>
36 changes: 36 additions & 0 deletions checkconf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

include 'db.php';
if (isset($_POST['check'])) {
$check = $_POST['check'];
if ($check == 1) {
$conf = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);

/* If connection fails for some reason */
if ($conf->connect_error) {
echo '<div class="alert alert-danger" role="alert">';
echo '<h3>Error, Database connection failed</h3>
<h4>Please: first create your DB name in your database</h4>
</div>';
echo '<hr>';
echo '<div class="alert alert-primary" role="alert">
<h3>Now click the buttom</h3>';
echo '<form><h3>Install you tables for your db</h3>';
echo '<input type="submit" name="install" class="btn btn-primary" value="Install">';
echo '</form>';
echo '</div>';
exit();
} else {
echo "<script>
window.setTimeout(function() {
window.location.href = 'index.php?view=select';
}, 3000);
</script>";
echo '<meta http-equiv="refresh" content="3; url=list.php" />';
echo '<div class="alert alert-primary" role="alert">'
. '<h2>Your DB is connected to ' . DBNAME . '</h2>';
echo '</div>';
}
}
}
?>
225 changes: 225 additions & 0 deletions config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
<?php
$file = 'db.php';
$nclose = '';
$folder = basename(dirname(__FILE__));
if (isset($_POST['submit'])) {

$handle = fopen($file, 'w') or die('Cannot open file: ' . $file);
$actual = file_get_contents($file);

$db_host = $_POST['host'];
$db_user = $_POST['user'];
$db_password = $_POST['password'];
$db_name = $_POST['dbname'];
$createdb = $_POST['cdbn'];
if ($createdb === 'yes') {

$mkdb = new mysqli($db_host, $db_user, $db_password);
// Check connection
if ($mkdb->connect_error) {
die("Connection failed: " . $mkdb->connect_error);
}

// Create database
$sql = "CREATE DATABASE " . $db_name;
if ($mkdb->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $mkdb->error;
}

$mkdb->close();
// Name of the file
$filename = 'sql/sql.sql';
$mktbs = new mysqli($db_host, $db_user, $db_password, $db_name);

// Check connection
if ($mktbs->connect_errno) {
echo "Failed to connect to MySQL: " . $mktbs->connect_errno;
echo "<br/>Error: " . $mktbs->connect_error;
}

// Temporary variable, used to store current query
$templine = '';
// Read in entire file
$lines = file($filename);
// Loop through each line
foreach ($lines as $line) {
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || $line == '') {
continue;
}
// Add this line to the current segment
$templine .= $line;
// If it has a semicolon at the end, it's the end of the query
if (substr(trim($line), -1, 1) == ';') {
// Perform the query
$mktbs->query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . $mktbs->error() . '<br /><br />');
// Reset temp variable to empty
$templine = '';
}
}
echo "Tables imported successfully";
$mktbs->close();
}
$filecontent = '';
$filecontent .= '<?php' . "\n\n";
$filecontent .= "define('DBHOST', '" . $db_host . "');" . "\n";
$filecontent .= "define('DBUSER', '" . $db_user . "');" . "\n";
$filecontent .= "define('DBPASS', '" . $db_password . "');" . "\n";
$filecontent .= "define('DBNAME', '" . $db_name . "');" . "\n";
$filecontent .= '$link = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);' . "\n";
$filecontent .= "
/* If connection fails for some reason */
if (\$link->connect_error) {
die('Error, Database connection failed: (' . \$link->connect_errno . ') ' . \$link->connect_error);
}" . "\n";
if (!empty($folder)) {
$filecontent .= "\$base = 'http://'.\$_SERVER['HTTP_HOST'].'/" . $folder . "/';" . "\n";
} else {
$filecontent .= "\$base = 'http://'.\$_SERVER['HTTP_HOST'].'/" . "\n";
}

$filecontent .= "require 'EasyCRUD.php';
?>
";
file_put_contents($file, $filecontent);
header('Location: index.php?view=select');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<title>Easy CRUD</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>

</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12 py-4"><h2>Easy CRUD</h2>
<p>The simple and practical tool to edit your data.
Create, update and delete without the need to create forms.</p>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-12 py-4">
<div id="resp"></div>
<?php
if (file_exists($file)) {
?>
<div class="modal fade in" id="myModal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header" >
<h5 class="modal-title">File already exists</h5> <button type="button" class="close" data-dismiss="modal"> <span>×</span> </button>
</div>
<div class="modal-body">
<p>The configuration file already exists.</p>
<button class="btn btn-primary" name="edit" id="edit">Edit DB config file</button>
<button class="btn btn-secondary" name="check" id="check">Check DB connection</button>
<button class="btn btn-info" name="install" id="install">Install tables</button>
</div>
<div class="modal-footer"> <a href="index.php?view=select" class="btn btn-primary" name="edit" id="edit">Go to page list</a> <button type="button" name="close" id="close" class="btn btn-secondary" data-dismiss="modal">Close</button> </div>
</div>
</div>
</div>
<?php } else {
?>
<form method="post">
<div class="form-group row">
<div class="col-8">
<h2> Save your setting for DB</h2>
</div>
</div>
<div class="form-group row">
<label for="host" class="col-4 col-form-label">Database Host</label>
<div class="col-8">
<input id="host" name="host" type="text" class="form-control">
</div>
</div>
<div class="form-group row">
<label for="user" class="col-4 col-form-label">Database Username</label>
<div class="col-8">
<input id="user" name="user" type="text" class="form-control">
</div>
</div>
<div class="form-group row">
<label for="password" class="col-4 col-form-label">Database Password</label>
<div class="col-8">
<input id="password" name="password" type="text" class="form-control">
</div>
</div>
<div class="form-group row">
<label for="dbname" class="col-4 col-form-label">Database Name</label>
<div class="col-8">
<input id="dbname" name="dbname" type="text" class="form-control">
</div>
</div>
<hr>
<h5>This option creates the database with the tables</h5>
<div class="form-group row">
<label for="dbname" class="col-4 col-form-label">You have a database or do you need create one</label>
<div class="col-8">
<input id="cdbn" name="cdbn" type="checkbox" value="yes" class="form-control mx-2">
</div>
</div>
<div class="form-group row">
<div class="offset-4 col-8">
<button name="submit" type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
<?php } ?>
</div>
</div>
</div>


<script type="text/javascript">
$(window).on('load', function () {
$('#myModal').modal('show');
});
$('#edit').click(function () {
$('#myModal').modal('toggle');
var edit = 1;
$.ajax({
type: 'POST',
url: 'editconf.php',
data: {edit: edit}
}).done(function (rsp) {
$('#resp').html(rsp);
});
});
$('#check').click(function () {
$('#myModal').modal('toggle');
var check = 1;
$.ajax({
type: 'POST',
url: 'checkconf.php',
data: {check: check}
}).done(function (rsp) {
$('#resp').html(rsp);
});
});
$('#install').click(function () {
var install = 1;
$.ajax({
type: 'POST',
url: 'installtables.php',
data: {install: install}
}).done(function (rsp) {
$('#resp').html(rsp);
});
});
</script>
</body>
</html>
49 changes: 12 additions & 37 deletions db.php
Original file line number Diff line number Diff line change
@@ -1,42 +1,17 @@
<?php

define('DBHOST', ''); // Add your host
define('DBUSER', ''); // Add your username
define('DBPASS', ''); // Add your password
define('DBNAME', ''); // Add your database name
//MySQLi Object / Procedural
define('DBHOST', 'localhost');
define('DBUSER', 'user');
define('DBPASS', 'password');
define('DBNAME', 'database_name');
$link = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);
if (!$link) {
die('Error: Could not connect: ' . mysqli_error());
}
//PDO Object / Procedural
class DB {

protected $conn = null;

public function Connect() {
try {
$hostDB = DBHOST;
$baseDB = DBNAME;
$userDB = DBUSER;
$passDB = DBPASS;
$charset = 'utf8';
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$dsn = "mysql:host=$hostDB;dbname=$baseDB;charset=$charset";
$this->conn = new PDO($dsn, $userDB, $passDB, $options);
return $this->conn;
} catch (PDOException $e) {
echo 'Connection error: ' . $e->getMessage();
}
}

public function Close() {
$this->conn = null;
/* If connection fails for some reason */
if ($link->connect_error) {
die('Error, Database connection failed: (' . $link->connect_errno . ') ' . $link->connect_error);
}
}

?>
$base = 'http://'.$_SERVER['HTTP_HOST'].'/EasyCRUD/';
require 'EasyCRUD.php';

?>

Loading

0 comments on commit 839b5a3

Please sign in to comment.