0
votes

I'm new to PHP. I'm currently working on adding data into a MySQL database table 'store' from store_master entered into an HTML form (Store name, address store, and status store). I'm at a complete loss. I have been struggling over this for the past few hours. I looked at a lot of similar questions, but I still don't see/understand what's going wrong in my code. About the error page it's like the image I had attached.

enter image description here

This is crudAction.php

<?php

function runQuery($db, $query, $queryType) {

    $result = mysqli_query($db, $query);
    if(!empty(mysqli_error($db))) {
        return [
            'error' => true,
            'message' => mysqli_error($db)
        ];
    }

        return [
            'error' => false,
            'message' => getMessage($queryType)
        ];
}

function buildInsertQuery($tableName, $data) {

    $tableColumns = [];
    $insertValues = [];
    foreach ($data as $key => $value) {
        array_push($tableColumns, $key);
        array_push($insertValues, $value);
    }

    $columns = "(`" . implode("`,`", $tableColumns) . "`)";
    $values = "('" . implode("','", $insertValues) . "')";
    $query = "INSERT INTO `$tableName` ". $columns . " VALUES " . $values ." ";
    
    return $query ?? false;
}

function getMessage($queryType) {
    $messages = [
        'insert' => 'Data has been succesfuly added',
        'delete' => 'Data has been succesfuly deleted'
    ];

    return $messages($queryType);
}

function getLastID() {
    return mysqli_insert_id();
}
?>

This is formAction.php

<?php

$actionObject = $_REQUEST['actionObject'] ?? '';
$actionType = $_REQUEST['actionType'] ?? '';

include $actionObject .'Crud.php';
include 'config.php';

function getTableName($actionObject) 
{
  $tableMapping = [
    'toko' => 'master_toko'
    // 'barang' => 'table_barang',
    // 'jenis' => 'table_jenis',
  ];
    return $tableMapping[$actionObject];
}

$db = getDBConnection();
$tableName = getTableName($actionObject);

switch($actionType):
  case 'insert' : $result = insertData($db, $tableName);
    break;
  case 'delete' : $result = deleteData($db, $tableName);
    break;
  case 'update' : $result = updateData($db, $tableName);
    break;
endswitch;

if (!$result['error']) {
    header('location: ' . $_REQUEST['pageReferrer'] . '&success=true&message=' . $result['message']);
} else {
    header('location: ' . $_REQUEST['pageReferrer'] . '&error=true&message=' . $result['message']);
}



?>

This is tokoCrud.php

    <?php
        include ('crudAction.php');
        
        function insertData($db, $tableName) {
            $data = [
                'id_ot' => ' ',
                'nama_ot' => $_POST['namaToko'],
                'alamat_ot' => $_POST['alamatToko'],
                'status_ot' => '1'
            ];
            $query = buildInsertQuery($tableName, $data);

            return runQuery($db, $query, 'insert');
        }

    ?>

This is the HTML Form

<?php
  require "partial/header.php";
  require "partial/sidebar.php";

  
 ?>
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1>Master Toko</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="#">Home</a></li>
              <li class="breadcrumb-item active">Master Toko</li>
            </ol>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>

    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-12">
            <div class="card">
              <div class="card-header">
                <h3 class="card-title">Data Toko</h3>
                <button type="button" class="btn btn-success float-right" data-toggle="modal" data-target=".modalBesar">Tambah Data</button>
              </div>
            <!-- Bagian Form Modal -->
              <div class="modal fade modalBesar" tabindex="-1" role="dialog" aria-hidden="true">
                <div class="modal-dialog">
                <div class="modal-content">
                <div class="modal-header">
                  <h4 class="modal-title" id="exampleModalLabel">Tambah Toko</h4>
                  <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                  </button>
                </div>
              <div class="modal-body">
                <form method = "post" action = "library/services/formAction.php">
                <input type="hidden" name="pageReferrer" value="" >
                <input type="hidden" name="actionObject" value="toko" >
                <input type="hidden" name="actionType" value="insert" >
                  <div class="form-group">
                    <label for="recipient-name" class="col-form-label">Nama Toko</label>
                    <input type="text" class="form-control" id="namaToko" placeholder="Masukkan Nama Toko" name="namaToko" required>
                  </div>
                  <div class="form-group">
                    <label for="recipient-name" class="col-form-label">Alamat Toko</label>
                    <input type="text" class="form-control" id="alamatToko" placeholder="Masukkan Nama Toko" name="alamatToko" required>
                  </div>
                  <div class="modal-footer">
                <button type="submit" class="btn btn-primary" name="submit">Submit</button>
              </div>
                </form>
              </div>
                  </div>
              </div>
            </div>
            <!-- Akhir Bagian Modal -->

            

              <!-- /.card-header -->
              <div class="card-body">
                <table id="example1" class="table table-bordered table-striped">
                  <thead>
                  <tr>
                    <th>No</th>
                    <th>Toko</th>
                    <th>Alamat</th>
                    <th>Status</th>
                    <th>Opsi</th>
                  </tr>
                  </thead>
                  <tbody>
                  
                  </tbody>
                </table>
              </div>
              <!-- /.card-body -->
            </div>
            <!-- /.card -->
          </div>
          <!-- /.col -->
        </div>
        <!-- /.row -->
      </div>
      <!-- /.container-fluid -->
    </section>
    <!-- /.content -->
    
  </div>
  <!-- /.content-wrapper -->
  <!-- /.content-wrapper -->
  <?php require "partial/footer.php"; ?>
return $messages($queryType); should be return $messages[$queryType]; in crudAction.php - Mattigins
Ahh okay thank you, and then i have another question, how if i want to redirect to my store page html from the execution and show the message info from getMessage function in the crudAction.php, i have input my code in the formAction.php, but it still didn't work - Ridho Wisnu