Unable to UPDATE data in MySQL using php any suggestions? - php

I am building php mysql based simple inventory management system.
Also, I have no formal knowledge about the things or assigned names like arrays, functions, etc, but I can understand.
Here is the issue, I am facing right now where needs your guidance/assistance:
As far as my knowledge, my code has to work.
I like to update my products row but can't
Affecting 0 rows using these queries:
if (isset( $_POST['prod_edit'] )) {
$prodId = $_POST['prodId'];
$pUpdate = $con->prepare("UPDATE products SET prodTeng = ? , prodTurd = ? , prodSKU = ? , prodBC = ? , prodPUPP = ? , prodPUSP = ? , prodCate = ? , prodQuan = ? WHERE prodId = ? ");
$pUpdate->bind_param("ssssssssi", $_POST['prodTeng'], $_POST['prodTurd'], $_POST['prodSKU'], $_POST['prodBC'], $_POST['prodPUPP'], $_POST['prodPUSP'], $_POST['prodCate'], $_POST['prodQuan'], $prodId);
$pUpdate->execute();
if ($pUpdate->affected_rows === 0) {
echo'<script> window.location.replace("home.php?p=inventory&alert=0"); </script>';
}
if ($pUpdate->affected_rows === 1) {
echo'<script> window.location.replace("home.php?p=inventory&alert=3"); </script>';
}
$pUpdate->close();
}
FORM:
<?php
$products_list = $con->query("SELECT * FROM products WHERE cid = '$cid' ");
while($row = $products_list->fetch_assoc()) {
$prodCate = $row['prodCate'];
$category_list = $con->query("SELECT * FROM categories WHERE cateId = '$prodCate' ");
while($cate = $category_list->fetch_assoc()) {
?>
<div class="modal fade modal-right" id="editProductRight<?=$row['prodId']; ?>" tabindex="-1" role="dialog" aria-labelledby="editProductRight<?=$row['prodId']; ?>" style="display: none;" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Edit Product Details</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action="home.php?p=inventory" method="post">
<div class="modal-body">
<div class="form-group mb-2">
<input type="text" class="form-control" name="prodTeng" placeholder="Name in English" required value="<?=$row['prodTeng']; ?>">
</div>
<div class="form-group mb-2">
<input type="text" class="form-control" name="prodTurd" placeholder="اردو میں نام" dir="rtl" value="<?=$row['prodTurd']; ?>">
</div>
<div class="form-group mb-2">
<input type="text" class="form-control" name="prodSKU" placeholder="SKU" required value="<?=$row['prodSKU']; ?>">
</div>
<div class="form-group mb-2">
<input type="text" class="form-control" name="prodBC" placeholder="Bar Code" value="<?=$row['prodBC']; ?>">
</div>
<div class="input-group mb-2 mr-sm-2">
<div class="input-group-prepend">
<div class="input-group-text">PKR</div>
</div>
<input type="number" class="form-control" name="prodPUPP" id="inlineFormInputGroupUsername2" placeholder="Per Unit Purchase Price" value="<?=$row['prodPUPP']; ?>">
</div>
<div class="input-group mb-2 mr-sm-2">
<div class="input-group-prepend">
<div class="input-group-text">PKR</div>
</div>
<input type="number" class="form-control" name="prodPUSP" id="inlineFormInputGroupUsername2" placeholder="Per Unit Sell Price" value="<?=$row['prodPUSP']; ?>">
</div>
<div class="form-group mb-2">
<input type="number" class="form-control" name="prodQuan" placeholder="On-Hand Quantity" value="<?=$row['prodQuan']; ?>">
</div>
<div class="form-group">
<select class="form-control" name="prodCate" required>
<option value="<?=$cate['cateId']; ?>"><?=$cate['cateTeng']; ?> | <?=$cate['cateTurd']; ?></option>
<?php
$stmt = $con->query("SELECT * FROM categories WHERE cid = '$cid' ");
while($row = $stmt->fetch_assoc()) {
?>
<option value="<?=$row['cateId']; ?>"><?=$row['cateTeng']; ?> | <?=$row['cateTurd']; ?></option>
<?php
}
$stmt->close();
?>
</select>
</div>
</div>
<hidden style="visibility:hidden;"><input type="text" value="<?=$row['prodId']?>" name="prodId" ></hidden>
<div class="modal-footer">
<button type="button" class="btn btn-outline-primary" data-dismiss="modal">Cancel</button>
<button type="submit" name="prod_edit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
</div>
<?php
}
$category_list->close();
}
$products_list->close();
?>
If there is any suggestion or other way to do that, then please guide!

