Undefined Index & Update Query - php

I am trying to update mysql data from user input displayed on a table, i have used the following code, even though the name is set and the value is set when i click update the code states "Undefined index: LHA" but as you can see i have declared this along with Work_date, LHA, AAS, LSA, AAH (the fields throwing the undefiend index errors) I also want the unefcted data to remain the same in the mysql database as apposed to settting it to blank! This is the HTML (A while statement to fetch the data to populate the value fields in the html table)
while($row = $sth->fetch()) { ?>
<form action='../action.php' method='post'>
<tr>
<td> <?php echo $row['Name'] ?></td>
<td> <input name='car' id='car' value="vv1"><br/><br/></td>
<td> <select name='Night' id='Night'><option selected='selected' value='no'>No</option><option value='Yes'>Yes</option></select> <br/><br/></td>
<td> <input class='text' name="Siteid" type="text" value="<?php echo $row['Workbook ID']; ?>" /> <br/><br/></td>
<td> <input class='datepicker' name="Work_date" type="text" value="<?php echo date('l, F d, Y', strtotime($row['Work Date'])) ?>" placeholder="Select Date!"/><br/><br/></td>
<td> <input class='timepicker' name="LHA" type="text" value="<?php echo $row['LHA'] ?>" /><br/><br/></td>
<td> <input class='timepicker' name="AAS" type="text" value="<?php echo $row['AAS'] ?>" /><br/><br/></td>
<td> <input class='timepicker' name="LSA" type="text" value="<?php echo $row['LS'] ?>" /><br/><br/></td>
<td> <input class='timepicker' name="AAH" type="text" value="<?php echo $row['AAH'] ?>" /><br/><br/></td>
<td> <input name="Bonus" type="text" value="<?php echo $row['Bonus']; ?>" /> <br/><br/></td>
<td> <input name="Work_Completed" type="text" value="<?php echo $row['Work Completed']; ?>" /> <br/><br/></td>
<td> <input name="BH" type="text" value="<?php echo $row['Hours']; ?>" /><br/><br/></td>
<input type='hidden' name='PID' value="<?php echo $row['PID'];?>" />
<td><input type='submit' name='delete_timesheet' value='delete' /></td>
<td><input type='submit' class='btn btn-default' name='update_timesheet' value='Update' /></td>
</tr>
</form>
<?php
}
Just to mention my query and field settings work perfectly on XAMPP but not when online, this is my PHP
if(isset($_POST['update_timesheet'])) {
$a = $_POST['car'];
$b = $_POST['Night'];
$c = $_POST['Siteid'];
$d = $_POST['Work_date'];
$e = $_POST['LHA'];
$f = $_POST['AAS'];
$g = $_POST['LSA'];
$h = $_POST['AAH'];
$i = $_POST['Bonus'];
$j = $_POST['BH'];
$l = $_POST['PID'];
$Paid = create_paid($e, $f, $g, $h);
$stmt = "UPDATE data SET Car = ?, Night = ?, `Workbook ID` = ?, `Work Date` = ?, LHA = ?, AAS = ? , LS = ? , AAH = ? , Bonus = ?, `Work Completed` = ?, Hours = ? WHERE PID = ? ";
$y = $pdo->prepare($stmt);
$y->execute(array($a,$b, $c, $d, $e, $f, $g, $h, $i, $j, $Paid, $l));
header('Location: ' . $_SERVER['HTTP_REFERER']);
}

Related

Undefined Variable Warning in PHP

