-2

save data that an error occurred, but the data is still stored. how do you get rid of these errors?

$query1 = "SELECT * FROM t_ref_pokja WHERE status='1' ORDER BY id"; 
$hasil1 = mysqli_query($koneksi,$query1);
if ($num_rows1 = mysqli_num_rows($hasil1)) {
while($list1 = mysqli_fetch_array($hasil1)){

$no = $list1['id'];
$a= "wisuda_$no";
$b= "RT_$no";
$c= "OPR_$no";
$d= "PI_$no";
$e= "Strat_$no";
$f= "pagu_$no";
$g= "save_$no";

$nama=$_POST[$f];
$wisuda1 = $_POST[$a];
$wisuda = str_replace(",", "",$wisuda1);
$rt1 = $_POST[$b]; 
$rt = str_replace(",", "",$rt1);
$opr1 = $_POST[$c]; 
$opr = str_replace(",", "",$opr1);
$pi1 = $_POST[$d];
$pi = str_replace(",", "",$pi1);
$strat1 = $_POST[$e];
$strat = str_replace(",", "",$strat1);
$save = $_POST[$g];                    //line 66
$total =($wisuda+$rt+$opr+$pi+$strat); //line 67
$tahun = date("Y");
if($save == "save"){

enter image description here

please help me

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Check the values before using them. You also could just use `$_POST = str_replace(',', '', $_POST)` then all your commas will be removed. – user3783243 Apr 08 '20 at 02:45

1 Answers1

0

First, these are notices and warnings rather than errors. As you have seen, code execution will continue in these scenarios whereas script execution would terminate in case of an error.

There are at least two different issues here:

  1. You are accessing an array index that does not exist(line 66)
  2. You are using arithmetic operators on non-numeric values(line 67)

For 1, you should check that the key exists in the $_POST array before accessing it. I am assuming since it is absent in the request, the "save" parameter is optional. Since it is not present, null will be assigned to $save, so presumably this is the desired value for later logic. You can simply add a condition using array_key_exists or isset to prevent the notice. If you are you using php 7 or later you can use the "null coalesce" operator(??).

$save = array_key_exists($g, $_POST)?$_POST[$g]:null;

OR

$save = $_POST[$g]??null;

For 2, I would need more information about the values and intentions thereof. Judging by the addition operator and comma replacement, it appears that you are accepting numeric values that are formatted in some way. PHP will implicitly convert numeric strings to numbers in cases such as this. You may need to remove further formatting. Perhaps intval or floatval would be applicable depending on the input data. Ultimately, you need to extract the numbers that you need from the strings and convert them to numeric values before performing addition(unless you are attempting string concatenation, in which case see the . operator)

akenion
  • 805
  • 1
  • 5
  • 9