I did use print_r($_POST); and found that the prodId was in the end as shown in the question.
Then I just changed the position/location of prodId <input> tag at the top & It Works (-_-).
Results of prodId <input> tag in the end was:
Array ( [prodTeng] => asdasd [prodTurd] => Aasd [prodSKU] => asd [prodBC] => [prodPUPP] => 3 [prodPUSP] => 3 [prodQuan] => 2 [prodCate] => 1 [prodId] => 3 [prod_edit] => )
Which end up effecting 0 rows
Results of prodId tag at top was:
Array ( [prodId] => 3 [prodTeng] => asdasd [prodTurd] => Aasd [prodSKU] => asd [prodBC] => [prodPUPP] => 3 [prodPUSP] => 3 [prodQuan] => 2 [prodCate] => 1 [prodId] => 3 [prod_edit] => )
Which successfully effecting rows.

Related

Handling multiple dynamic forms (not inputs) in one view - Laravel

I have a page with a form that has dynamic inputs, where the user can add and "remove" lines. (By "removes," I mean that it removes from the front end but doesn't actually remove from the database.) The functionality works, as far as updating existing rows and adding new rows. The problem I'm having is with deleting rows.
I'd like to have a modal popup that confirms that they want to delete the row before it deletes it. My first thought was to loop through the existing rows and create a corresponding modal per row with a form, but I'm stuck on setting up the controller to recognize which form/row it's looking to delete - so to recap there's going to be one form with dynamic fields (this is the one that works right now), and there's going to be multiple dynamic forms for deleting rows.
Here's my HTML:
<form action="{{route('preliminary-children.updateChildren')}}" method="POST">
#csrf
<!-- Content Row -->
<div class="row" id="childInfo">
<!-- About Process -->
<div class="col-xl-12 col-lg-12">
<div class="card shadow mb-4">
<!-- Card Header - Dropdown -->
<div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
<h6 class="m-0 font-weight-bold text-secondary">Family Information - Children</h6>
</div>
<div id="dynamic_field">
#foreach($children as $i => $child)
<div class="card-body" id="row{{$i+1}}">
#if ($i != 0)
<hr/>
<div class="text-right px-2 my-2 child-{{$i+1}}">
<a href="#" class="text-decoration-none" data-toggle="modal" data-target="#remove{{$i+1}}">
<button type="button" class="px-0 btn btn-circle btn-danger">
<i class="fal fa-times"></i>
</button>
</a>
</div>
<div class="modal fade" id="remove{{$i+1}}" tabindex="-1" role="dialog" aria-labelledby="label{{$i+1}}" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="label{{$i+1}}">Delete Row</h5>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">Are you sure you want to remove this row?</div>
<div class="modal-footer">
<button class="btn bg-gray-300" type="button" data-dismiss="modal">Cancel</button>
<form id="remove-form{{$i+1}}" name="formrow[]" action="{{route('preliminary-children.updateChildren')}}" method="POST" class="d-inline-block">
<input type="text" name="removeID[]" id="removeID{{$i+1}}" value="{{$child->id}}" hidden="hidden">
<button type="submit" id="{{$i+1}}" name="removerow[]" class="btn btn-danger text-decoration-none btn_remove">Remove</button>
</form>
</div>
</div>
</div>
</div>
#endif
<input type="text" name="userID[]" id="userID{{$i+1}}" value="{{ $user->id }}" hidden="hidden">
<input type="text" name="ID[]" id="childID{{$i+1}}" value="{{$child->id}}" hidden="hidden">
<input type="text" name="rel[]" id="rel{{$i+1}}" value="child" hidden="hidden">
<div class="form-row">
<div class="form-group col-md-6">
<label for="first">First Name</label>
<input name="first[]" type="text" class="form-control" id="first{{$i+1}}" value="{{$child->first_name}}" placeholder="First Name">
</div>
<div class="form-group col-md-6">
<label for="last">Last Name</label>
<input name="last[]" type="text" class="form-control" id="last{{$i+1}}" value="{{$child->last_name}}" placeholder="Last Name">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputSSN">SSN</label>
<input data-inputmask="'mask': '999-99-9999'" value="{{$child->ssn}}" type="text" class="form-control" name="inputSSN[]" id="SSN{{$i+1}}" placeholder="SSN">
</div>
<div class="form-group col-md-6">
<label for="dob">Date of Birth</label>
<input data-inputmask="'mask': '99/99/9999'" type="datepicker" value="{{$child->dob}}" class="form-control" name="dob[]" id="do{{$i+1}}B" placeholder="Date of Birth">
</div>
</div>
<div class="form-row">
<div class="form-group col-md">
<label for="inputGender">Gender</label>
<br/>
<select class="form-control d-inline-block w-25 mr-1" name="inputGender[]" id="Gender{{$i+1}}" placeholder="Gender">
#foreach($genders as $gender)
<option value="{{$gender}}" #if($child){{ ($child->gender == $gender) ? 'selected' : ''}} #endif>{{$gender}}</option>
#endforeach
</select>
</div>
<div class="form-group col-md">
<label for="primaryHeight">Height</label>
<br/>
<select class="form-control d-inline-block w-25 mr-1" name="primaryFeet[]" id="primaryFeet{{$i+1}}">
#foreach($feet as $foot)
<option value="{{$foot}}" #if($child){{($child->height_feet == $foot) ? 'selected' : ''}} #endif>{{$foot}}</option>
#endforeach
</select>
<span class="d-inline-block w-10 mr-3">ft</span>
<select class="form-control d-inline-block w-25 mr-1" name="primaryInches[]" id="primaryInches{{$i+1}}">
#foreach($inches as $inch)
<option value="{{$inch}}" #if($child){{($child->height_inch == $inch) ? 'selected' : ''}} #endif>{{$inch}}</option>
#endforeach
</select>
<span class="d-inline-block w-10">in</span>
</div>
<div class="form-group col-md">
<label for="primaryWeight">Weight (lbs)</label>
<br/>
<input type="number" min="0" step=".1" class="form-control d-inline-block w-75 mr-1" id="primaryWeight{{$i+1}}" value="{{$child->weight_lbs}}" name="primaryWeight[]" placeholder="Weight (lbs)">
<span class="d-inline-block w-10">lbs</span>
</div>
</div>
</div>
#endforeach
</div>
<hr/>
<div class="text-center my-4">
<button type="button" name="add" id="add" class="btn btn-success">Add More</button>
</div>
</div>
</div>
</div>
<div class="form-row mb-4 text-center">
<div class="form-group col-md-12 mt-4">
Go Back <button type="submit" name="savecontinue" class="btn btn-info">Save and Continue</button>
</div>
</div>
</form>
And here's my controller:
public function updateChildren(Request $request)
{
$id = Auth::user()->id;
$user = Auth::user();
$family = UserFamily::where(['user_id'=> $id, 'rel' => 'child'])->get();
$count = count($family);
$children = ($count > 0 ? $family : '');
$input = $request->all();
$rules = [
'first[*]' => 'required',
'userID[*]' => 'required',
'rel[*]' => 'required',
'first[*]' => 'required',
'last[*]' => 'required',
'inputSSN[*]' => 'required',
'dob[*]' => 'required',
'inputGender[*]' => 'required',
'primaryFeet[*]' => 'required',
'primaryInches[*]' => 'required',
'primaryWeight[*]' => 'required',
];
$validator = Validator::make($request->all(), $rules);
if ($validator->passes()) {
foreach($input['userID'] as $index => $value) {
$feet = $input['primaryFeet'][$index];
$inch = $input['primaryInches'][$index];
$weight = $input['primaryWeight'][$index];
$inches = ($feet * 12) + $inch;
$bmi = number_format(((703 * $weight) / ($inches * $inches)), 2, '.', '');
if(isset($input['ID'][$index])){
$famid = $input['ID'][$index];
}else {
$famid = '';
}
if($famid > 0 || $famid != '') {
$user = UserFamily::firstOrNew(['user_id'=> $id, 'rel' => 'child', 'id' => $famid]);
}else{
$user = UserFamily::create(['user_id'=> $id, 'rel' => 'child']);
}
$user->user_id = $id;
$user->bmi = $bmi;
$user->first_name = $input['first'][$index];
$user->dob = $input['dob'][$index];
$user->last_name = $input['last'][$index];
$user->rel = $input['rel'][$index];
$user->ssn = $input['inputSSN'][$index];
$user->gender = $input['inputGender'][$index];
$user->height_feet = $input['primaryFeet'][$index];
$user->height_inch = $input['primaryInches'][$index];
$user->weight_lbs = $input['primaryWeight'][$index];
$user->save();
}
return redirect()->route('preliminary-review');
}
return redirect()->route('preliminary-children')->withErrors($validator);
}
The only thing I've tried is setting up an if/else if($request->has('savecontinue'){ ... } and wrapping my existing controller in that. The else{} would handle all the modals, but it kept kicking back errors as if all my syntax was incorrect.
Thoughts? I'm also not opposed to approaching this differently if there's a better way.
PS, there's JS for the remove and add buttons, but I didn't think it was necessary for this question. If you need me to update this with it, I can. :)
Deleting a child is a different operation than updating a child, so you should have a different method for it in your controller. Not sure about how you've got anything named, but this should give you an idea.
Define a new route:
Route::delete('/whatever/{child}', [ChildController::class, 'deleteChild'])
->name('preliminary-children.deleteChild');
Note we'll use the delete HTTP method to access this route, and the ID is passed as part of the URL.
Update your view to point to this new route:
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="label{{$i+1}}">Delete Row</h5>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">Are you sure you want to remove this row?</div>
<div class="modal-footer">
<button class="btn bg-gray-300" type="button" data-dismiss="modal">Cancel</button>
<form id="remove-form{{$i+1}}" name="formrow[]" action="{{route('preliminary-children.deleteChild', $child->id)}}" method="POST" class="d-inline-block">
<input name="_method" value="DELETE" type="hidden"/>
<button type="submit" id="{{$i+1}}" name="removerow[]" class="btn btn-danger text-decoration-none btn_remove">Remove</button>
</form>
</div>
</div>
</div>
A web browser can't send a delete request, so we spoof the method with a hidden input.
And then we make the method:
public function deleteChild(UserFamily $child)
{
$child->delete();
return response()->json("success");
}
By type hinting the parameter, Laravel automatically looks up the relevant record in the database for you. Of course you'll want to do some error checking there too.

