This forum is archived. Posts are preserved for historical reference. For current help, join us on Discord.

structure for statement in php for mysqli

In UserSpice 5 - Roadmapping the Future · Started by picassoo on 2016-08-22 10:06 am · 10089 views · 3 replies

Hi, can you post here... separate script for: Insert - Upload - select- delete, each option. PHP script only.
I want to do a table, but can not understand script, models/funcs.php

Example
table:
Id,
name,
email,
phone,
and description

Here what I have, example.


<pre>
// Editare Lista produse *******************************************************************************************************************************
function fetchAllbarangg()
{
	global $mysqli,$db_table_prefix; 
	$stmt = $mysqli->prepare("SELECT 
		id_brg,
		nama_brg,
		harga_brg,
		desc_brg,
		jml_brg,
		gambar_brg,
		detali_ac,
		detali_atentie,
		Masuri_de_siguranta,
		Semne_si_simptome,
		Pastrare_si_utilizare,
		oferta
		FROM ".$db_table_prefix."barang 
		WHERE 
		id_brg = ?
		LIMIT 1");
	$stmt->execute();
	$stmt->bind_result($id_brg, $nama_brg, $harga_brg, $desc_brg, $jml_brg, $gambar_brg, $detali_ac, $detali_atentie, $Masuri_de_siguranta, $Semne_si_simptome, $Pastrare_si_utilizare, $oferta);
	
	while ($stmt->fetch()){
		$row[] = array('id_brg' => $id_brg, 'nama_brg' => $nama_brg, 'harga_brg' => $harga_brg, 'desc_brg' => $desc_brg, 'jml_brg' => $jml_brg, 'gambar_brg' => $gambar_brg, 'detali_ac' => $detali_ac, 'detali_atentie' => $detali_atentie, 'Masuri_de_siguranta' => $Masuri_de_siguranta, 'Semne_si_simptome' => $Semne_si_simptome, 'Pastrare_si_utilizare' => $Pastrare_si_utilizare, 'oferta' => $oferta);
	}
	$stmt->close();
	return ($row);
}
</pre>



Thanks.
You are thinking procedurally, instead of thinking like an object oriented programmer. UserSpice can do procedural, but you have to use your own database class (or at least establish your own connection). And...make sure your first column is id. You can have id_brg, but you also want to have a regular id in your database.


Regardless of whether you are updating or inserting, you want to put all of your data into an array before you do your query.
$fields=array('id_brg'=>$id_brg, 'nama_brg'=>'$nama_brg'); //column_name=>entry

Then, if you are INSERTING a new record, all you need is the table you are trying to insert into

So, it would be
$db->insert('table_name',$fields);

If you are UPDATING, you need to know what record you are updating, so you need to give it that id.
$db->update('table_name',5,$fields);

To DELETE, you only really need to know the table and some form of logic. So do you want to delete when the username is "bob" or delete when the "id" equals 4. Whatever you want to do, just choose the table name and the logic.

$db->delete('table_name',array('id','=',4));

Pretty much everything you do is just a longer version of that. Does that make sense?
http://userspice.org/documentation-db-class-2/
you are very good, thank you, now I understand.
Awesome. Glad I could help.