Skip to content

changed a lot of things #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: php-include
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 105 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,61 +1,144 @@
# php-crud
this is a helper for php crud operation.

<hr>


# How to Use

<img src="https://imgbbb.com/images/2019/04/14/Screenshot-56.png" alt="folder sceenshot" />
<img src="https://imgbbb.com/images/2019/04/14/Screenshot-56.png" alt="folder screenshot" />

Create a new file `index.php`

Then, open `index.php` & `config.php` in your text editor.

create a new file `index.php`
In `config.php`, you need to set up the database connection first:

then , open `index.php` & `config.php` in your text editor .
```php
define("DB_HOST", "localhost"); // your host name
define("DB_USER", "your_username"); // your database username
define("DB_PASS", "your_password"); // your database password
define("DB_NAME", "your_database_name"); // your database name

In `config.php` , You need to setup Database connection as first.
// Message Settings
define('insertSuccess', 'Insert Successfully');
define('updateSuccess', 'Update Successfully');
define('deleteSuccess', 'Deleted Successfully');

then `index.php`,

put these at the top ,
In index.php, include the necessary files at the top:

``` include 'database/index.php';```
include 'db_connection.php';
include 'config.php';

# For Insert :
```php
`DB::insert($table, $data);
````
`$table` = its your table name,

`DB::insert($table, $data);`
`$data`: An associative array where the key will be the column name of your table.

`$table` = its your table name,
### Example :
````php
$table = 'users';
$data = [
'name' => 'John Doe',
'email' => 'john@example.com'
];
DB::insert($table, $data);
````

# For selectAll :
````php
DB::select($table);
````
`$table`: The name of your table.

### Example :

````php
$results = DB::selectAll($table);

foreach ($results as $row) {
echo $row['name'];
}
````

`$data` = A array ,where key will be the column of your table.
# For Select with Conditions :

# For select :
````php
$conditions = ['id' => 1, 'status' => 'active'];

$results = DB::selectWithConditions($table, $conditions);

foreach ($results as $row) {
echo $row['name'];
}
````
`$table`: The name of your table.
`$conditions`: An associative array of column names and values for the WHERE clause.

`DB::select($table);`

### Example :

```bash
$results = DB::select($table);
````php

while ($rows = $results->fetch_assoc()) {
$table = 'users';
$conditions = ['status' => 'active'];
$results = DB::selectWithConditions($table, $conditions);

echo $rows['name'];
foreach ($results as $row) {
echo $row['name'];
}
```
````



# For Update :

`DB::update($table, $data, $id);`
````php
$data = ['name' => 'New Name'];
$conditions = ['id' => 1];
DB::update($table, $data, $conditions);
````

# For Delete :
`$table`: The name of your table.
`$data`: An associative array where the key will be the column name of your table.
`$conditions`: An associative array of column names and values for the WHERE clause

`DB::delete($table, $id);`
### Example:
````php
$table = 'users';
$data = ['name' => 'Jane Doe'];
$conditions = ['id' => 1];
DB::update($table, $data, $conditions);
````

---

# For Delete :
````php
$conditions = ['id' => 1];
DB::delete($table, $conditions);
````
`$table`: The name of your table.
`$conditions`: An associative array of column names and values for the WHERE clause.


### Example
```php
$table = 'users';
$conditions = ['id' => 1];
DB::delete($table, $conditions);
```

### For more Links

- [GitHub](https://github.com/IANirab/)
- [GitHub](https://github.com/Ok9xNirab/)
- [Facebook](https://web.facebook.com/istiaq.nirab.1)

**Enjoy!**

### Few Notes after the update
all `$conditions` are supposed to be a key value pairs where keys are column names and values are values of those columns

2 changes: 2 additions & 0 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
define('insertSuccess', 'Insert Successfully');
define('updateSuccess', 'Update Successfully');
define('deleteSuccess', 'Deleted Successfully');

//suggest defining the tables here also as constants to avoid typos
21 changes: 13 additions & 8 deletions database/db_connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,28 @@ class dbConnection
private $user;
private $pass;
private $dbname;
private $conn;

public function __construct($host, $user, $pass, $dbname)
{
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->dbname = $dbname;


try {
$dsn = "mysql:host=$this->host;dbname=$this->dbname";
$this->conn = new PDO($dsn, $this->user, $this->pass);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "Database Not Connected: " . $e->getMessage();
}
}

public function connection()
public function getConnection()
{
// Create connection
$conn = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
// Check connection
if ($conn->connect_error) {
echo "Database Not Connected !!";
}
return $conn;
return $this->conn;
}
}
?>
41 changes: 35 additions & 6 deletions database/delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,47 @@ class delete
{
private $conn;
private $table;

/**
* Constructor to initialize connection and table.
*
* @param PDO $conn The PDO connection object.
* @param string $table The name of the table.
*/
public function __construct($conn, $table)
{
$this->conn = $conn;
$this->table = $table;
}
public function deleteData($id)

/**
* Deletes records from the table based on the provided conditions.
*
* @param array $conditions An associative array of column names and values for the WHERE clause.
* @return string|null The success message or null if an error occurs.
*/
public function deleteData($conditions)
{
$delete = "DELETE FROM " . $this->table . " WHERE id='$id'";
$result = $this->conn->query($delete);
$this->conn->close();
if ($result != null) {
return $dbname = deleteSuccess;
$whereClause = "";
$params = [];

foreach ($conditions as $column => $value) {
$whereClause .= "$column = :$column AND ";
$params[$column] = $value;
}

// Remove the trailing ' AND '
$whereClause = rtrim($whereClause, " AND ");

$stmt = $this->conn->prepare("DELETE FROM $this->table WHERE $whereClause");

try {
$stmt->execute($params);
return deleteSuccess;
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
return null;
}
}
}
?>
Loading