Mysql and PHP Update Problems

I am working on a simple Userprofile, where the user can update some data over Mysql. But it doesn´t update anything. Why?
<?php
if (isset($_GET['pageID'])) {
$id = $_GET['pageID'];
include("connector.php");
$stmt = $conn->prepare(" SELECT
cl.clientID, cl.firstname, cl.lastname, cl.mobilephone, cl.phone, cl.client_ID,
ad.street, ad.city, ad.postalcode,
wo.work_ID,
ha.hardware1, ha.serialnumber, ha.hardware2, ha.serialnumber2
FROM client cl
JOIN adress ad ON cl.clientID = ad.clientID
LEFT JOIN work wo ON cl.clientID = wo.clientID
LEFT JOIN hardware ha ON ad.adressID = ha.adressID
WHERE cl.clientID = '$id'");
$stmt->execute();
}
?>
<?php while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) : ?>
<div class="container">
<div class="col-sm-12 col-md-12 col-xl-12">
<div class="col-sm-3 col-md-3 col-xl-3 profileNames">
<img src="Userfotos/jedi.jpg" class="img-responsive"><br>
<button type="submit" name="submit" class="btn btn-info">edit Foto</button>
</div>
<div class="col-sm-3 col-md-3 col-xl-3 profileNames">
<p><h4>Lastname:</h4><?php echo $row['lastname']; ?></p>
<p><h4>Firstname:</h4> <?php echo $row['firstname']; ?></p>
<p><h4>Street</h4> <?php echo $row['street']; ?></p>
<p><h4>City</h4> <?php echo $row['city']; ?></p>
<p><h4>Postalcode</h4> <?php echo $row['postalcode']; ?></p>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#editPrivateData">Edit data
</button>
<div id="editPrivateData" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Edit Personal data</h4>
</div>
<div class="modal-body">
<form action="userProfile.php?pageID=1" method="post">
<div class="form-group">
<label for="_firstname">Firstname</label>
<input type="text" class="form-control" id="_firstname" name="firstname"
value="<?php echo $row['firstname']; ?>">
</div>
<div class="form-group">
<label for="_lastname">Lastname</label>
<input type="text" class="form-control" id="_lastname" name="lastname"
value="<?php echo $row['lastname']; ?>">
</div>
<div class="form-group">
<label for="_street">Street</label>
<input type="text" class="form-control" id="_street" name="street"
value="<?php echo $row['street']; ?>">
</div>
<div class="form-group">
<label for="_city">City</label>
<input type="text" class="form-control" id="_city" name="city"
value="<?php echo $row['city']; ?>">
</div>
<div class="form-group">
<label for="_postalcode">Postalcode</label>
<input type="text" class="form-control" id="_postalcode" name="postalcode"
value="<?php echo $row['postalcode']; ?>">
</div>
<button type="submit" name="submitPrivate" class="btn btn-info">Save</button>
<?php
if (isset($_POST['submitHardware'])) {
try {
$id = $_GET['pageID'];
$stmt = $conn->prepare("UPDATE clients.client SET
firstname = :firstname,
lastname = :lastname,
mobilephone = :mobilephone,
phone = :phone
WHERE clientID = $id");
$stmt->bindParam(':firstname', $firstname);
$stmt->bindParam(':lastname', $lastname);
$stmt->bindParam(':mobilephone', $mobilephone);
$stmt->bindParam(':phone', $phone);
$stmt->execute([':firstname' => $_POST['firstname'],
':lastname' => $_POST['lastname'],
':mobilephone' => $_POST['mobilephone'],
':phone' => $_POST['phone']
]);
} catch (PDOException $e) {
echo $e->getMessage() . ", ";
die("try to fix your inputs . If this doesn`t work, please contact your Admin.");
}
}
?>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-info" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
<div class="col-sm-3 col-md-3 col-xl-3 profileNames">
<p><h4>Hardware1:</h4><?php echo $row['hardware1']; ?></p>
<p><h4>Serialnumber1:</h4><?php echo $row['serialnumber']; ?></p>
<p><h4>Hardware2:</h4><?php echo $row['hardware2']; ?></p>
<p><h4>Serialnumber2:</h4><?php echo $row['serialnumber2']; ?></p>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#editHardware">Edit data
</button>
<!-- Modal -->
<div id="editHardware" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Edit hardware data</h4>
</div>
<div class="modal-body">
<form action="userProfile.php?pageID=3" method="post">
<div class="form-group">
<label for="_hardware1">Hardware1</label>
<input type="text" class="form-control" id="_hardware" name="hardware1"
placeholder="<?php echo $row['hardware1']; ?>">
</div>
<div class="form-group">
<label for="serialnumber">Serialnumber1</label>
<input type="text" class="form-control" id="_serialnumber" name="serialnumber"
placeholder="<?php echo $row['serialnumber']; ?>">
</div>
<div class="form-group">
<label for="_hardware2">Hardware2</label>
<input type="text" class="form-control" id="_hardware2" name="hardware2"
placeholder="<?php echo $row['hardware2']; ?>">
</div>
<div class="form-group">
<label for="_serialnumber2">Serialnumber2</label>
<input type="text" class="form-control" id="_serialnumber2" name="serialnumber2"
placeholder="<?php echo $row['serialnumber2']; ?>">
</div>
<button type="submit" name="submitHardware" class="btn btn-info">Save</button>
<?php
if (isset($_POST['submitHardware'])) {
require("connector.php");
try {
$stmt = $conn->prepare("UPDATE clients.hardware SET
hardware1 = :hardware1,
serialnumber = :serialnumber,
hardware2 = :hardware2,
serialnumber2 = :serialnumber2,
WHERE hardware.clientID = '$id'");
$stmt->bindParam(':firstname', $firstname);
$stmt->bindParam(':lastname', $lastname);
$stmt->bindParam(':mobilephone', $mobilephone);
$stmt->bindParam(':phone', $phone);
$stmt->bindParam(':client_ID', $client_ID);
$stmt->execute([':hardware1' => $_POST['hardware1'],
':serialnumber' => $_POST['serialnumber'],
':hardware2' => $_POST['hardware2'],
':serialnumber2' => $_POST['seerialnumber2']
]);
} catch (PDOException $e) {
echo $e->getMessage() . ", ";
die("try to fix your inputs . If this doesn`t work, please contact your Admin.");
}
}
?>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-info" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
<div class="col-sm-3 col-md-3 col-xl-3 profileNames">
<p><h4>ClientID:</h4><?php echo $row['client_ID']; ?></p>
<p><h4>WorkID:</h4><?php echo $row['work_ID']; ?></p>
<button type="submit" name="submit" class="btn btn-info">Edit data</button>
</div>
</div>
<?php endwhile; ?>
As you can see, there is a while-loop to fetch the data and display them into some fields. In Between this loop, i created two Bootstrap-Modals, where the user can edit something. if the user clicks on the "save" button, nothing happens. i really don`t understand where is the problem.
and another small point: the userprofile is generated dynamically with the clientID(pageID). how do i temporary save this pageID, so i can return to the current page after the form action method ="POST"
The clientID is the primary key in the database for the clients.client table. The client_ID is an ID the user has to set by himself, he can set whatever he want to. The PageID is linked to the clientID is for generating dynamical pages. That means: on another page there is a table which displays some basically informations about all users. If the logged in User clicks on a user from the table, there will be the userprofile dynamically generated.
your code is really weird. Please tell us what the page ID and client ID are. They are obviously not the same. But you use them as such. I reduced your code to just one modal and used hidden fields for client and page ID values.
<?php
// TODO: filter all input
if (isset($_POST['submitHardware']) && $_POST['pageID'] == 3) {
try {
include("connector.php");
$client_id = $_GET['clientID'];
$stmt = $conn->prepare("UPDATE clients.client SET
firstname = :firstname,
lastname = :lastname,
mobilephone = :mobilephone,
phone = :phone
WHERE clientID = $client_id");
$stmt->bindParam(':firstname', $firstname);
$stmt->bindParam(':lastname', $lastname);
$stmt->bindParam(':mobilephone', $mobilephone);
$stmt->bindParam(':phone', $phone);
$stmt->execute([':firstname' => $_POST['firstname'],
':lastname' => $_POST['lastname'],
':mobilephone' => $_POST['mobilephone'],
':phone' => $_POST['phone']
]);
} catch (PDOException $e) {
echo $e->getMessage() . ", ";
die("try to fix your inputs . If this doesn`t work, please contact your Admin.");
}
}
if (isset($_GET['clientID'])) {
$client_id = $_GET['clientID']; // TODO: filter
include("connector.php");
$stmt = $conn->prepare(" SELECT
cl.clientID, cl.firstname, cl.lastname, cl.mobilephone, cl.phone, cl.client_ID,
ad.street, ad.city, ad.postalcode,
wo.work_ID,
ha.hardware1, ha.serialnumber, ha.hardware2, ha.serialnumber2
FROM client cl
JOIN adress ad ON cl.clientID = ad.clientID
LEFT JOIN work wo ON cl.clientID = wo.clientID
LEFT JOIN hardware ha ON ad.adressID = ha.adressID
WHERE cl.clientID = '$client_id'");
$stmt->execute();
}
?>
<?php while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) : ?>
<div class="container">
<div class="col-sm-12 col-md-12 col-xl-12">
<div class="col-sm-3 col-md-3 col-xl-3 profileNames">
<img src="Userfotos/jedi.jpg" class="img-responsive"><br>
<button type="submit" name="submit" class="btn btn-info">edit Foto</button>
</div>
<div class="col-sm-3 col-md-3 col-xl-3 profileNames">
<p><h4>Hardware1:</h4><?php echo $row['hardware1']; ?></p>
<p><h4>Serialnumber1:</h4><?php echo $row['serialnumber']; ?></p>
<p><h4>Hardware2:</h4><?php echo $row['hardware2']; ?></p>
<p><h4>Serialnumber2:</h4><?php echo $row['serialnumber2']; ?></p>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#editHardware">Edit data
</button>
<!-- Modal -->
<div id="editHardware" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Edit hardware data</h4>
</div>
<div class="modal-body">
<form action="userProfile.php" method="post">
<input type="hidden" name="pageID" value="3">
<input type="hidden" name="clientID" value="<?php echo $client_id; ?>">
<div class="form-group">
<label for="_hardware1">Hardware1</label>
<input type="text" class="form-control" id="_hardware" name="hardware1"
placeholder="<?php echo $row['hardware1']; ?>">
</div>
<div class="form-group">
<label for="serialnumber">Serialnumber1</label>
<input type="text" class="form-control" id="_serialnumber" name="serialnumber"
placeholder="<?php echo $row['serialnumber']; ?>">
</div>
<div class="form-group">
<label for="_hardware2">Hardware2</label>
<input type="text" class="form-control" id="_hardware2" name="hardware2"
placeholder="<?php echo $row['hardware2']; ?>">
</div>
<div class="form-group">
<label for="_serialnumber2">Serialnumber2</label>
<input type="text" class="form-control" id="_serialnumber2" name="serialnumber2"
placeholder="<?php echo $row['serialnumber2']; ?>">
</div>
<button type="submit" name="submitHardware" class="btn btn-info">Save</button>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-info" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</div>
<?php endwhile; ?>