So I've been doing this school project and need to make it so I can edit whatever is in the table. Whenever i click on "Edit" it redirects me correctly but in the form it says there is an undefined variable eventhough that variable is used pretty much everywhere.
Here is some code of the table:
<table style='margin-left:auto ; margin-right:auto;'>
<tr>
<th>#</th>
<th>Name</th>
<th>Zeit</th>
<th>Datum</th>
<th>Titel</th>
<th>Inhalt</th>
<th>Ort</th>
</tr>
<?php
if($stmt=$db->prepare("SELECT * FROM terminkalender")) {
$stmt->execute();
$stmt->store_result();
$zeilen = $stmt->num_rows();
$stmt->close();
}else {
$zeilen = 0;
}
if($zeilen > 0) {
//nur wenn Einträge, dann ausgeben
if($stmt = $db->prepare("SELECT * FROM terminkalender ORDER BY zeit,datum DESC")) {
$stmt->execute();
$stmt->bind_result($id,$name,$zeit,$datum,$ort,$titel,$inhalt);
$stmt->store_result();
//Ausgabe starten
while($stmt->fetch()){
echo "<tr>";
?>
<td><?php echo $id ;?></td>
<td><?php echo htmlspecialchars($name) ;?></td>
<td><?php echo htmlspecialchars($datum) ;?></td>
<td><?php echo htmlspecialchars($zeit) ;?></td>
<td><?php echo htmlspecialchars($ort) ;?></td>
<td><?php echo htmlspecialchars($titel) ;?></td>
<td><?php echo htmlspecialchars($inhalt); ?></td>
<td><a href='edit.php?id=<?php echo $id;?>'>Edit</a></td>
<td><a href='delete.php?id=<?php echo $id;?>'>Delete</a></td>
<?php
echo "</tr>" ;
}
}
}
?>
</table>
and here for the edit.php file:
<?php
include("./config/connect.inc.php");
$id = $_GET['id']; // get id through get string
$result=mysqli_query($db,"SELECT * FROM terminkalender WHERE id=$id");
if(isset($_POST['update'])) {
$name=$_POST['name'];
$datum=$_POST['datum'];
$zeit=$_POST['zeit'];
$ort=$_POST['ort'];
$titel=$_POST['titel'];
$inhalt=$_POST['inhalt'];
$result = "UPDATE terminkalender
SET name='$name',
datum='$datum',
zeit='$zeit',
ort='$ort',
titel='$titel',
inhalt='$inhalt'
WHERE id=$id";
header("location: ausgabe.php");
}
?>
<form name="form" method="POST" action="edit.php">
<input type="text" name="name" value="<?php echo $name; ?>" Required>
<input type="date" name="datum" value="<?php echo $datum; ?>" Required>
<input type="time" name="zeit" value="<?php echo $zeit; ?>" Required>
<input type="text" name="ort" value="<?php echo $ort; ?>" Required>
<input type="text" name="titel" value="<?php echo $titel; ?>" Required>
<input type="text" name="inhalt" value="<?php echo $inhalt; ?>" Required>
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">
<input type="submit" name="update" value="Update">
</form>
Ẁould be really awesome if anyone could help. Thanks is advance!
Maybe you can try using isset function of php to check if that variable contains a value or not for more details check:-https://www.stechies.com/notice-undefined-variable-in-php/

How to disable button when returned books is equal with original books volume?

I am making a book management system.I have a return button ,with that button user can return books.
Here is my release-books.php:
<?php
$sql_select = "SELECT * FROM carti ORDER BY titlu";
$rezultat = mysqli_query($conn, $sql_select);
if (mysqli_num_rows($rezultat) > 0) {
while ($row = mysqli_fetch_assoc($rezultat)) {
$disabled = $row['stoc'] > 0 ? "" : "disabled"; ?>
<tr><td><input type="submit" name="id" value="<?php echo $row['idCarte']; ?>" <?php echo $disabled; ?> formaction="imprumutare.php"></input></td>
<td><input type="submit" name="returnare" value="<?php echo $row['idCarte']; ?>" formaction="returnare_carte.php"></input>
</td>
<td><input type="text" name="nume" value="<?php echo $row['titlu']; ?>" ></input></td>
<td><input type="text" name="" value="<?php echo $row['autor']; ?>"></input></td>
<td><input type="text" name="" value="<?php echo $row['editura']; ?>"></input></td>
<td><input type="text" name="" value="<?php echo $row['categorie']; ?>"></input></td>
<td><input type="text" name="" value="<?php echo $row['data_adaugarii']; ?>"></input></td>
<td><input type="text" name="" value="<?php echo $row['stoc']; ?>"></input></td>
</tr>
<?php
}
}
?>
And here is my borrowing.php
include('conexiune.php');
//sfarsit if
//Imprumutare
if (isset($_POST['id'])) {
$identificator = $_POST['id'];
$carte_nume = $_POST['nume'];
$current_date = date('y:m:d');
$current_date_plus_14days = date('y:m:d', strtotime("+14 days"));
$nume_carte = $_POST['nume'];
$insert_in_imprumuturi = "INSERT INTO imprumuturi(idc,nume_carte,data,termen_returnare,carti_imprumutate) VALUES('$identificator','$carte_nume','$current_date','$current_date_plus_14days','1')";
mysqli_query($conn, $insert_in_imprumuturi) or die(mysqli_error($conn));
$sql_rezervare = "UPDATE carti SET stoc=stoc-1 WHERE iDCarte='$identificator' ";
if (mysqli_query($conn, $sql_rezervare)) {
header('Refresh:0,url=emitere_carti.php');
} else {
die(mysqli_error($conn));
}
}
But I do not know how to disable the return button when the returned books volume is equal or greater that the originial volume
Can somone help me?
Check If your books volume is equal or greater than original volume, if it is. Use PHP echo to disable the Return Button.
<button type="submit" name="return"
<?php
if($returned_books_volume >= $original_books_volume){
echo 'disabled';
}
?>
>Return</button>

