when I want to do an update, the data is not updated, what should i do?
What are u write in the route ?
okaay…
{
private $config = array(
“dbdriver” => “mysql”,
“dbhost” => “127.0.0.1”,
“dbuser” => “root”,
“dbpass” => “root”,
“dbname” => “wifidabba”
);
public function __construct()
{
$dbhost = $this->config[‘dbhost’];
$dbuser = $this->config[‘dbuser’];
$dbpass = $this->config[‘dbpass’];
$dbname = $this->config[‘dbname’];
# $sqlitedb = $this->config[‘sqlitedb’];
$options = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
);
try {
switch ($this->config[“dbdriver”]) {
case “sqlite”:
$conn = “sqlite:{$sqlitedb}”;
break;
case “mysql”:
$conn = “mysql:host={$dbhost};dbname={$dbname}”;
break;
default:
echo “Unsuportted DB Driver! Check the configuration.”;
exit(1);
}
$this->db = new PDO($conn, $dbuser, $dbpass, $options);
} catch (PDOException $e) {
echo $e->getMessage();
exit(1);
}
}
public function run($sql, $bind=array())
{
$sql = trim($sql);
try {
$result = $this->db->prepare($sql);
$result->execute($bind);
return $result;
} catch (PDOException $e) {
echo $e->getMessage();
exit(1);
}
}
{
$fields = $this->filter($table, $data);
$sql = “INSERT INTO ” . $table . ” (” . implode($fields, “, “) . “) VALUES (:” . implode($fields, “, :”) . “);”;
$bind = array();
foreach ($fields as $field) {
$bind[“:$field”] = $data[$field];
}
$result = $this->run($sql, $bind);
return $this->db->lastInsertId();
}
public function get($table, $where=””, $bind=array(), $fields=”*”)
{
$sql = “SELECT ” . $fields . ” FROM ” . $table;
if (!empty($where)) {
$sql .= ” WHERE ” . $where;
}
$sql .= “;”;
$result = $this->run($sql, $bind);
$rows = array();
while ($row = $result->fetch()) {
$rows[] = $row;
}
return $rows;
}
public function getFirst($table, $where=””, $bind=array(), $fields=”*”)
{
$sql = “SELECT ” . $fields . ” FROM ” . $table;
if (!empty($where)) {
$sql .= ” WHERE ” . $where;
}
$sql .= “;”;
$result = $this->run($sql, $bind);
return $result->fetch();
}
public function update($table, $data, $where, $bind=array())
{
$fields = $this->filter($table, $data);
$fieldSize = sizeof($fields);
$sql = “UPDATE ” . $table . ” SET “;
for ($f = 0; $f < $fieldSize; ++$f) {
if ($f > 0) {
$sql .= “, “;
}
$sql .= $fields[$f] . ” = :update_” . $fields[$f];
}
$sql .= ” WHERE ” . $where . “;”;
foreach ($fields as $field) {
$bind[“:update_$field”] = $data[$field];
}
$result = $this->run($sql, $bind);
return $result->rowCount();
}
public function delete($table, $where, $bind=””)
{
$sql = “DELETE FROM ” . $table . ” WHERE ” . $where . “;”;
$result = $this->run($sql, $bind);
return $result->rowCount();
}
private function filter($table, $data)
{
$driver = $this->config[‘dbdriver’];
if ($driver == ‘sqlite’) {
$sql = “PRAGMA table_info(‘” . $table . “‘);”;
$key = “name”;
} elseif ($driver == ‘mysql’) {
$sql = “DESCRIBE ” . $table . “;”;
$key = “Field”;
} else {
$sql = “SELECT column_name FROM information_schema.columns WHERE table_name = ‘” . $table . “‘;”;
$key = “column_name”;
}
if (false !== ($list = $this->run($sql))) {
$fields = array();
foreach ($list as $record) {
$fields[] = $record[$key];
}
return array_values(array_intersect($fields, array_keys($data)));
}
return array();
}
}
This is the whole DB wrapper class for plain PHP
<?php
ini_set(‘display_errors’, 1);
ini_set(‘display_startup_errors’, 1);
error_reporting(E_ALL|E_STRICT);
require_once ‘db.php’;
$db = new db();
pr(
$db->update(‘ip_events’, [‘mac_address’ => ‘mac1’], ‘id = :id’, [‘:id’ => 1])
);
Thanks, it looks clean and professionally structured. Thank you once more for the help
Okay, sure I will.
I strongly recommend that you dont write the sql queries yourself
We focuses on technology, design, strategy, & support. We have been digital experiences for over + years and we believe in building comprehensive solutions that adapt to real world business situations.
okay… how is supposed to be done?
its not really supposed to or not supposed to
ooh, i get what you mean
require(“db.php”);
$fields = ”;
$values = ”;
foreach ($_POST as $key=>$value ) {
if($fields != ”){
$fields = $fields.’, ‘.$key.”;
$values = $values.”, ‘”.addslashes($value).”‘”;
}else{
$fields = $fields.””.$key.””;
$values = $values.”‘”.addslashes($value).”‘”;
}
}
$statment = “INSERT INTO $tbl_name($fields) VALUES ($values)”;
/* echo $statment;
*/
if($query=$con->query($statment)){
return true;
}else{
//for debugging purposes
echo $statment;
}
Okay, thanks, let try to implement it
lastly, when implmeneting your libraries, dont forget about security issues such as sql injection and xss attacks
If you don’t reload the page, i get “unauthenticated” when i hit the logout route
This is my file:
view : https://pastebin.com/yLsWfh7R
route: https://pastebin.com/HLVC9rJ2
controller : https://pastebin.com/WyZvU9rw
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
This is my file:
view : https://pastebin.com/yLsWfh7R
route: https://pastebin.com/HLVC9rJ2
controller : https://pastebin.com/WyZvU9rw
solve guys, i’m just put method save() before return
why?
Because it show blank white page…
@foreach($menu->getChild as $submenu)
@foreach($submenu->knowledgeRooms as $knowledgeRoom)
{{ dd($knowledgeRoom) }}
@endforeach
@endforeach
@endforeach
@foreach($menu->getChild as $submenu)
@foreach($submenu->knowledgeRooms as $knowledgeRoom)
{{ dd($knowledgeRoom) }}
@endforeach
@endforeach
@endforeach
my blade
{
Schema::defaultStringLength(191);
view()->composer(‘*’, function($view) {
$view->with(‘menus’, Category::with(‘knowledge_rooms’)->whereNull(‘parent_id’)->get());
});
}
public function up()
{
Schema::create(‘categories’, function (Blueprint $table) {
$table->bigIncrements(‘id’);
$table->string(‘name_fa’);
$table->string(‘name_en’);
$table->integer(‘parent_id’);
$table->string(‘icon’)->nullable();
$table->timestamps();
});
}
knowledge_rooms table
public function up()
{
Schema::create(‘knowledge_rooms’, function (Blueprint $table) {
$table->bigIncrements(‘id’);
$table->string(‘title’);
$table->string(‘lang’);
$table->text(‘body’);
$table->string(‘image’);
$table->string(‘slug’);
$table->bigInteger(‘user_id’)->unsigned();
$table->foreign(‘user_id’)->references(‘id’)->on(‘users’)->onDelete(‘cascade’);
$table->timestamps();
});
Schema::create(‘category_knowledge_room’, function (Blueprint $table) {
$table->bigInteger(‘category_id’)->unsigned();
$table->bigInteger(‘knowledge_room_id’)->unsigned();
$table->primary([‘category_id’, ‘knowledge_room_id’]);
$table->foreign(‘category_id’)->references(‘id’)->on(‘categories’)->onDelete(‘cascade’);
$table->foreign(‘knowledge_room_id’)->references(‘id’)->on(‘knowledge_rooms’)->onDelete(‘cascade’);
});
}
AppServiceProvider.php
public function register()
{
Schema::defaultStringLength(191);
view()->composer(‘*’, function($view) {
$view->with(‘menus’, Category::whereParent_id(‘0’)->get());
});
}
Category.php
public function knowledgeRooms()
{
return $this->belongsToMany(KnowledgeRoom::class);
}
master.blade.php
@foreach($menus as $menu)
@foreach($menu->getChild as $submenu)
@foreach($submenu->knowledgeRooms as $knowledgeRoom)
{{ dd($knowledgeRoom) }}
@endforeach
@endforeach
@endforeach
web.php
Route::view(‘/’, ‘Home.master’)->name(‘index’);

What are u write in the route ?!
Route::view(‘/’, ‘Home.master’)->name(‘index’);