PDO Update User Information Query

its me again :)
i'm almost done with my project theres only one more thing left.
i am not able to update values in my database using pdo.
this is my script
<a
href="?UpdateUser=' .$ID. '"
class="btn btn-sm btn-icon btn-pure btn-default on-default edit-row"
data-original-title="Bearbeiten"
ref="" data-toggle="modal" data-target="#EditUser">
<i class="icon wb-edit" aria-hidden="true"></i></a>
<div class="modal fade" id="EditUser" aria-hidden="false" aria-labelledby="EditUserModal"
role="dialog" tabindex="-1">
<div class="modal-dialog modal-simple modal-center">
<form class="modal-content" method="POST" role="form" action="">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="EditUserModal">Benutzer bearbeiten</h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-xl-12 form-group">
<p><b>Username</b></p>
<input type="text" class="form-control" name="username" value="'.$username.'">
</div>
<div class="col-xl-4 form-group">
<p><b>Vorname</b></p>
<input type="text" class="form-control" name="vorname" value="'.$vorname.'">
</div>
<div class="col-xl-8 form-group">
<p><b>Nachname</b></p>
<input type="text" class="form-control" name="nachname" value="'.$nachname.'">
</div>
<div class="col-xl-8 form-group">
<p><b>Email</b></p>
<input type="email" class="form-control" name="email" value="'.$email.'">
</div>
<div class="col-xl-4 form-group">
<p><b>TelefonNr.</b></p>
<input type="text" class="form-control" name="handy" value="'.$handy.'">
</div>
<div class="col-xl-12 form-group">
<p><b>Admin</b></p>
<input type="text" class="form-control" name="admin" value="'.$admin.'">
</div>
<div class="col-md-12 float-right">
<button class="btn btn-primary btn-outline" data-dismiss="modal" type="submit">Speichern</button>
</div>
</div>
</div>
</form>
</div>
</div>
</td>
</tr>';
}
if(isset($_POST['UpdateUser']))
{
if($_POST['UpdateUser'])
{
$stmt = $odb->execute("UPDATE Account SET username = '$username' , vorname = '$vorname', nachname = '$nachname', email = '$email', handy = '$handy', admin = '$admin' WHERE id=? ");
}
echo "<meta http-equiv='refresh()' content='0'>";
}
am i doing a wrong query ? or why is it not updating?
there is no error showing up on the page, but the values are not updating nor refreshing the page.
You need to use prepare before execute, like this:
$query = 'UPDATE Account SET
username = :username,
vorname = :vorname,
nachname = :nachname,
email = :email,
handy = :handy,
admin = :admin
WHERE id = :id';
$stmt = $pdo->prepare($query);
$stmt->execute(array(
':username' => $username,
':vorname' => $vorname,
':nachname' => $nachname,
':email' => $email,
':handy' => $handy,
':admin' => $admin,
':id' => $id
));
Also, is better to use placeholders for every input value, instead of write variables inside the query string.
And now I figured out this:
You are using a form to send data with method POST and trying to read $_POST['UpdateUser'] to start executing the update, but in that form there is no field called UpdateUser.
You have to add that value inside the form, or change the if (isset($_POST['UpdateUser'])) with some other field.