MySQL Insert only last row with while loop

I have a while loop. It is within a form.I want to insert all rows from while loop when submitting submit button. Below is the code of while loop.
<form method="POST" action="insert.php" n>
<table>
<?php
$i =0;
$link = mysqli_connect('localhost', 'root', '', 'shibu');
$sql = "select `cus_id`, `mobile`, `date`, `time`,
sum(`d1` + `d2` + `d3`) as `credit`,
from `finale`;
$result=mysqli_query($link, $sql);
$count = mysqli_num_rows($result);
if($count == 0){
?>
<tr>
<th><?php echo 'No Records Founds!' .mysqli_error($link); ?></th>
</tr>
<?php
}else{
while($sql_result = mysqli_fetch_assoc($result)){
?>
<tr>
<td><?php echo $i += 1; ?></td>
<td><input type="text" name="cus_id" value="<?php echo $sql_result['cus_id']; ?>" ></td>
<td><input type="text" name="mobile" value="<?php echo $sql_result['mobile']; ?>" ></td>
<td><input type="text" name="date" value="<?php echo $sql_result['date']; ?>" ></td>
<td><input type="text" name="time" value="<?php echo $sql_result['time']; ?>"</td>
<td><input type="text" name="credit" value="<?php echo $sql_result['credit']; ?>"></td>
</tr>
<?php
}
}
?>
</table>
<input name="submit" type="submit" />
</form>
This is my insert query.
<?php
$link = mysqli_connect('localhost', 'root', '', 'shibu');
$stmt = mysqli_prepare($link, "INSERT INTO single
(`cus_id`, `mobile`, `date`, `time`, `credit`) VALUES (?,?,?,?,?)");
mysqli_stmt_bind_param($stmt, 'isssi', $cus_id, $mobile, $date, $time, $credit);
foreach ((array)$_POST['cus_id'] as $i => $cus_id) {
$mobile = $_POST['mobile'];
$date = $_POST['date'];
$time = $_POST['time'];
$credit = $_POST['credit'];
mysqli_stmt_execute($stmt);
}
if(!$stmt){
echo "error". mysqli_error($link);
}
My problem is when I enter submit button, only last row enters not the whole rows.
I need your help heartily.
In your html code you need to have input names defined as array.
Like
<td><input type="text" name="cus_id[]" value="<?php echo $sql_result['cus_id']; ?>" ></td>
<td><input type="text" name="mobile[]" value="<?php echo $sql_result['mobile']; ?>" ></td>
<td><input type="text" name="date[]" value="<?php echo $sql_result['date']; ?>" ></td>
<td><input type="text" name="time[]" value="<?php echo $sql_result['time']; ?>"</td>
<td><input type="text" name="credit[]" value="<?php echo $sql_result['credit']; ?>"></td>
And while using them in php you need to loop them and run insert query
foreach ($_POST['cus_id'] as $i => $value) {
$cus_id = $_POST['cus_id'][$i];
$mobile = $_POST['mobile'][$i];
$date = $_POST['date'][$i];
$time = $_POST['time'][$i];
$credit = $_POST['credit'][$i];
// Yourinsert query
}
Please move your
mysqli_stmt_bind_param
line into foreach loop, just above
mysqli_stmt_execute
line.

Show data with the same name only once

I need to show my billing history, so user can automatically submit by clicking submit. But instead of showing all transactions, I only have to show for each billing_name, so I should use SELECT DISTINCT right?
<?php
$query3 = "SELECT DISTINCT billing_name, billing_reference_num FROM billing_history";
$result3 = mysqli_query($link, $query3) or die(mysqli_error($link));
while ($row3 = mysqli_fetch_array($result3)) {
$billing_name = $row3['billing_name'];
$billing_reference_num = $row3['billing_reference_num'];
?>
<td><?php echo $billing_name ?></td>
<td><?php echo $billing_reference_num ?></td>
<?php
$queryBill = "SELECT * FROM billing_history WHERE billing_name = '$billing_name'";
$resultBill = mysqli_query($link, $queryBill) or die(mysqli_error($link));
while ($row4 = mysqli_fetch_array($resultBill)) {
$billing_name_id = $row4['billing_name_id'];
$account_id = $row4['account_id'];
$account_number = $row4['account_number'];
}
?>
<td><input type="text" name="transaction_amount"/></td>
<td><input type="submit" value="Submit"/></td>
<input type="hidden" name="billing_name_id" value="<?php echo $billing_name_id ?>"/>
<input type="hidden" name="billing_name" value="<?php echo $billing_name ?>"/>
<input type="hidden" name="billing_reference_num" value="<?php echo $billing_reference_num ?>"/>
<input type="hidden" name="account_number" value="<?php echo $account_number ?>"/>
<input type="hidden" name="account_id" value="<?php echo $account_id ?>"/>

saving data from an array without submitting any form php

I'm tryng to save an array so I have the following code:
<?php
$sql = "SELECT * FROM scenarii where code_s='".mysql_real_escape_string($_POST['code_s'])."'";
$qry = mysql_query($sql) or die(__LINE__.mysql_error().$sql);
$i = -1; // index des enregistrements
?>
<table cellpadding="5" cellspacing="5">
<tr>
<td><strong>CODE SCENARIO</strong></td>
<td><strong>LIBELLE</strong></td>
<td><strong>ACTION</strong></td>
<td><strong>DESCRIPTION</strong></td>
<td><strong>DATE</strong></td>
</tr>
<form action="<?php echo (isset($_POST['go'])) ? 'go.php' : '#'; ?>" method="post">
<input type="hidden" name="liasse" value="<?php echo $_POST['liasse']; ?>"/>
<input type="hidden" name="n_doss" value="<?php echo $_POST['n_doss']; ?>"/>
<input type="hidden" name="qualite" value="<?php echo $_POST['qualite']; ?>"/>
<?php while($row = mysql_fetch_assoc($qry)): ?>
<tr>
<td><input name="data[<?php echo ++$i; ?>][code_s]" type="text" value="<?php echo $row['code_s'];?>" size="10"></td>
<td><input name="data[<?php echo $i; ?>][titre]" type="text" value="<?php echo $row['titre']; ?>" size="45"></td>
<td><input name="data[<?php echo $i; ?>][action]" type="text" value="<?php echo $row['action']; ?>" size="15"></td>
<td><input name="data[<?php echo $i; ?>][libelle]" type="text" value="<?php echo $row['libelle']; ?>" size="55"></td>
<td><input type="text" name="data[<?php echo $i; ?>][date]" value="<?php echo $get_date($row['jour']) ; ?>" size="12"></td>
</tr>
<?php endwhile; ?>
And in order to save this I have this code:
if (isset($_POST['liasse'])) {
$value = $_POST['data'] ;
foreach($value as $key => $array)
{
$sql = 'INSERT INTO agenda SET
liasse = "'.mysql_real_escape_string($_POST['liasse']).'",
code_s = "'.mysql_real_escape_string($array['code_s']).'",
date_action = "'.date('Y-m-d',strtotime($array['date'])).'",
libelle = "'.mysql_real_escape_string($array['titre']).'",
action = "'.mysql_real_escape_string($array['action']).'",
description = "'.mysql_real_escape_string($array['libelle']).'",
n_doss = "'.mysql_real_escape_string($_POST['n_doss']).'",
qualite = "'.mysql_real_escape_string($_POST['qualite']).'"
';
mysql_query($sql) or die(__LINE__.mysql_error().$sql);
}
But I'm really lost,
In fact I use a form for that, now I would like to submit all of this data but without any form, directly when I have the first while I would like to save it.
The thing is that I'm lost because I can not call any var like that data[][code_s].
So I do not know how to save this. I would like to save it in background, and not to display that something has been saved.
Receive all my Utmost Respect
kind regards,
SP.
Wrap the code od the lower code block into a function and hand over the value array as argument:
function storeValues ($data) {
foreach($data as $key => $val)
{
$catalog=sprintf("%s='%s'",$key,$val);
$sql = sprintf('INSERT INTO agenda SET %s', implode(',',$catalog));
mysql_query($sql) or die(__LINE__.mysql_error().$sql);
} // foreach
} // function storeValues
You call this function when you want to save the values. So after retrieving them from the database. You call it for each row you retrieve and hand over the values like that:
storeValues ($row);
This will store one row of values at a time. Obviously this can be optimized to use a multiple insert. But let's take one step after another...

Categories