When I want to do an update, the data is not updated, what should i do?

|
Cleo_hacker 2020-07-19 11:09:03
Wow! thank you @channaveer_hakari Let me implement it and i will be back we discuss more of your product DISQUS
Cleo_hacker 2020-07-19 11:09:14
flyingdragons 2020-07-19 11:08:22
You can use the following Database wrapper class

okaay…

flyingdragons 2020-07-19 11:09:31
Its bit long. I will send you personally
flyingdragons 2020-07-19 11:09:53
class db
{
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);
}
}

Cleo_hacker 2020-07-19 11:10:06
Okay, no problem
flyingdragons 2020-07-19 11:10:06
public function create($table, $data)
{
$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();
}
}

flyingdragons 2020-07-19 11:10:25
Cleo_hacker 2020-07-19 11:10:06
Okay, no problem

This is the whole DB wrapper class for plain PHP

flyingdragons 2020-07-19 11:10:56
You can use it as

<?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])
);

Cleo_hacker 2020-07-19 11:12:01
flyingdragons 2020-07-19 11:10:25
This is the whole DB wrapper class for plain PHP

Thanks, it looks clean and professionally structured. Thank you once more for the help

flyingdragons 2020-07-19 11:12:42
There is a lot, but hope you will stick with the proper code with using too many swtich cases to check the post details
Cleo_hacker 2020-07-19 11:15:06
Okay, I’m learning..I’m not conversant with OOP PHP, I just use Laravel and previously just PHP without OOP style
flyingdragons 2020-07-19 11:17:18
I have written few article and have have ready to use got in my GitHub if your interested kindly ping me personally. I dont want to spam it for others as its Laravel group
flyingdragons 2020-07-19 11:17:41
Plain PHP articles and plain php code in GitHub
Cleo_hacker 2020-07-19 11:18:14
flyingdragons 2020-07-19 11:17:18
I have written few article and have have ready to use got in my GitHub if your interested kindly ping me personally. I dont want to spam it for others as its Laravel group

Okay, sure I will.

flyingdragons 2020-07-19 11:20:56
Hi has anyone implemented server side Syntax highlighting in Laravel.
flyingdragons 2020-07-19 11:22:34
Basically I am using Canvas Admin package for my blog. When I am saving the code to database it stores the code syntax with highlits js tags.
flyingdragons 2020-07-19 11:23:28
So in AMP html I cant use javascript to highlight the code. So somehow I need to do it server side
natghi2010 2020-07-19 14:39:33
Cleo_hacker 2020-07-19 10:55:28

I strongly recommend that you dont write the sql queries yourself

Sales.com 2020-07-19 14:51:13
http://Ritechnology.in

Ri Technologyritechnology.in
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.
Cleo_hacker 2020-07-19 14:53:26
natghi2010 2020-07-19 14:39:33
I strongly recommend that you dont write the sql queries yourself

okay… how is supposed to be done?

Cleo_hacker 2020-07-19 14:54:28
is it?
natghi2010 2020-07-19 14:56:41
Cleo_hacker 2020-07-19 14:53:26
okay… how is supposed to be done?

its not really supposed to or not supposed to

natghi2010 2020-07-19 14:57:00
its just writing statements will increase your chance of making mistakes
Cleo_hacker 2020-07-19 14:58:19
natghi2010 2020-07-19 14:57:00
its just writing statements will increase your chance of making mistakes

ooh, i get what you mean

natghi2010 2020-07-19 14:58:59
let me share my function with you
Cleo_hacker 2020-07-19 14:59:18
Okaay
natghi2010 2020-07-19 15:01:05
public function insert_db($tbl_name){
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;
}

natghi2010 2020-07-19 15:02:07
insert_db(“personal_info”);
natghi2010 2020-07-19 15:02:53
just make sure your input name matches your db field name
Cleo_hacker 2020-07-19 15:04:20
natghi2010 2020-07-19 15:02:53
just make sure your input name matches your db field name

Okay, thanks, let try to implement it

Cleo_hacker 2020-07-19 15:04:31
many thanks
natghi2010 2020-07-19 15:05:43
Cleo_hacker 2020-07-19 15:04:20
Okay, thanks, let try to implement it

lastly, when implmeneting your libraries, dont forget about security issues such as sql injection and xss attacks

Cleo_hacker 2020-07-19 15:07:30
😁😁😁Okay, I’m learning. Thanks
harmlez_prinz 2020-07-19 15:45:36
Please, in my laravel-vue spa, i noticed, user are unable to logout, until they reload the page

If you don’t reload the page, i get “unauthenticated” when i hit the logout route

focalfossa20 2020-07-19 16:21:51
when I want to do an update, the data is not updated, what should i do?,
This is my file:
view : https://pastebin.com/yLsWfh7R
route: https://pastebin.com/HLVC9rJ2
controller : https://pastebin.com/WyZvU9rw

edit.blade – Pastebin.comPastebin
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.
oxbir 2020-07-19 16:29:45
my relation id not working
focalfossa20 2020-07-19 16:29:46
focalfossa20 2020-07-19 16:21:51
when I want to do an update, the data is not updated, what should i do?,
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

focalfossa20 2020-07-19 16:30:05
oxbir 2020-07-19 16:29:45
my relation id not working

why?

oxbir 2020-07-19 16:31:04
focalfossa20 2020-07-19 16:30:05
why?

Because it show blank white page…

oxbir 2020-07-19 16:32:45
@foreach($menus as $menu)
@foreach($menu->getChild as $submenu)
@foreach($submenu->knowledgeRooms as $knowledgeRoom)
{{ dd($knowledgeRoom) }}
@endforeach
@endforeach
@endforeach
oxbir 2020-07-19 16:33:42
oxbir 2020-07-19 16:32:45
@foreach($menus as $menu)
@foreach($menu->getChild as $submenu)
@foreach($submenu->knowledgeRooms as $knowledgeRoom)
{{ dd($knowledgeRoom) }}
@endforeach
@endforeach
@endforeach

my blade

oxbir 2020-07-19 16:34:17
AppServiceProvider.php
oxbir 2020-07-19 16:35:13
public function register()
{
Schema::defaultStringLength(191);
view()->composer(‘*’, function($view) {
$view->with(‘menus’, Category::with(‘knowledge_rooms’)->whereNull(‘parent_id’)->get());
});
}
oxbir 2020-07-19 16:36:18
I have three tables categories and knowledge_rooms and category_knowledge_room. And all their data is stored.
oxbir 2020-07-19 16:36:38
Look at my codes and demo.
oxbir 2020-07-19 16:37:09
categories table

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’);

oxbir 2020-07-19 16:37:43
But I see this demo, it is white page.
oxbir 2020-07-19 16:38:29
laravel_discuss-45500.jpg

2020-07-19 16:39:31
oxbir 2020-07-19 16:38:29

What are u write in the route ?!

oxbir 2020-07-19 16:43:03
2020-07-19 16:39:31
What are u write in the route ?!

Route::view(‘/’, ‘Home.master’)->name(‘index’);

|