How to create Drop down from database in Codeigniter

Sorry, i want the create Form input using drop down from database. but i've try to created and error like this.
My Code in controller
public function ajax_add()
{
$data = array(
'date_man_activity_ra' => $this->input->post('date_man_activity_ra',TRUE),
'dd_user' => $this->mymodel->dd_user(),
'user_selected' => $this->input->post('id_user') ? $this->input->post('id_user') : '',
'id_user' => $this->input->post('id_user',TRUE),
'note' => $this->input->post('note',TRUE),
);
$insert = $this->Man_activity->save($data);
echo json_encode(array("status" => TRUE));
}
and mymodel
public function dd_user()
{
// ambil data dari db
$this->db->order_by('id_user', 'asc');
$result = $this->db->get('ops_user');
// bikin array
// please select berikut ini merupakan tambahan saja agar saat pertama
// diload akan ditampilkan text please select.
$dd[''] = 'Please Select';
if ($result->num_rows() > 0) {
foreach ($result->result() as $row) {
// tentukan value (sebelah kiri) dan labelnya (sebelah kanan)
$dd[$row->id_user] = $row->username;
}
}
return $dd;
}
and my View
<div class="modal fade" id="modal_form" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h3 class="modal-title">Person Form</h3>
</div>
<div class="modal-body form">
<form action="#" id="form" class="form-horizontal">
<input type="hidden" value="" name="id"/>
<div class="form-body">
<div class="form-group">
<label class="control-label col-md-3">date_man_activity_ra</label>
<div class="col-md-9">
<input name="date_man_activity_ra" placeholder="yyyy-mm-dd" class="form-control datepicker" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Id User</label>
<div class="col-md-9">
<?php
$dd_user_attribute = 'class="form-control select2"';
echo form_dropdown('id_user', $dd_user, $user_selected, $dd_user_attribute);
?>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Note</label>
<div class="col-md-9">
<input name="note" placeholder="note" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" id="btnSave" onclick="save()" class="btn btn-primary">Save</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
how to solve it? or Another ways?
Thank You
public function ajax_add()
{
$data = array(
'date_man_activity_ra' => $this->input->post('date_man_activity_ra',TRUE),
'dd_user' => $this->mymodel->dd_user(),
'user_selected' => $this->input->post('id_user') ? $this->input->post('id_user') : '',
'id_user' => $this->input->post('id_user',TRUE),
'note' => $this->input->post('note',TRUE),
);
$insert = $this->Man_activity->save($data);
// Load your view and pass the data that you use in dropdown
//echo json_encode(array("status" => TRUE)); // Remove it
}

