inserting array using button php - php

hey guys i've been trying to insert an bunch of data from array and variable into database using button , but it can't be inserted and the database still empty , is there anyone who know where is my mistake ?
here is my code
<?php
include 'koneksi.php';
$arr_nf = array(1,0.334,-0.334,-1);
$arr_ef = array(0,0.333,0.667,1);
$arr_lf = array(1,0.667,0.333,0);
$kelas = "80 keatas" ;
$iduser = "8" ;
$query = mysql_query(" select id_alternatif from tb_alternatif where kelas = '".$kelas."' order by nama "); // run query
$idalter = array();
while($row = mysql_fetch_assoc($query)){
$idalter[] = $row;
}
////////////////////////////////////////// RANGKING LF
foreach ($arr_lf as $val){ // net flow kali 1000
$arr_lfx[]= $val * 1000 ;
}
$int_lfx = array_map( // rubah jd integer
function($value) { return (int)$value; },
$arr_lfx );
$lfx_copy = $int_lfx;
rsort($lfx_copy);
$lfx_copy = array_flip($lfx_copy);
foreach($int_lfx as $val){
$rangkinglf[] = ($lfx_copy[$val]+1);
}
//////////////////////////////////////////
$jumlah_atlet = count($arr_nf);
if (isset($_POST['submit'])){
for($x=0; $x<$jumlah_atlet ;$x++){
mysql_query(" INSERT INTO tb_histori(id_histori, id_user, id_alternatif, kelas, nilai, rangking, waktu) values(NULL, '$iduser', '".$idalter[$x]['id_alternatif']."', '$kelas', '$arr_nf[$x]', '$rangkingnf[$x]', now());") or die(mysql_error()); };
};
print_r($jumlah_atlet);
?>
<html>
<head>
<title> insert data array using only button </title>
</head>
<body>
<form action="" method="post">
<input name="submit" type="button" value="Print & Save" onClick="" />
</form>
</body>
<?php
$data = mysql_query("select * from tb_histori");
$no = 1;
while($d = mysql_fetch_array($data)){
?>
<tr>
<td><?php echo $no++; ?></td>
<td><?php echo $d['id_user']; ?></td>
<td><?php echo $d['id_alternatif']; ?></td>
<td><?php echo $d['kelas']; ?></td>
<td><?php echo $d['nilai']; ?></td>
<td><?php echo $d['rangking']; ?></td>
<td><?php echo $d['waktu']; ?></td>
</tr>
<br>
<?php } ?>
<br>
</html>

Related

How to fix between date to date error in php

