Pretty much what I want to do is get ID from database and pass that ID values into input boxes so I can re-edit them when clicking on edit button
<td><a class= "waves-effect waves-light btn" href="http://localhost/robinzonlucica/public/includes/evidencija.inc.php?edit='.$row['evidencijaId'].'"><span class="fas fa-edit"></span></a></td>
This is my icon href to get Id and pass it to evidencija.inc.php
if(isset($_GET["edit"])){
$id = $_GET["edit"];
require_once $_SERVER['DOCUMENT_ROOT'].'/robinzonlucica/public/includes/dbh.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/robinzonlucica/public/includes/funkcije.inc.php';
izmijeniEvidenciju($conn,$id);
}
else{
header("location: http://localhost/robinzonlucica/public/evidencija.php");
exit();
}
This is my evidencija.inc.php part to get id from that row and there is izmijeniEvidenciju($conn,$id); function to edit values for given edit=id
function izmijeniEvidenciju($conn, $id) {
$sql = "SELECT * FROM evidencija WHERE evidencijaId = ?;";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql)){
header("location: http://localhost/robinzonlucica/public/evidencija.php?error=stmtgreska");
exit();
}
mysqli_stmt_bind_param($stmt, "s", $id);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
}
That is my function to get data I want data to show in input fields not sure where I went wrong I am very confused
if(isset($_SESSION["username"])) {
echo '<h2 class ="center-align evidencijanaslov">Evidencija</h2>
<div class="row">
<form action="http://localhost/robinzonlucica/public/includes/evidencija.inc.php" class="col s12 registerforma" method="post">
<div class="row">
<div class="input-field col s6">
<input id="ime" type="text" class="validate" name="ime">
<label for="last_name">Ime Turista</label>
</div>
</div>
<div class="row">
<div class="input-field col s6">
<input id="ime" type="text" class="validate" name="prezime" >
<label for="last_name">Prezime turista</label>
</div>
</div>
<div class="row">
<div class="input-field col s6">
<input id="ime" type="text" class="validate" name="oib">
<label for="last_name">OIB Turista</label>
</div>
</div>
<div class="row">
<div class="input-field col s6">
<input type=text name="datum" id="datum" class="datepicker">
<label for="datum">Datum odlaska</label>
</div>
</div>
<button class="btn waves-effect waves-light gumbsalji" type="submit" name="submit">Upiši</button>
</div>
</form>';
}
This is html input
Related
So, I cannot find the solution to the problem I'm having. I'm really new to coding but learned how to start coding using basic HTML, PHP, PDO, and AJAX. So my problem comes from a form that retrieves dates from a calendar using the type=date from the form. The code of the form is down below.
<div class="col-lg-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title text-center"><i class="fa fa-bar-chart fa-fw"></i> Ingreso de reporte</h3>
</div>
<div id="alert_success" class="panel-body">
<br>
<form method="post" class="form-horizontal" role="form" action="ajax_form_post.php" id="insertreport">
<div class="form-group">
<label class="control-label col-sm-2" for="video" style="color:#777;">ID de video</label>
<div class="col-sm-10">
<input type="text" name="video" class="form-control" id="video" placeholder="Ingresa id del video" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="date_i" style="color:#777;">Fecha de arriendo</label>
<div class="col-sm-10">
<input type="date" name="date_i" class="form-control" id="date_i" placeholder="" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="date_f" style="color:#777;">Fecha de devolución</label>
<div class="col-sm-10">
<input type="date" name="date_f" class="form-control" id="date_f" placeholder="" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input type="hidden" name="c_id" class="form-control" id="user_id" value="<?php echo $id ?>" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-primary" name="update_customer" value="Enviar" id="submitdata">
</div>
</div>
</form>
<div class="text-right">
<i class="fa fa-arrow-circle-right"></i>
</div>
</div>
</div>
</div>
Now the problem starts with this Ajax form I built. BTW the script is working fine, the problem is inside this set of code.
<?php
/****************Get customer info to ajax *******************/
//require database class files
require("includes/pdocon.php");
//instatiating our database objects
$db = new Pdocon ;
if(isset($_POST['c_id'])){
$id = $_POST['c_id'];
$date_i = date("Y-m-d", strtotime($_POST['date_i']));
$date_f = date("Y-m-d", strtotime($_POST['date_f']));
$raw_v_id = clean_data($_POST['video']);
$v_id = val_int($raw_v_id);
$db->query('SELECT * FROM videos WHERE v_id = :v_id');
$db->bindvalue(':v_id', $v_id, PDO::PARAM_INT);
$row = $db->fetchSingle();
$db->query('INSERT INTO arriendo (transaccion, c_id, v_id, f_arriendo, f_devolucion)
VALUES (NULL, :c_id, :v_id :f_arriendo, :f_devolucion)');
$db->bindvalue(':f_arriendo', $date_i, PDO::PARAM_STR);
$db->bindvalue(':f_devolucion', $date_f, PDO::PARAM_STR);
$db->bindvalue(':c_id', $id, PDO::PARAM_INT);
$db->bindvalue(':v_id', $v_id, PDO::PARAM_INT);
$run = $db->execute();
}
if($run){
echo "<p class='bg-success text-center' style='font-weight:bold;'>Valor actualizado </p>";
}
?>
I get the following error:
Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''2021-08-05', '2021-08-06')' at line 2
Any help or a little guidance would be greatly appreciated. Thanks in advance.
Currently I am stuck when i click edit icon my page goes to
http://localhost/robinzonlucica/public/includes/evidencija.inc.php?edit=3
It should insert data into my form
if(isset($_GET["edit"])){
$id = $_GET["edit"];
require_once $_SERVER['DOCUMENT_ROOT'].'/robinzonlucica/public/includes/dbh.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/robinzonlucica/public/includes/funkcije.inc.php';
izmijeniEvidenciju($conn,$id);
}
This form should call function izmijeniEvidenciju($conn,$id);
This is function code
function izmijeniEvidenciju($conn, $id) {
$sql = "SELECT * FROM evidencija WHERE evidencijaId = ?;";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql)){
header("location: http://localhost/robinzonlucica/public/evidencija.php?error=stmtgreska");
exit();
}
mysqli_stmt_bind_param($stmt, "s", $id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
return $row;
mysqli_stmt_close($stmt);
}
And this is my html part form that values should be written in after clicking edit but when i click icon to edit it goes on blank page and stays like that
<?php
include_once 'header.php';
require_once 'includes/funkcije.inc.php';
require_once 'includes/dbh.inc.php';
?>
<section class="evidencija">
<?php
if(isset($_SESSION["username"])) {
$editrow = izmijeniEvidenciju($conn, $id);
echo '<h2 class ="center-align evidencijanaslov">Evidencija</h2>
<div class="row">
<form action="http://localhost/robinzonlucica/public/includes/evidencija.inc.php" class="col s12 registerforma" method="post">
<div class="row">
<div class="input-field col s6">
<input id="ime" type="text" class="validate" name="ime" value="'.$editrow['evidencijaTuristIme'].'">
<label for="last_name">Ime Turista</label>
</div>
</div>
<div class="row">
<div class="input-field col s6">
<input id="ime" type="text" class="validate" name="prezime" value="'.$editrow['evidencijaTuristPrezime'].' ">
<label for="last_name">Prezime turista</label>
</div>
</div>
<div class="row">
<div class="input-field col s6">
<input id="ime" type="text" class="validate" name="oib" value="'.$editrow['evidencijaTuristOIB'].'">
<label for="last_name">OIB Turista</label>
</div>
</div>
<div class="row">
<div class="input-field col s6">
<input type=text name="datum" id="datum" class="datepicker" value="'.$editrow['evidencijaTuristDatumOdlaska'].'">
<label for="datum">Datum odlaska</label>
</div>
</div>
<button class="btn waves-effect waves-light gumbsalji" type="submit" name="submit">Upiši</button>
</div>
</form>';
}
else {
echo '<h1>Prijavite se kako bi vidjeli evidenciju</h1>';
}
I am getting that $id variable is not declared too don't know where I went wrong PS hardcoding $id with id from table inputs values into form :/
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; ?>
I'm trying to add the data from my form into the database. But it's only refreshing. Here is my code for my form:
<form action="" method="post">
<div id="modal1" class="modal modal-fixed-footer">
<div class="modal-content">
<h4>Bedroom</h4><br><br>
<div class="row">
<div class="input-field col s6">
<i class="material-icons prefix">account_circle</i>
<input name="txt_room_ID" id="txt_room_ID" type="text" class="validate" readonly="true"
value="<?php
$sql= "select * from tblBedroom";
$stmt = sqlsrv_query($con,$sql);
while($rs = sqlsrv_fetch_array($stmt)){
$Setting_adminID = $rs['BedRoomID'];
$adminID_new = str_replace("B","",$Setting_adminID);
$adminID = str_pad($adminID_new + 1,11,0,STR_PAD_LEFT);}
echo htmlentities('B'. $adminID);
?>">
<label for="room_ID">Bedroom ID:</label>
</div>
<div class="input-field col s6">
<i class="material-icons prefix">account_circle</i>
<input name="txt_bedroom_no" id="txt_bedroom_no" type="text" class="validate">
<label for="bedroom_no">Room Number</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<select name="txt_Room_Type">
<option value="" disabled selected>Choose Room Type</option>
<?php
$sql = "select * from tblBedRoomType";
$stmt = sqlsrv_query($con,$sql);
while($rs = sqlsrv_fetch_array($stmt)){
$BedroomTypeID = $rs['BedroomTypeID'];
$BedroomName = $rs['BedroomName'];
echo"<option value='$bedroomTypeID'>$BedroomName</option>";
}
?>
</select>
<label>Room Type</label>
</div>
</div>
</div><!--END OF MODAL CONTENT-->
<div class="modal-footer">
<a class="modal-action modal-close waves-effect waves-green btn-flat ">Cancel</a>
<input id="btn_Save" name="btn_Save" type="submit" value="Save" class="waves-effect waves-green btn-flat " onclick="return confirm('Are You Sure You Want To Save?');">
</div>
</div>
</form>
The php below the form:
<?php
if(isset($_POST['btn_Save'])){
$txt_room_ID = $_POST['txt_room_ID'];
$txt_bedroom_no = $_POST['txt_bedroom_no'];
$txt_Room_Type = $_POST['txt_Room_Type'];
$AcctStat = 1;
$sql = "insert into tblBedRoom (BedRoomID,BedroomType,RoomNo,AcctStat)
values('$txt_room_ID','$txt_Room_Type','$txt_bedroom_no','$AcctStat')";
$stmt = sqlsrv_query($con,$sql);
echo "<meta http-equiv='refresh' content='0'>";
}
?>
And my columns in my tblBedRoom are:
BedRoomID | BedroomType | RoomNo |AcctStat
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";
}