If value already exists in array then issue an error in PHP

I have a code wherein it checks if a value already exists in the array. Basically, what the program does is that it first stores all the values in the array. Then it will be checked using count(array_keys) function. There are three inputs. If in those 3 inputs, a value occurs twice or thrice, it will issue an error. Now, my problem is that if INPUT A AND INPUT B IS THE SAME BUT INPUT C is different, it will still add to the database, BUT IF INPUT A AND C IS THE SAME BUT INPUT B is different then it will not add (which is correct).
Here is my php code:
<?php
include 'config.php';
if(isset($_POST['update_actual_lupong'])){
$id = isset($_GET['id'])? $_GET['id'] : "";
$id_hearing = $_POST['hearing_lup'];
$lupong = $_POST['act_lupong'];
$actual = array();
foreach($lupong as $aaa) {
$actual[] = $aaa;
}
if ((count(array_keys($actual, $aaa)) > 1)) {
echo '<br><br>this array contains DUPLICATE <br><br>';
}
else {
foreach ($lupong as $lup) {
$updateMemo = mysqli_query($conn, "INSERT INTO actuallupong(Hearing_idHearing, bar_pos2) VALUES ('$id_hearing', (SELECT idtable_position FROM table_position WHERE Person_idPerson = '$lup'));");
}
echo "ADDED ggg";
}
//header ('location: view_case_profile.php?id='.$id);
mysqli_close($conn);
}
?>
HTML code (it's in a modal):
<div class="modal fade bs-example-modal-lg" id="modal_lupong" data-backdrop="static" data-keyboard="false" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" >
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title" id="myModalLabel">Update Lupong</h4>
</div>
<form id="update_actual_lupong" class="form-horizontal form-label-left calender" name = "update_actual_lupong" enctype="multipart/form-data" method="post" role="form" novalidate>
<div class="modal-body">
<div class="d item form-group">
<label class="col-sm-3 control-label">Hearing Number</label>
<div class="col-sm-7">
<input type="number" class="form-control" id="hearing_lup" name="hearing_lup" readonly="readonly"/>
</div>
</div>
<div class="f item form-group" id = "act1">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Lupong 1 <span class="required">*</span></label>
<div class="col-sm-7">
<input name="actlupong[]" id="search_keyword_id_act" class="search_keyword_act form-control col-md-7 col-xs-12" required="required" placeholder ="Search first name or last name... " type="text">
<input type="hidden" name="act_lupong[]" id="act_lup1"/>
<div id="result3"></div>
</div>
</div>
<div class="f item form-group" id = "act2">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Lupong 2 <span class="required">*</span></label>
<div class="col-sm-7">
<input name="actlupong[]" id="search_keyword_id_act1" class="search_keyword_act1 form-control col-md-7 col-xs-12" required="required" placeholder ="Search first name or last name... " type="text">
<input type="hidden" name="act_lupong[]" id="act_lup2"/>
<div id="result4"></div>
</div>
</div>
<div class="f item form-group" id = "act3">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Lupong 3 <span class="required">*</span></label>
<div class="col-sm-7">
<input name="actlupong[]" id="search_keyword_id_act2" class="search_keyword_act2 form-control col-md-7 col-xs-12" required="required" placeholder ="Search first name or last name... " type="text">
<input type="hidden" name="act_lupong[]" id="act_lup3"/>
<div id="result5"></div>
</div>
</div>
</div>
<div class="modal-footer" style="margin:0;">
<button type="button" class="btn btn-default" data-dismiss="modal" style="margin-top: 4px;">Close</button>
<button id="send" type="submit" class="btn btn-success" name="update_actual_lupong">Save Record</button>
</div>
</form>
</div>
</div>
</div>
Screenshot:
What seems to be wrong in my code? Which part should I change? Your help will be much appreciated. Thank you.
As you said:- There are three inputs. If in those 3 inputs, a value occurs twice or thrice, it will issue an error.
A bit modification to your code needed:-
<?php
include 'config.php';
if(isset($_POST['update_actual_lupong'])){
$id = isset($_GET['id'])? $_GET['id'] : "";
$id_hearing = $_POST['hearing_lup'];
$lupong = $_POST['act_lupong'];
if (count(array_unique($lupong)) < count($lupong))) { // check that count of unique $lupong and original $lupong is equal or not if not then $lupong have duplicate values
echo '<br><br>this array contains DUPLICATE <br><br>';
}
else {
foreach ($lupong as $lup) {
$updateMemo = mysqli_query($conn, "INSERT INTO actuallupong(Hearing_idHearing, bar_pos2) VALUES ('$id_hearing', (SELECT idtable_position FROM table_position WHERE Person_idPerson = '$lup'));");
}
echo "ADDED ggg";
}
//header ('location: view_case_profile.php?id='.$id);
mysqli_close($conn);
}
?>
You exit the loop with $aaa as the value of c so only check this value for duplication.
You should check for duplicates inside the loop and set a variable i.e.
$dup = false;
foreach($lupong as $aaa) {
$actual[] = $aaa;
if ((count(array_keys($actual, $aaa)) > 1)) {
$dup = true;
}
}
if ($dup) {
echo '<br><br>this array contains DUPLICATE <br><br>';
}
else {
foreach ($lupong as $lup) {
$updateMemo = mysqli_query($conn, "INSERT INTO actuallupong(Hearing_idHearing, bar_pos2) VALUES ('$id_hearing', (SELECT idtable_position FROM table_position WHERE Person_idPerson = '$lup'));");
}
echo "ADDED ggg";
}

Categories