When I try to Search Between two dates all are working display on reports-details.php but When I try to fresh reload on reports-details.php show this error Warning:
Invalid argument supplied for foreach() in C:\xampp\htdocs\parking
reservation\admin\reports-details.php on line 52
reports.php content is:
<form action="reports-details.php" method="POST">
<label>From Date</label>
<input type="date" name="start_date">
<label>To Date</label>
<input type="date" name="end_date">
<button name="search">Search</button>
</form>
back-end-reports.php
<?php
include 'config.php';
class report extends Connection{
public function managereport(){
if (isset($_POST['search'])) {
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];
$sqlselect = "SELECT * FROM tbl_customers WHERE test_date BETWEEN '$start_date' AND '$end_date' ORDER BY test_date";
$result = $this->conn()->query($sqlselect);
$result->execute();
return $result->fetchAll();
}
}
}
$new_vehicle = new report();
$new_vehicle->managereport();
?>
reports-details.php
<?php
include '../back-end/back-end-reports.php';
$result = new report();
$query = $result->managereport();
?>
<?php $id = 1; foreach($query as $row) { ?>
<tbody>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $row['serial']; ?></td>
<td><?php echo $row['fullname']; ?></td>
<td><?php echo $row['num_plate']; ?></td>
<td>View | <a class="text-dark" href="print.php">Print</a></td>
</tr>
</tbody>
<?php $id++; } ?>
Your issue here is the if statement in your function
if (isset($_POST['search'])) {
When you refresh your page there is no post data so the managereport returns nothing. The value of $query is therefore empty and so you cant iterate over it in the foreach loop.
My suggestion would be that your mangerreport() should return an empty array in the situation where there is no post data i.e
public function managereport(){
if (isset($_POST['search'])) {
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];
$sqlselect = "SELECT * FROM tbl_customers WHERE test_date BETWEEN '$start_date' AND '$end_date' ORDER BY test_date";
$result = $this->conn()->query($sqlselect);
$result->execute();
return $result->fetchAll();
} else {
return array();
}
}
<?php $id = 1; foreach($query as $row) { ?>
When using the foreach loop, you must first judge the array $query, otherwise you will be prompted to report an error like this Invalid argument supplied for foreach() , you can add judgment before the loop,
<?php $id = 1; if (is_array($query) && count($query)) {
foreach($query as $row) { ?>
<tbody>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $row['serial']; ?></td>
<td><?php echo $row['fullname']; ?></td>
<td><?php echo $row['num_plate']; ?></td>
<td>View | <a class="text-dark" href="print.php">Print</a></td>
</tr>
</tbody>
<?php $id++; } } ?>

How to only update 1 row instead of all the rows when clicking a button

I have a table containing multiple rows. Behind every row is a button you can click to update a column (baatinn) in the database from 0 to 1. However when you click the button it will update all the rows from 0 to 1 instead of the row you are clicking the button on. How do i make it so it will only update the row you are clicking on
Picture of database:
HTML:
<tr>
<th>Båt ut</th>
<th>Båt inn</th>
<th>Båtnr</th>
<th>Fornavn</th>
<th>Etternavn</th>
<th>Tid</th>
<th>Kr</th>
<th>Edit</th>
</tr>
PHP:
$sql = "SELECT utleid, inntid, baatnr, fornavn, etternavn, tid, kr, baatinn FROM utleie WHERE baatnr LIKE '%$sok%' or fornavn LIKE '%$sok%' or etternavn LIKE '%$sok%' or tid LIKE '%$sok%' ORDER BY id desc";
$result = $conn-> query($sql);
if ($result-> num_rows > 0) {
while ($row = $result-> fetch_assoc()) {
?>
<tr>
<td><?php echo $row["utleid"]; ?></td>
<td><?php echo $row["inntid"]; ?></td>
<td><?php echo $row["baatnr"]; ?></td>
<td><?php echo $row["fornavn"]; ?></td>
<td><?php echo $row["etternavn"]; ?></td>
<td><?php echo $row["tid"]; ?></td>
<td><?php echo $row["kr"]; ?></td>
<td><form method="post" action="innlevering.php">
<button name="edit" value="1">Edit</button>
</form></td>
</tr>
<?php
}
echo "</table>";
} else {
echo "0 results";
}
$conn-> close();
innlevering.php:
<?php
include_once 'dbconnect.php';
if ($_POST['edit']) {
$conn->query("UPDATE utleie SET baatinn=1");
}
?>
To help your injection problem, parameterize. It would be something like this (I use PDO, so you will want to double check):
/functions/getUtleie.php
function getUtleie($so, $conn)
{
$query = $conn->prepare("SELECT utleid, inntid, baatnr, fornavn, etternavn, tid, kr, baatinn FROM utleie WHERE baatnr LIKE ? or fornavn LIKE ? or etternavn LIKE ? or tid LIKE ? ORDER BY id desc");
$so = "%{$so}%";
$query->bind_param('ssss',$so, $so, $so, $so);
$result = $query->execute();
if($result->num_rows == 0)
return [];
while($row = $result->fetch_assoc()) {
$data[] = $row;
}
return $data;
}
Now, when you go to use it, include the function, then the key on the form is to make the id in a hidden field:
# Fetch the data
$result = getUtleie($so, $conn);
# If there are any results
if(!empty($result)): ?>
<table>
<?php foreach($result as $row): ?>
<tr>
<td><?php echo $row["utleid"] ?></td>
<td><?php echo $row["inntid"] ?></td>
<td><?php echo $row["baatnr"] ?></td>
<td><?php echo $row["fornavn"] ?></td>
<td><?php echo $row["etternavn"] ?></td>
<td><?php echo $row["tid"]; ?></td>
<td><?php echo $row["kr"]; ?></td>
<td>
<form method="post" action="innlevering.php">
<input type="hidden" name="action" value="update_utleie" />
<input type="hidden" name="utleid" value="<?php echo $row["utleid"] ?>" />
<input type="text" name="val" />
<input type="submit" value="Edit" />
</form>
</td>
</tr>
<?php endforeach ?>
</table>
<?php else: ?>
0 results
<?php endif ?>
After you submit the form, you will want to update using a WHERE clause:
<?php
include_once 'dbconnect.php';
# Check to make sure the form was submitted
if(!empty($_POST['action'] && $_POST['action'] == 'update_utleie') {
# Trim these. You should also check they aren't empty (especially the id)
$id = trim($_POST['utleid']);
$value = trim($_POST['val']);
$query = $conn->prepare("UPDATE `utleie` SET `baatinn` = ? WHERE `utleid` = ?");
$query->bind_param('si', $value, $id);
$query->execute();
}
Anyway, I haven't checked these scripts but it should be pretty close. Should at least point you in the right direction.

Rearrange table head data to match with table body

I want to arrange the table head with my table body, so the data fill have a same type, how can i doing it??
The black square is the correct one
This is my code
<table>
$syntax = 'SELECT customer,type, count(type) as num FROM view_detail GROUP BY type ORDER BY customer';
$rows = $config->prepare($syntax);
$rows->execute();
$result = $rows->fetchAll(PDO::FETCH_OBJ);
?>
</tr>
<td>No</td>
<td>Customer</td>
<?php for($x=0;$x<count($result);$x++){ ?>
<td><?php echo $result[$x]->type; ?></td>
<?php } ?>
<td>Total</td>
<?php $no = 1;
$query = 'SELECT customer,type, count(type) as tipe FROM view_detail GROUP BY customer ORDER BY customer';
$baris = $config->prepare($query);
$baris->execute();
$hasil1 = $baris->fetchAll(PDO::FETCH_OBJ);
for($i=0;$i<count($hasil1);$i++){
$sql = 'SELECT * FROM view_detail WHERE customer = "'.$hasil1[$i]->customer.'" ORDER BY customer';
$row = $config->prepare($sql);
$row->execute();
$hasil = $row->fetchAll(PDO::FETCH_OBJ);
?>
<tr>
<td><?php echo $no; $no++;?></td>
<td><?php echo $hasil[$i]->customer; ?></td>
<?php
for($y=0;$y<count($result);$y++){
if($y < count($hasil)){
?>
<td><?php echo $hasil[$y]->type; ?></td>
<?php }else{ ?>
<td>0</td>
<?php } } }?>
</tr>
</table>
Thanks
I want to make it like this
Edit:
Dynatrade -> MFN200, N50Z, and N70
Super Bloom -> N70, MFN100, MFN120, and MFN150

Storing multiple value in database using checkbox

I want to store both $row_tbl['Course_ID'] and $row_tbl['Section'] value in checkbox name variable. Basically I want to store course id and section in database by checking that field.
What can I do?
<?php
if (isset($_POST["sub"])) {
$values = $_POST['check'];
$valuesArr = array();
foreach ($values as $a){
$valuesArr[] = mysql_real_escape_string($a);
}
$max = sizeof($values);
for ($i = 0; $i < $max ; $i++) {
$query2 = "INSERT INTO course_reg (stu_id,course_id,section,semester,grade,status) VALUES ('$sesid','$valuesArr[$i]','????','Summer 2016','Z','a')";
$reg = mysqli_query($con, $query2);
}
}
?>
<?php while($row_tbl = mysqli_fetch_array($query)): ?>
<tr class="success" style="font-size:12px;">
<td>
<input type="checkbox" name="check[]" class="chk_val" value="<?php echo $row_tbl['Course_ID'] ?>" id="in" onclick="test()" />
</td>
<td><?php echo $row_tbl['Course_ID'] ?></td>
<td><?php echo $row_tbl['Course_Title'] ?></td>
<td><?php echo $row_tbl['Section'] ?></td>
<td><?php echo $row_tbl['Time'] ?></td>
<td><?php echo $row_tbl['Day'] ?></td>
<td><?php echo $row_tbl['Dept'] ?></td>
<td><?php echo $row_tbl['Capacity'] ?></td>
</tr>
<?php endwhile; ?>
You need to maintain index value of each checkbox for getting checked checkbox values check below ex:
$index = 0;
while($row_tbl = mysqli_fetch_array($query))
{
<input type="checkbox"
name="<?php echo 'check['.$index.']['.$row_tbl['Course_ID']; ?> ]"
value=" <?php echo $row_tbl['Section'] ?> "/>
<?php echo $row_tbl['Section'] ?>
$index++;
}
At Php server page loop through index and call insert into DB logic

search multiple fields from a single table in php mysql

I have a table named current with several fields in that table like post, experience, company, etc.
I want to make a search having three select boxes one showing the posts from post field, 2nd showing the experience and 3rd showing the company from a single current table.
I want that if user selects from any one select field or from all it should search the respective data from the table and show the results only related to search.
For this I have written the code but its not working please help me in finding where did I went wrong, I cant understand.
It only shows data entered in post field but not in any other field.
My code goes here
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="">
<table>
<tr>
<?php
$ss=mysql_query("select * from current");
?>
<td><select name="post"><?php while($rr=mysql_fetch_array($ss))
{?><option value="<?php echo $rr['post'];?>"><?php echo $rr['post'];?></option><?php } ?></select></td>
<?php
$s11=mysql_query("select * from current");
?>
<td><select name="experience"><?php while($r11=mysql_fetch_array($s11))
{?><option value="<?php echo $r11['experience'];?>"><?php echo $r11['experience'];?></option><?php } ?> </select></td>
<td>
<?php
$sss=mysql_query("select * from current");
?>
<select name="company"><?php while($rrr=mysql_fetch_array($sss))
{?><option value="<?php echo $rrr['cname'];?>"><?php echo $rrr['cname'];?></option><?php } ?></select></td>
<td><input type="submit" name="search" value="Search" /></td>
</tr>
</table>
</form>
my search code goes here
<?php
include('Admin/config.php');
error_reporting(E_ERROR | E_WARNING | E_PARSE);
if(isset($_REQUEST['search']))
{
$post = $_REQUEST['post'];
$ex = $_REQUEST['experience'];
$company = $_REQUEST['company'];
$query = "select * from current where post like '%$post%' or experience like '%$ex%' or cname like '%$company%' ";
$res1 = mysql_query($query);
while($rows = mysql_fetch_array($res1))
{
?>
<tr>
<td><?php echo $rows['date'];?></td>
<td><?php echo $rows['post'];?></td>
<td><?php echo $rows['qualification'];?></td>
<td><?php echo $rows['experience'];?></td>
<td><?php echo $rows['nop'];?></td>
<td><?php echo $rows['noj'];?></td>
<td><?php echo $rows['cname'];?></td>
<td><?php echo $rows['jloc'];?></td>
</tr><?php
}}
else
{
$s=mysql_query("SELECT * FROM current ORDER BY date DESC");
while($rows=mysql_fetch_array($s))
{
?>
<tr>
<td><?php echo $rows['date'];?></td>
<td><?php echo $rows['post'];?></td>
<td><?php echo $rows['qualification'];?></td>
<td><?php echo $rows['experience'];?></td>
<td><?php echo $rows['nop'];?></td>
<td><?php echo $rows['noj'];?></td>
<td><?php echo $rows['cname'];?></td>
<td><?php echo $rows['jloc'];?></td>
</tr>
<?php
}}
?>
</table>
<?php
include('Admin/config.php');
error_reporting(E_ERROR | E_WARNING | E_PARSE);
$ss=mysql_query("select `post`,`experience`,`cname` from current");
$filter = array();
while($rr = mysql_fetch_assoc($ss)) {
foreach ($rr as $key => $val) {
$filter[$key][] = $val;
}
}
$request = array('post' => '', 'experience' => '', 'cname' => '');
if (isset($_REQUEST['search']))
{
$request = array(
'post' => $_REQUEST['post'],
'experience' => $_REQUEST['experience'],
'cname' => $_REQUEST['cname'],
);
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="">
<table>
<tr>
<?php
foreach ($filter as $name => $value) {
?>
<td>
<select name="<?=$name?>">
<?
foreach ($value as $key => $val) {
?>
<option value="<?=$val?>"<?= $val === $request[$name] ? ' selected' : ''?>><?=$val?></option>
<?
}
?>
</select>
</td>
<?
}
?>
<td><input type="submit" name="search" value="Search" /></td>
</tr>
</table>
</form>
<table>
<?php
if(isset($_REQUEST['search']))
{
$where = '';
foreach ($request as $key => $val) {
if ($where !== '') {
$where .= ' OR ';
}
$where .= "`".$key."` like '%".$val."%' ";
}
$query = "select * from current where ".$where;
}
else
{
$query = "select * from current ORDER BY date DESC";
}
$res1 = mysql_query($query);
while($rows=mysql_fetch_array($res1))
{
?>
<tr>
<td><?php echo $rows['date'];?></td>
<td><?php echo $rows['post'];?></td>
<td><?php echo $rows['qualification'];?></td>
<td><?php echo $rows['experience'];?></td>
<td><?php echo $rows['nop'];?></td>
<td><?php echo $rows['noj'];?></td>
<td><?php echo $rows['cname'];?></td>
<td><?php echo $rows['jloc'];?></td>
</tr>
<?php
}
?>
</table>

Categories