Could you help the newbie ?!
In general, there is a database. In Denver. (although the database does not matter) There are CARS, FOOTBALL and COUNTRIES bases.
Inside the bases all the same! For example: in CARS the MAIN table (Merc, BMW, Audi, etc.)
And then comes the table MODELS, which are tied to the MARKES (for example, MERS - Gelandewagen, SLK and CLK).
In FOOTBALL - the main CLUBS, and the second - PLAYERS in them. In COUNTRIES - the main COUNTRY, and SECOND - CITY in them.

The essence of the question: the fact is that I have a lot of different requests from "process.php" in "functions.php" for functions such as INSERT, SELECT, UPDATE and DELETE:

function insert_add_marka($connect){ $marka=@$_POST['marka_name']; $sql="SELECT * FROM marki WHERE marka_name='$marka'"; $res=mysqli_query($connect,$sql) or die(mysqli_error($connect)); $get=mysqli_fetch_assoc($res); if($get['id']==0){ $sql="INSERT INTO marki (marka_name)value('$marka')"; $res = mysqli_query($connect,$sql); echo "<script>window.location='index.php'</script>"; } } function update_edit_marka($connect){ $marka=@$_POST['marka_name']; $marka_id=@$_POST['marka_id']; $sql="UPDATE marki SET marka_name='$marka' WHERE id=$marka_id"; $res = mysqli_query($connect,$sql); echo "<script>window.location='index.php'</script>"; } function insert_add_model($connect){ $file_name=null; $file_error=@$_FILES['photo_file']['error']; if($file_error==0){ $file_name=@$_FILES['photo_file']['name']; $file_tmp_name=@$_FILES['photo_file']['tmp_name']; move_uploaded_file($_FILES['photo_file']['tmp_name'], "../uploads/". $_FILES['photo_file']['name']); } $mod_name=@$_POST['model_name']; $opisanie=@$_POST['opisanie']; $marka_id=@$_POST['marka_id']; $sql="SELECT * FROM car_models WHERE model_name='$mod_name'"; $res=mysqli_query($connect,$sql) or die(mysqli_error($connect)); $get=mysqli_fetch_assoc($res); if($get['id']==0){ $sql = "INSERT INTO car_models (model_name, photo, opisanie, marka_id)values('$mod_name','$file_name','$opisanie', $marka_id)"; $res = mysqli_query($connect,$sql); echo "<script>window.location='model.php?marka_id=$marka_id'</script>"; } } 

Now the teacher wants me to have new universal functions Insert, select, update and delete for all occasions!

But how to do it, for example, if for example in $sql="INSERT INTO marki (marka_name)value('$marka')" are 3 parameters, and in $sql = "INSERT INTO car_models (model_name, photo, opisanie, marka_id)values('$mod_name','$file_name','$opisanie', $marka_id)" - already 9 parameters!

In $sql="UPDATE marki SET marka_name='$marka' WHERE id=$marka_id"; - 4 parameters come, and in $sql = "UPDATE car_models SET model_name='$mod_name', photo='$file_name', marka_id='$marka_id', opisanie='$opisanie' WHERE id=$model_id"; - 9-10 parameters!

Already 4 day I write the code - it is impossible! And I can not find such on the Internet!

Has anyone already come across - and wrote such universal functions INSERT, SELECT, UPDATE и DELETE ?!

ATTENTION: I do not use classes yet! Please write answers without them!

Thanks in advance for the cuff in the right direction! :-)

  • Pass in the function not individual parameters, but arrays. And inside already do explode(',', $arr) or to taste - toxxxa
  • Can you give an example? What will it look like? - aiba

1 answer 1

In a real project, this cannot be done in any way, but for the “kick in the right direction” it’s the most:

 $car_models_values = array( 'model_id' => '1', 'model_name' => 'Name1', 'photo' => 'photo.png', 'opisanie' => 'bla-bla-bla', ); insert('car_models', $car_models_values); function insert($table, $data) { $columns = "'" . implode("','", array_keys($data)) . "'"; $values = "'" . implode("','", array_values($data)) . "'"; $sql = "INSERT INTO {$table} ({$columns}) VALUES ({$values})"; ... } 
  • But here I have another question: What should I do if I have two kinds of SELECT? In one, I just get over the array, and the second after the bulkhead is used to display the result in a table! - aiba
  • 1) function select2 ($ hren1, $ hren2, $ connect) {[B] // the array is moved here. [/ B] $ sql = "SELECT * FROM $ hren1 WHERE id = $ hren2"; // echo $ sql; $ hren3 = mysqli_query ($ connect, $ sql); $ hren4 = mysqli_fetch_assoc ($ hren3); return $ hren4; 2) function select4 ($ hren1, $ hren2, $ hren3, $ connect) {[B] // here after iterating through the array - there is a cycle in the table [/ B] $ sql = "SELECT * FROM $ hren1 WHERE $ hren2 = $ hren3 "; $ hren4 = mysqli_query ($ connect, $ sql); $ hren5 = array (); $ i = 0; while ($ hren6 = mysqli_fetch_assoc ($ hren4)) {$ hren5 [$ i] = $ hren6; $ i ++; } return $ hren5; } - aiba
  • Thanks @toxxxa! <br> And how to make the UPDATE function for an array? <br> $ sql = "UPDATE car_models SET model_name = '$ mod_name', marka_id = '$ marka_id', opisanie = '$ opisanie' WHERE id = $ model_id"; - aiba