So I have a problem with many to many relationships.
Currently, I have surat and surat_user table.
How do I insert data into surat and at the same time insert multiple values from select2 multiple forms into surat_user table and how to get data so I can update it.
UPDATE :
I solve the insert problem please see answer below
But now i have no idea how to update those values.
For example
surat_user
id_surat | id_user
1 | 1
1 | 2
How to update surat_user (in controller and model) if i want to remove one of id_user where 'id_surat = 1`
At the moment i don't know how to fetch the multiple values into select2 form edit so here is mu uncomplete codes :
Controller
public function edit_sm($id_surat){
$this->load->view('template/header');
$this->load->view('template/sidebar');
$where = array('id_surat' => $id_surat);
//$data=array('id_status'=> $this->M_sm->get_option());
$data['surat'] = $this->M_sm->edit_sm($where,'surat')->result();
$this->load->view('v_edit_surat',$data);
$this->load->view('template/footer');
}
public function edit_sm_proses() {
$data = array(
'id_surat'=>$this->input->post('id_surat'),
'no_surat'=>$this->input->post('no_surat'),
'id_status'=>$this->input->post('id_status'),
'id_user'=>$this->input->post('id_user')
);
$where = array(
'id_surat' => $id_surat
);
$this->M_sm->edit_sm_proses($where,$data,'surat');
redirect('SuratMasuk');
}
Model:
public function edit_sm($where,$table){
$this->db->join('status_surat', 'status_surat.id_status = surat.id_status');
return $this->db->get_where($table,$where);
}
public function edit_sm_proses($where,$data,$table){
$this->db->where($where);
$this->db->update($table,$data);
}
View
<section class="content">
<div class="row">
<div class="col-xs-12">
<div class="box box-solid box-primary"">
<div class="box-header with-border">
<h3 class="box-title">Default Box Example</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<?php foreach ($surat as $key) { ?>
<form method="post" action="<?php echo base_url()."SuratMasuk/edit_sm_proses" ?>" enctype="multipart/form-data" />
<input type="hidden" name="id_surat" value="<?=$key->id_surat?>">
<div class="form-group">
<label class="control-label col-lg-2">No Surat</label>
<div class="col-lg-5">
<input type="text" name="no_surat" class="form-control no_surat" placeholder="Masukkan Nomor Surat" value="<?=$key->no_surat?>">
<span class="help-block"></span>
</div>
</div>
<br>
<br>
<div class="form-body">
<div class="form-group">
<label class="control-label col-lg-2">Status</label>
<div class="col-lg-5">
<select class="form-control select2 id_status" name="id_status" style="width: 100%;">
<option value="<?=$key->id_status;?>" selected="<?=$key->id_status;?>"><?php echo $key->status;?></option>
<?php foreach ($id_status as $row) { ?>
<option value="<?php echo $row->id_status; ?>"> <?php echo $row->status; ?></option>
<?php } ?>
</select>
<span class="help-block"></span>
</div>
</div>
</div>
<br>
<br>
<div class="form-body">
<div class="form-group">
<label class="control-label col-lg-2">Disposisi</label>
<div class="col-lg-5">
<select class="form-control select2 id_user" name="id_user[]" style="width: 100%;">
<option value="<?=$key->id_user;?>" selected="<?=$key->id_user;?>"><?php echo $key->nama;?></option>
<?php foreach ($id_user as $row) { ?>
<option value="<?php echo $row->id_user; ?>"> <?php echo $row->nama; ?></option>
<?php } ?>
</select>
<span class="help-block"></span>
</div>
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button class="btn btn-danger" type="reset" value="reset">Reset</button>
<button class="btn btn-info">Update</button><br>
Kembali
</form>
<?php
}
?>
</div>
<!-- box-footer -->
</div>
<!-- /.box -->
</div>
<!-- /.col -->
</div>
<!-- /.row -->
</section>
<!-- /.content -->
Current Result :
result from codes above
Manage to solve the problem by my self.
i change my controller to this:
public function add_sm_proses(){
$data = array(
'no_surat'=>$this->input->post('no_surat'),
'id_status'=>$this->input->post('id_status')
);
$insert = $this->M_sm->trans_surat_user($data);
if($insert=1){
redirect('SuratMasuk');
} else {
echo "<h2>Gagal menambahkan data</h2>";
}
}
change my model to this :
public function trans_surat_user($data){
$this->db->trans_start();
$this->db->insert('surat', $data);
$id_surat = $this->db->query('SELECT surat.id_surat FROM surat ORDER BY id_surat DESC limit 1');
foreach ($id_surat->result() as $row) {
$id_surat_result = $row->id_surat;
}
$id_user = $_POST['id_user'];
foreach ($id_user as $data2) {
array_push($id_user, $data2);
$this->db->query('INSERT INTO surat_user (id_surat,id_user) VALUES ('.$id_surat_result.','.$data2.')');
}
$this->db->trans_complete();
}
class SuratMasuk extends CI_Controller {
public function add_sm_proses(){
$data = array(
'no_surat'=>$this->input->post('no_surat'),
'id_status'=>$this->input->post('id_status'),
);
Select MAX ID from table like this
$id_surat = $this->db->query('SELECT MAX(surat.id_surat) AS maxid FROM surat')->row()->maxid;
Update:-
$id_surat will give MAX ID present in the table. Don't use for next record.
First, Increment it by value 1 and then use it
$id_surat = $id_surat + 1;
$id_user = $this->input->post('id_user') ? $this->input->post('id_user') : array();
First, check if $id_user have values or not otherwise foreach loop will give e error
if(count($id_user) > 0){
foreach ($id_user as $u){
$data_values = array('id_surat' => $id_surat, 'id_user' => $u);
$this->db->insert('surat_user', $data_values);
}
}
$insert = $this->M_sm->add_sm($data);
$insert will not always return 1. So, only check if value exists
if($insert){
redirect('SuratMasuk');
} else {
echo "<h2>Gagal menambahkan data</h2>";
}
}
}
First, Show all record from DB and for each record add a button to edit the record.
Note:- Don't use <form> inside the loop
<a class="btn btn-warning btn-sm" href="<?= base_url('SuratMasuk/edit_sm/'.$row->id_surat)?>"><i class="fa fa-pencil-square-o" aria-hidden="true"></i>Edit</a>
Controller for Edit
I'm just adding backend code here
where and table is reserved word for DB. It is better to not use it or use in another way.
public function edit_sm($id_surat){
$condition_array = array('id_surat' => $id_surat);
$data['surat'] = $this->M_sm->edit_sm($condition_array ,'surat');
}
Model for Edit
Get the record for edit
This function is getting only one record from DB so use row()
public function edit_sm($condition_array ,$record_in){
$this->db->join('status_surat', 'status_surat.id_status = surat.id_status');
return $this->db->get_where($record_in, $condition_array )->row();
}
Now, Form for will show with data populated in it.
Getting updated data from Controller
public function edit_sm_proses() {
$data = array(
'id_surat'=> $this->input->post('id_surat'),
'no_surat'=>$this->input->post('no_surat'),
'id_status'=>$this->input->post('id_status'),
'id_user'=>$this->input->post('id_user')
);
$condition_array = array(
'id_surat' => $this->input->post('id_surat')
);
$this->M_sm->edit_sm_proses($condition_array, $data, 'surat');
redirect('SuratMasuk');
}
Updating data in Model
public function edit_sm_proses($condition_array, $data, $record_in){
$this->db->where($condition_array);
return $this->db->update($record_in, $data);
}
Related
could someone help me to find the error? only the post_image input is saved the others are not saved in the database what am I doing wrong?
I've checked it several times but only the filename of the post_image field is saved in the database, the other files are not persisting in the database, could someone help me where I'm going wrong?
Every help is welcome.
Thank you very much in advance.
create.blade.php
div class="form-group row">
<label for="post_image" class="col-md-4 col-form-label text-md-right">{{ __('Image') }}</label>
<div class="col-md-6">
<input type="file" name="post_image"/>
#error('post_image')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<br>
<hr>
<label for="post_video" class="col-md-4 col-form-label text-md-right">{{ __('Video') }}</label>
<div class="col-md-6">
<input type="file" name="post_video"/>
#error('post_video')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<br>
<hr>
<label for="post_gif" class="col-md-4 col-form-label text-md-right">{{ __('GIF') }}</label>
<div class="col-md-6">
<input type="file" name="post_gif"/>
#error('post_gif')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<br>
<hr>
</div>
function store in controller -->
public function store(Request $request, Community $community)
{
$user = Auth::user();
$creds = $request->validate([
'post_image' => '|image|mimes:jpeg,jpg,png,svg|max:18048',
'post_video' => 'mimes:mp4,ogx,oga,ogv,ogg,webm|max:180048',
'post_gif' => '|image|mimes:gif|max:18048',
'title' => ['required'],
'post_text' => ['required'],
'post_url' => ['required']
]);
//IMAGE JPG,PNG,SVG
if ($image = $request->file('post_image')) {
$destinationPath = 'media/uploads';
$profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
$image->move($destinationPath, $profileImage);
$input['post_image'] = "$profileImage";
}
//END IMAGE JPG,PNG,SVG
//VIDEO MP4
if ($image = $request->file('post_video')) {
$destinationPath = 'media/uploads';
$profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
$image->move($destinationPath, $profileImage);
$inputmp4 ['post_video'] = "$profileImage";
}
//END VIDEOS
// GIF IMAGES
if ($image = $request->file('post_gif')) {
$destinationPath = 'media/uploads';
$profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
$image->move($destinationPath, $profileImage);
$inputgif ['post_gif'] = "$profileImage";
}
//END GIF IMAGES
$post = $community->posts()->create
(['user_id' => auth()->id(),
'title' => $creds['title'],
'post_text' => $creds['post_text'],
'post_url' => $creds['post_url'],
'post_image' => $input['post_image'] ?? '',
'post_video' => $inputmp4['post_video'] ?? '',
'post_gif' => $inputgif['post_gif'] ?? '',
]);
return redirect()->route('communities.show', $community);
}
Is it possible post_video and post_gif are empty? Php has a max_upload and max post size limit that is defined on the ini file. It is possible that that size is being exceeded. Hence your files are not actually uploading.
Also, your if statements are kinda confusing. Check this out :
//This if statement will always return true. Since you're assigning $image variable to $request->file('post_video').
if ($image = $request->file('post_video')) {
//Code For saving file
//Don't use this
}
// Instead you should use hasFile to check if the file is uploaded. here's how
if ($request->hasFile('post_video')) {
$video = $request->file('post_video');
//Code For saving file
//Use This instead
}
Also, at the last line, where you call create() method, do this:
$post = $community->posts()->create
(['user_id' => auth()->id(),
'title' => $creds['title'],
'post_text' => $creds['post_text'],
'post_url' => $creds['post_url'],
'post_image' => $input['post_image'], // skip the "?? ''" part
'post_video' => $inputmp4['post_video'],
'post_gif' => $inputgif['post_gif'],
]);
Now, if either post_image, post_video or post_gif is empty, you'll get a error and can see if the data is wrong.
Also, on a kinda related note, did you know laravel has a good helper to save files in storage? Check it out
I have a problem with how to upload files excel with phpsreadsheet to the database. I want to put the data into two tables namely:
Beasiswa contain table: beasiswa_nama, beasiswa_id, beasiswa_file (link file), beasiswa_tahun, DATE_CREATED, DATE_UPDATED.
data_mahasiswa table cotain table: id_mhs, nama_mhs, nim_mhs, (FK) id_beasiswa
How to insert a file and read it to the Beasiswa table, and student data to data_mahasiswa?
Relation table:
Error create data:
Beasiswa Controller
public function create()
{
$this->form_validation->set_rules('nama_beasiswa', 'Nama Beasiswa', 'trim|required');
$this->form_validation->set_rules('tahun_beasiswa', 'Tahun Mahasiswa', 'trim|required');
if (empty($_FILES['nama_mahasiswa_beasiswa']['name'])) {
$this->form_validation->set_rules('nama_mahasiswa_beasiswa', 'File Nama Mahasiswa', 'required');
}
if ($this->form_validation->run() == TRUE) {
// true case
$upload_nama_mahasiswa = $this->_upload_nama_mahasiswa();
$data = array(
'beasiswa_nama' => htmlspecialchars($this->input->post('nama_beasiswa', true)),
'beasiswa_tahun' => htmlspecialchars($this->input->post('tahun_beasiswa', true)),
'beasiswa_file' => $upload_nama_mahasiswa,
'date_created' => date("Y-m-d H:i:s")
);
$create = $this->m_beasiswa->create($data);
if ($create == true) {
$this->session->set_flashdata('success', 'Berhasil Upload Data Beasiswa');
redirect('beasiswa/', 'refresh');
} else {
$this->session->set_flashdata('errors', 'Terjadi Galat. Silahkan Periksa Kembali!!');
redirect('beasiswa/create', 'refresh');
}
} else {
$this->render_template('beasiswa/create', $this->data);
}
}
private function _upload_nama_mahasiswa()
{
$filename = 'import-data';
$config['upload_path'] = 'assets/file/beasiswa/';
$config['file_name'] = $filename;
$config['overwrite'] = TRUE;
$config['allowed_types'] = 'xlsx|xls';
$config['max_size'] = '1024';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('nama_mahasiswa_beasiswa')) {
// $error = array('errors' => $this->upload->display_errors('File yang anda masukkan tidak sesuai permintaan, atau file terlalu besar.'));
$this->session->set_flashdata('errors', 'File yang anda masukkan tidak sesuai permintaan, atau file terlalu besar.');
redirect('beasiswa/create', 'refresh');
} else {
$data = array('upload_data' => $this->upload->data());
$img = $data['upload_data']['file_name'];
$excelReader = new PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$loadExcel = $excelReader->load($config['upload_path'] . $img);
$sheet = $loadExcel->getActiveSheet()->toArray(null, true, true, true);
$dataExcel = array();
// $this->data['sheet'] = $sheet;
$numRow = 3;
foreach ($sheet as $row) {
if ($numRow > 3) {
array_push(
$dataExcel,
[
'nama_mhs' => $row['A'],
'nim_mhs' => $row['B']
]
);
}
$numRow++;
}
$this->db->insert_batch('data_beasiswa', $dataExcel);
unlink($config['upload_path'] . $img);
}
}
Beasiswa Model
public function create($data)
{
if ($data) {
$insert = $this->db->insert('beasiswa', $data);
return ($insert == true) ? true : false;
}
}
Beasiswa View
<?php
defined('BASEPATH') or exit('No direct script access allowed');
?>
<!-- Main Content -->
<div class="main-content">
<section class="section">
<div class="section-header">
<div class="section-header-back">
<i class="fas fa-arrow-left"></i>
</div>
<h1>Tambah Beasiswa</h1>
</div>
<div class="section-body">
<div class="row">
<div class="col-md-12 col-xs-12 col-xl-12 col-12">
<div id="messages"></div>
<?php if ($this->session->flashdata('success')) : ?>
<div class="alert alert-success alert-dismissible">
<div class="alert-body">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<?php echo $this->session->flashdata('success'); ?>
</div>
</div>
<?php elseif ($this->session->flashdata('errors')) : ?>
<div class="alert alert-danger alert-dismissible show fade">
<div class="alert-body">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<?php echo $this->session->flashdata('errors'); ?>
</div>
</div>
<?php endif; ?>
<div class="card">
<div class="card-body">
<b style="font-size: 17px">Unduh format excel data mahasiswa</b><br>
Download template
<form action="<?php base_url('beasiswa/create') ?>" method="POST" enctype="multipart/form-data">
<div class="form-group">
<div class="section-title">Nama Program Beasiswa</div>
<input type="text" class="form-control" name="nama_beasiswa" id="nama_beasiswa" placeholder="Nama Program Beasiswa" value="<?= set_value('nama_beasiswa'); ?>">
<?= form_error('nama_beasiswa', '<small class="text-danger">', '</small>'); ?>
</div>
<div class="form-group">
<div class="section-title">Tahun Beasiswa</div>
<input type="text" class="form-control" name="tahun_beasiswa" id="tahun_beasiswa" placeholder="Tahun Beasiswa (contoh: 2020)" value="<?= set_value('tahun_beasiswa'); ?>">
<?= form_error('tahun_beasiswa', '<small class="text-danger">', '</small>'); ?>
</div>
<div class="form-group">
<div class="section-title">Upload File Nama Mahasiswa</div>
<input type="file" name="nama_mahasiswa_beasiswa" id="nama_mahasiswa_beasiswa" class="form-control col-sm-6 col-md-6">
<p class="text-danger" style="font-style:italic"><b>* Upload file sesuai dengan format yang sudah disesuaikan</b></p>
</div>
<button type="submit" class="btn btn-primary">Simpan</button>
Kembali
</form>
</div>
</div>
</div>
</div>
</div>
</section>
</div>
It is because foreign key error probably happening because you insert a null data in data_mahasiswa table.
There was empty space in your excel (that probably have been edited?) that are used in foreach ($sheet as $row). You can fix this with:
foreach ($sheet as $row) {
if ($numRow > 3) {
if($row['A'] != null && $row['B'] != null)
{
$dataExcel[] = array(
'nama_mhs' => $row['A'],
'nim_mhs' => $row['B']
);
}
/*you can also break the function, if no empty row between data in your excel*/
/*else
{
break;
}*/
}
}
you need to get rid of the null value when you input the data to the database. above this line of your code at Beasiswa Controller
$this->db->insert_batch('data_beasiswa', $dataExcel);
add this line of code to delete cell that have null data
foreach ($dataExcel as $data => $val) {
if(empty($val['nama_mhs'])){
unset($dataExcel[$data]['nama_mhs']);
unset($dataExcel[$data]['nim_mhs']);
}
}
$dataExcel = array_filter($dataExcel);
$data = array('data_excel' => $dataExcel);
I want to retrieve image from folder and rest of data from database using CodeIgniter.
Right now I am able to retrieve data only from database.
Can anyone explain how to get an image from any folder and display it in a web page.
Follow this procedure:
Model
function getDataByID ($user_id)
{
$q = $this->db->query("SELECT name, image FROM tbl_user WHERE user_id = $user_id");
if ($q->num_rows() > 0) {
$d = $q->row_array();
if ($d['image'] != '') {
$temp = $d['image'];
$d['image'] = "http://domain.com/myimages/".$temp;
} else {
$d['image'] = '';
}
return $d;
} else {
return array();
}
}
Controller
function user_detail($user_id)
{
$dt = $this->code_model->getDataByID($user_id);
$display['user_data'] = $dt;
$this->load->view('user_view',$display);
}
View
if(isset($user_data) && !empty($user_data)) {
extract($user_data);
} ?>
<div class="form-group">
<label class="col-lg-2 control-label">Name</label>
<div class="col-lg-6">
<?php echo $name; ?>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Image</label>
<div class="col-lg-6">
<img src="<?=$image?>">
</div>
</div>
I am not sure how you are constructing the page, but the easiest way to add an image is to set the data to a variable in PHP and adding it to the <img> tag:
<img src="{base_url}_assets/_images/folder/<?=$productData[0]->Image1?>" />
I am using auto complete, here problem is repeating same tuples.
Have to use Distinct with like in my code.
Here is my Model code:
public function get_products()
{
if(!isset($_REQUEST['term']))
exit;
$data=trim($_REQUEST['term']);
$this->db->like('Name',$data);
$result=$this->db->get('eezy_product');
if($result->num_rows()>0)
{
foreach($result->result_array() as $row)
{
$result_array[] = array(
'label' => $row['Name'],
'value' => $row['Name'],
'keyid' =>$row['ProductID'],
);
}
}
else
{
$result_array=false;
}
return $result_array;
}
Here is my view file :
<link href="<?php echo base_url();?>/css/jquery-ui-1.8.2.custom.css" rel="stylesheet">
<script type="text/javascript" src="<?php echo base_url()?>js/jquery1.7.1.js"></script>
<script type="text/javascript" src="<?php echo base_url()?>js/jquery-ui-1.8.2.custom.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#keyword').autocomplete({
source:'<?php echo base_url()?>index.php/search/get_products', minLength:2,
});
});
</script>
<div class="col-lg-2 search">
<form class="form-vertical" role="form" method="post" action="<?php echo base_url()?>index.php/search/product">
<div class="col-lg-12">
<input type="text" name='keyword' id='keyword' class="form-control" placeholder='Keyword'>
</div><!-- /.col-lg-12 -->
<input type="submit" value="Search">
</form>
</div>
Here in text box am getting repeated values also as auto suggest, but I want to remove duplicates
Try this:
$data=trim($_REQUEST['term']);
$this->db->distinct();
$this->db->like('Name',$data);
$result=$this->db->get('eezy_product');
$data=trim($_REQUEST['term']);
$this->db->select('Name, ProductID');
$this->db->distinct('Name');
$this->db->like('Name',$data);
$result=$this->db->get('eezy_product');
Try to group by name like this
$data=trim($_REQUEST['term']);
$this->db->select("Name, ProductID");
$this->db->like('Name',$data);
$this->db->group_by("Name");
$result=$this->db->get('eezy_product');
My line of code below is supposed to update a NULL value field with (in this case) a pre-defined value. When i execute my wpdb query however the page gives a 500 error.
$wpdb->query( $wpdb->query( "UPDATE ziekbeter SET healthy= '1994-06-04' WHERE person = 5 AND sick IS NOT NULL AND healthy IS NULL") );
Can someone take a look at the line of code and possibly tell me whats wrong?
The code is being executed on a button click.
A screenshot of my wp table is added.
Person ID and the healthy date are going to be dynamic but for now im keeping it static.
profile.php
<?php
$user_ID = get_current_user_id();
echo $user_ID;
global $wpdb;
if ($wpdb->get_results( "SELECT * FROM ziekbeter WHERE person = $user_ID AND healthy IS NULL"))
{
$row = $wpdb->get_results( "SELECT * FROM ziekbeter WHERE person = $user_ID AND healthy IS NULL");
{
?>
<form action="<?php bloginfo('url'); ?>/wp-content/themes/stk/ziekbeter.php" method="post">
<input type="submit" name="submitBeter" value="Meld mij beter!">
</form>
<?php
}
}
elseif ($wpdb->get_results("SELECT healthy FROM ziekbeter WHERE person = $user_ID AND healthy IS NOT NULL"))
{
$row = $wpdb->get_results( "SELECT * FROM ziekbeter WHERE person = $user_ID AND healthy IS NOT NULL");
{
?>
<form action="<?php bloginfo('url'); ?>/wp-content/themes/stk/ziekbeter.php" method="post">
<input type="submit" name="submitZiek" value="Meld mij ziek!">
</form>
<?php
}
}
else {
?>
<form action="<?php bloginfo('url'); ?>/wp-content/themes/stk/ziekbeter.php" method="post">
<input type="submit" name="submitZiek" value="Meld mij ziek!">
</form>
<?php
}
?>
ziekbeter.php
if(isset($_POST['submitZiek']))
{
/* This function will come after i got the submitBeter working */
}
elseif(isset($_POST['submitBeter']))
{
$wpdb->query( $wpdb->query( "UPDATE ziekbeter SET healthy= '1994-06-04' WHERE person = 5 AND sick IS NOT NULL AND healthy IS NULL") );
echo "submitBeter wordt uitgevoerd";
}
Should i replace the wpdb-> query with an echo the code will execute properly and run the echo without any problems.
Try reworking the logic, something like:
profile.php
<?php
global $wpdb;
$user_ID = get_current_user_id();
echo $user_ID;
//First DB query
$row1 = $wpdb->get_results("SELECT * FROM ziekbeter WHERE person = $user_ID AND healthy IS NULL");
//Second DB query
$row2 = $wpdb->get_results("SELECT healthy FROM ziekbeter WHERE person = $user_ID AND healthy IS NOT NULL");
// I.e. greater than zero draw the HTML form
if (count($row1)>0) {
?>
<form action="<?php esc_url(bloginfo('url')); ?>/wp-content/themes/stk/ziekbeter.php" method="post">
<input type="submit" name="submitBeter" value="Meld mij beter!">
</form>
<?php
}
// Second DB query draw different HTML form
if (count($row2)>0) {
?>
<form action="<?php esc_url(bloginfo('url')); ?>/wp-content/themes/stk/ziekbeter.php" method="post">
<input type="submit" name="submitZiek" value="Meld mij ziek!">
</form>
<?php
}
// Draw third HTML form otherwise
if (!$row1 || !$row2) {
?>
<form action="<?php esc_url(bloginfo('url')); ?>/wp-content/themes/stk/ziekbeter.php" method="post">
<input type="submit" name="submitZiek" value="Meld mij ziek!">
</form>
<?php
}
?>
Now, the other file, do not forget to globalize the $wpdb variable:
ziekbeter.php (EDITED):
<?php
global $wpdb;
if(isset($_POST['submitZiek'])) {
/* This function will come after i got the submitBeter working */
}
if(isset($_POST['submitBeter'])) {
$result = $wpdb->query( "UPDATE ziekbeter SET healthy= '1994-06-04' WHERE person = 5 AND sick IS NOT NULL AND healthy IS NULL");
/*Or, use the native WordPress function:
$result = $wpdb->update( $table, $data, $where, $format = null, $where_format = null ); */
if ($result) echo "submitBeter wordt uitgevoerd";
}
?>
If you're trying to access those files directly, i.e. http://example.com/wp-content/themes/stk/profile.php and http://example.com/wp-content/themes/stk/ziekbeter.php, then you need to include WordPress:
<?php
require_once '../../../wp-load.php';
// rest of profile.php
<?php
require_once '../../../wp-load.php';
// rest of ziekbeter.php