Undefined index when passing variable to another page from a form - php

I'm trying to pass a value from one page to the other but I can't for the life of me understand why I keep getting the undefined index error message.
This is the PHP code to the first page:
<?php
include 'database_connection.php';
$id = $_GET['id'];
$sql="select engineer.id, engineer.team_id, engineer.first_name, engineer.active, engineer.last_name, engineer.role, engineer.region, engineer.phone, to_date, team.team_name, team.manager_name, team.description, team.type, engineer.email from engineer inner join team on engineer.team_id=team.id where active=0 and engineer.team_id > 0 and engineer.id = '".$id."'";
$results = mysqli_query($connection, $sql);
while($row = mysqli_fetch_array($results)) {
$id=$row['id'];
$first_name=$row['first_name'];
$last_name=$row['last_name'];
$role=$row['role'];
$email=$row['email'];
$phone=$row['phone'];
$region=$row['region'];
$type=$row['type'];
?>
<form class="form-horizontal col-sm-12" role="form" method="post" action="../admin/update.php">
<fieldset disabled>
<div class="form-group">
<label for="sso" class="col-sm-2 control-label">SSO ID</label>
<div class="col-sm-10">
<input type="text" id="id" name="id" class="form-control" value="<?php echo $id; ?>">
</div>
</div>
</fieldset>
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="first_name" placeholder="First Name" value="<?php echo $first_name; ?>">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">Surname</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="last_name" placeholder="Last Name" value="<?php echo $last_name; ?>">
</div>
</div>
<div class="form-group">
<label for="team" class="col-sm-2 control-label">Team</label>
<div class="col-sm-10">
<select style="width:auto;" class="btn btn-default dropdown-toggle form-control" type="button" name="team_name" value="" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<?php
//connect to the database
include 'database_connection.php';
//query the database
$sql_team = "SELECT DISTINCT id, team_name FROM team";
//uses $sql_team variable to make a specific query
$query = mysqli_query($connection, $sql_team);
?>
<option value="<?php echo $team_name; ?>"><?php echo $team_name; ?></option>
<?php
//initilises a while loop to retrieve all the rows
while ($row = mysqli_fetch_array($query) )
{
//echos all the distinct catDesc rows into a list
echo "<option value='" . $row['id'] . "' >".htmlspecialchars($row["team_name"])."</option>";
}
?>
</select>
</div>
</div>
<div class="form-group">
<label for="role" class="col-sm-2 control-label">Role</label>
<div class="col-sm-10">
<input type="role" class="form-control" id="role" name="role" placeholder="Role" value="<?php echo $role; ?>">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="<?php echo $email; ?>">
</div>
</div>
<div class="form-group">
<label for="phone" class="col-sm-2 control-label">Phone Number</label>
<div class="col-sm-10">
<input type="phone" class="form-control" id="phone" name="phone" placeholder="Business number" value="<?php echo $phone; ?>">
</div>
</div>
<div class="form-group">
<label for="region" class="col-sm-2 control-label">Region</label>
<div class="col-sm-10">
<input type="region" class="form-control" id="region" name="region" placeholder="Region e.g. South" value="<?php echo $region; ?>">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input type="submit" value="Save" class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<! Will be used to display an alert to the user>
</div>
</div>
</form>
<?php
//closes and stops the loop
}
?>
This is the code to the second page, where the error message appears:
<?php
include 'database_connection.php';
$id = mysqli_real_escape_string($connection, $_GET['id']);
$first_name = mysqli_real_escape_string($connection, $_POST['first_name']);
$last_name = mysqli_real_escape_string($connection, $_POST['last_name']);
$team_name = mysqli_real_escape_string($connection, $_POST['team_name']);
$role = mysqli_real_escape_string($connection, $_POST['role']);
$email = mysqli_real_escape_string($connection, $_POST['email']);
$phone = mysqli_real_escape_string($connection, $_POST['phone']);
$region = mysqli_real_escape_string($connection, $_POST['region']);
if ($role == ''){
$role = NULL;
}
if ($email == ''){
$email = NULL;
}
if ($phone == ''){
$phone = NULL;
}
if ($region == ''){
$region = NULL;
}
$sql = "UPDATE people SET id='$id', first_name='$first_name', last_name='$last_name', team_id='$team_name', role='$role', email='$email', phone=$phone', region='$region' where id='$id')";
?>
So the error appears when I try to retrieve the ID onto the second page from the first page. What am I missing?

if you want to pass a variable as get method you can pass it inside action attribute of form
eg: action="xyz.php?id="
on line 3 you have assigned a $_GET[] variable you cannot assign there since those values only sets after submitting form so if you want to remove error please script it like this
if(isset($GET['id']){
$id = $_GET['id'];
}

use an
<input id="id" type="hidden" value=$id>
in your form and read it as
$_POST['id']
in the second page

Related

How to check and uncheck a checkbox field by boolean value?

I want to update my Users table, and activate or deactivate a user. But i have problem in that.
I am trying to get a boolean value from Access database into checkbox or radio button. I can't do it. I want if the boolean value is 1 the checkbox should be checked if it's 0 the checkbox should be unchecked.
The same problem for User role I want to display it's current role if necessary update it by selecting Admin or User
Then if I check, it should register 1 or true to the database if unchecked register 0 to database.
Here is my code:
// PHP
include_once("../dbConnection.php");
// here i get a user by it's ID to update
$userId = $_GET['id'];
$sql = "SELECT * FROM Users WHERE User_ID = $userId ; ";
$result = $pdo->query($sql);
if (isset($_POST['update'])) {
$username = $_POST['username'];
$password = hash('sha256', $_POST['password']);
$email = $_POST['email'];
$role = $_POST['role'];
$isActive = $_POST['isActive'];
$editSql = "UPDATE Users
SET Login='$username', Password='$password', Email='$email', Type='$role', UserActive='$isActive'
WHERE User_ID=$userId;";
if ($pdo->query($editSql)) {
echo "Record successfully registered ! <br />";
header('location: ../index.php');
} else {
echo "Could not register ! <br>";
}
}
foreach ($result->fetchAll() as $row) {
}
//MY FORM
<form action="" class="container" method="POST">
<h3 class="text-center mt-0 mb-3">Updating User ! </h3>
<div class="row mt-3">
<div class="col-md-6">
<label class="form-label">Username </label>
<input type="text" class="form-control" name="username" value="<?= $row['Login'] ?>" required autofocus>
</div>
<div class="col-md-6">
<label class="form-label" for="">Password </label>
<input type="password" class="form-control" name="password" value="<?= $row['Password'] ?>" required>
</div>
</div>
<div class="row mt-3">
<div class="col-md-6">
<label class="form-label" for="">Email Address </label>
<input type="email" class="form-control" name="email" value="<?= $row['Email'] ?>">
</div>
<div class="col-md-6">
<label class="form-label" for="">Select User Type </label>
<select name="role" class="form-select" required>
<!-- <option>Choose a Role</option> -->
<option name="user" value="<?= $row['Type'] ?>" required>User</option>
<option name="user" value="2" required>User</option>
<option name="admin" value="1">Admin</option>
</select>
</div>
</div>
<div class="row mt-3">
<div class="col-md-6">
<label class="form-label" for="">Is Active </label> <br>
<?php if ($row['UserActive'] == 1) { ?>
YES: <input type="checkbox" name="isActive" value="1" checked>
<?php } else { ?>
NO: <input type="checkbox" name="isActive" value="0">
<?php } ?>
</div>
</div>
<div class="row">
<div class="col">
<button type="submit" class="btn btn-primary mt-2" name="update"> Update ! </button>
</div>
</div>
</form>

Data from row into no php-site for edit

I have a problem with my php-code. On the first site is a list with all the data. The table of the data is correctly displayed. In the last column of the table is a link placed to the next page which should hand over the id of the row.
Here's the link:
print 'Ändern';
But I can't now get the data into the input fields for edit. The form show up correct, but i have in every input field this error:
Warning: Illegal string offset 'vorname' in /home_pr5/d/e/deniseli.ch/htdocs/www.deniseli.ch/T .... tor/editsr.php on line 130S
Here's the editsr.php:
<?php
session_start();
require_once("inc/config.inc.php");
require_once("inc/functions.inc.php");
$id = $_GET['id'];
$statement = $pdo->prepare("SELECT * FROM users WHERE id = $id");
$result = $statement->execute(array('id' => ['id']));
$user = $statement->fetch();
include("templates/header.inc.php");
if(isset($_GET['save'])) {
$save = $_GET['save'];
if($save == 'personal_data') {
$vorname = trim($_POST['vorname']);
$nachname = trim($_POST['nachname']);
$adresse = trim($_POST['adresse']);
$plz = trim($_POST['plz']);
$ort = trim($_POST['ort']);
$geburtstag = trim($_POST['geburtstag']);
$handy = trim($_POST['handy']);
$liga = trim($_POST['liga']);
$verein = trim($_POST['verein']);
$bank = trim($_POST['bank']);
$iban = trim($_POST['iban']);
if($vorname == "" || $nachname == "" || $adresse == "" || $plz == "" || $ort == "" || $handy == "" || $liga == "" || $verein == "") {
$error_msg = "Bitte alle Angaben ausfüllen.";
} else {
$statement = $pdo->prepare("UPDATE users SET vorname = :vorname, nachname = :nachname, adresse = :adresse, plz = :plz, ort = :ort, geburtstag = :geburtstag, handy = :handy, liga = :liga, verein = :verein, bank = :bank, iban = :iban, id = :id, updated_at=NOW() WHERE id = $id");
$result = $statement->execute(array('vorname' => $vorname, 'nachname'=> $nachname,'adresse' => $adresse, 'plz' => $plz, 'ort' => $ort, 'geburtstag' => $geburtstag, 'handy' => $handy, 'liga' => $liga, 'verein' => $verein,'bank' => $bank, 'iban' => $iban, 'id' => $user['id'] ));
$success_msg = "Daten erfolgreich gespeichert.";
}
} else if($save == 'email') {
$passwort = $_POST['passwort'];
$email = trim($_POST['email']);
$email2 = trim($_POST['email2']);
if($email != $email2) {
$error_msg = "Die eingegebenen E-Mail-Adressen stimmten nicht überein.";
} else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error_msg = "Bitte eine gültige E-Mail-Adresse eingeben.";
} else if(!password_verify($passwort, $user['passwort'])) {
$error_msg = "Bitte korrektes Passwort eingeben.";
} else {
$statement = $pdo->prepare("UPDATE users SET email = :email WHERE id = $id");
$result = $statement->execute(array('email' => $email));
$success_msg = "E-Mail-Adresse erfolgreich gespeichert.";
}
} else if($save == 'passwort') {
$passwortAlt = $_POST['passwortAlt'];
$passwortNeu = trim($_POST['passwortNeu']);
$passwortNeu2 = trim($_POST['passwortNeu2']);
if($passwortNeu != $passwortNeu2) {
$error_msg = "Die eingegebenen Passwörter stimmten nicht überein.";
} else if($passwortNeu == "") {
$error_msg = "Das Passwort darf nicht leer sein.";
} else if(!password_verify($passwortAlt, $user['passwort'])) {
$error_msg = "Bitte korrektes Passwort eingeben.";
} else {
$passwort_hash = password_hash($passwortNeu, PASSWORD_DEFAULT);
$statement = $pdo->prepare("UPDATE users SET passwort = :passwort WHERE id = $id");
$result = $statement->execute(array('passwort' => $passwort_hash));
$success_msg = "Passwort erfolgreich gespeichert.";
}
}
}
?>
<div class="container main-container">
<h1>Schiedsrichter Profil bearbeiten</h1>
<?php
if(isset($success_msg) && !empty($success_msg)):
?>
<div class="alert alert-success">
×
<?php echo $success_msg; ?>
</div>
<?php
endif;
?>
<?php
if(isset($error_msg) && !empty($error_msg)):
?>
<div class="alert alert-danger">
×
<?php echo $error_msg; ?>
</div>
<?php
endif;
?>
<div>
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active">Übersicht</li>
<li role="presentation">Persönliche Daten</li>
<li role="presentation">E-Mail</li>
<li role="presentation">Passwort</li>
</ul>
<!-- Übersicht-->
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="home">
<br>
<form action="?save=personal_data&id=<?php echo $_GET['id'] ?>" method="post" class="form-horizontal">
<div class="form-group">
<label for=inputVorname class="col-sm-2 control-label">Vorname</label>
<div class="col-sm-10">
<input class="form-control" id="inputVorname" name="vorname" type="text" value="<?php echo htmlentities($user['vorname']); ?>" readonly>
</div>
</div>
<div class="form-group">
<label for="inputNachname" class="col-sm-2 control-label">Nachname</label>
<div class="col-sm-10">
<input class="form-control" id="inputNachname" name="nachname" type="text" value="<?php echo htmlentities($user['nachname']); ?>" readonly>
</div>
</div>
<div class="form-group">
<label for="inputAdresse" class="col-sm-2 control-label">Adresse</label>
<div class="col-sm-10">
<input class="form-control" id="inputAdresse" name="adresse" type="text" value="<?php echo htmlentities($user['adresse']); ?>" readonly>
</div>
</div>
<div class="form-group">
<label for="inputPLZ" class="col-sm-2 control-label">PLZ</label>
<div class="col-sm-10">
<input class="form-control" id="inputPLZ" name="plz" type="text" value="<?php echo htmlentities($user['plz']); ?>" readonly>
</div>
</div>
<div class="form-group">
<label for="inputOrt" class="col-sm-2 control-label">Ort</label>
<div class="col-sm-10">
<input class="form-control" id="inputOrt" name="ort" type="text" value="<?php echo htmlentities($user['ort']); ?>" readonly>
</div>
</div>
<div class="form-group">
<label for="inputGeburtstag" class="col-sm-2 control-label">Geburtsdatum</label>
<div class="col-sm-10">
<input class="form-control" id="inputGeburtstag" name="geburtstag" type="text" value="<?php echo htmlentities($user['geburtstag']); ?>" readonly>
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-sm-2 control-label">E-Mail</label>
<div class="col-sm-10">
<input class="form-control" id="inputEmail" name="email" type="email" value="<?php echo htmlentities($user['email']); ?>" readonly>
</div>
</div>
<div class="form-group">
<label for="inputHandy" class="col-sm-2 control-label">Handy</label>
<div class="col-sm-10">
<input class="form-control" id="inputHandy" name="handy" type="text" value="<?php echo htmlentities($user['handy']); ?>" readonly>
</div>
</div>
<div class="form-group">
<label for="inputLiga" class="col-sm-2 control-label">Liga</label>
<div class="col-sm-10">
<input class="form-control" id="inputLiga" name="liga" type="text" value="<?php echo htmlentities($user['liga']); ?>" readonly>
</div>
</div>
<div class="form-group">
<label for="inputVerein" class="col-sm-2 control-label">Verein</label>
<div class="col-sm-10">
<input class="form-control" id="inputVerein" name="verein" type="text" value="<?php echo htmlentities($user['verein']); ?>" readonly>
</div>
</div>
<div class="form-group">
<label for="inputBank" class="col-sm-2 control-label">Bankname</label>
<div class="col-sm-10">
<input class="form-control" id="inputBank" name="bank" type="text" value="<?php echo htmlentities($user['bank']); ?>" readonly>
</div>
</div>
<div class="form-group">
<label for="inputIban" class="col-sm-2 control-label">IBAN</label>
<div class="col-sm-10">
<input class="form-control" id="inputIban" name="iban" type="text" value="<?php echo htmlentities($user['iban']); ?>" readonly>
</div>
</div>
</form>
</div>
<!-- Persönliche Daten-->
<div role="tabpanel" class="tab-pane" id="data">
<br>
<form action="?save=personal_data&id=<?php echo $_GET['id'] ?>" method="post" class="form-horizontal">
<div class="form-group">
<label for="inputVorname" class="col-sm-2 control-label">Vorname</label>
<div class="col-sm-10">
<input class="form-control" id="inputVorname" name="vorname" type="text" value="<?php echo htmlentities($user['vorname']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputNachname" class="col-sm-2 control-label">Nachname</label>
<div class="col-sm-10">
<input class="form-control" id="inputNachname" name="nachname" type="text" value="<?php echo htmlentities($user['nachname']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputAdresse" class="col-sm-2 control-label">Adresse</label>
<div class="col-sm-10">
<input class="form-control" id="inputAdresse" name="adresse" type="text" value="<?php echo htmlentities($user['adresse']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputPLZ" class="col-sm-2 control-label">PLZ</label>
<div class="col-sm-10">
<input class="form-control" id="inputPLZ" name="plz" type="text" value="<?php echo htmlentities($user['plz']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputOrt" class="col-sm-2 control-label">Ort</label>
<div class="col-sm-10">
<input class="form-control" id="inputOrt" name="ort" type="text" value="<?php echo htmlentities($user['ort']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputGeburtstag" class="col-sm-2 control-label">Geburtsdatum</label>
<div class="col-sm-10">
<input class="form-control" id="inputGeburtstag" name="geburtstag" type="text" value="<?php echo htmlentities($user['geburtstag']); ?>" placeholder="01.01.2000">
</div>
</div>
<div class="form-group">
<label for="inputHandy" class="col-sm-2 control-label">Handy</label>
<div class="col-sm-10">
<input class="form-control" id="inputHandy" name="handy" type="text" value="<?php echo htmlentities($user['handy']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputLiga" class="col-sm-2 control-label">Liga</label>
<div class="col-sm-10">
<input class="form-control" id="inputLiga" name="liga" type="text" value="<?php echo htmlentities($user['liga']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputVerein" class="col-sm-2 control-label">Verein</label>
<div class="col-sm-10">
<input class="form-control" id="inputVerein" name="verein" type="text" value="<?php echo htmlentities($user['verein']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputBank" class="col-sm-2 control-label">Bankname</label>
<div class="col-sm-10">
<input class="form-control" id="inputBank" name="bank" type="text" value="<?php echo htmlentities($user['bank']); ?>" placeholder="Postfinance">
</div>
</div>
<div class="form-group">
<label for="inputIban" class="col-sm-2 control-label">IBAN</label>
<div class="col-sm-10">
<input class="form-control" id="inputIban" name="iban" type="text" value="<?php echo htmlentities($user['iban']); ?>" placeholder="CHxx xxxx xxxx xxxx xxxx x">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">Speichern</button>
<a class="btn btn-danger" href='internal.php'>Abbrechen</a>
</div>
</div>
</form>
</div>
<!-- Änderung der E-Mail-Adresse -->
<div role="tabpanel" class="tab-pane" id="email">
<br>
<p>Zum Änderen deiner E-Mail-Adresse gib bitte dein aktuelles Passwort sowie die neue E-Mail-Adresse ein.</p>
<form action="?save=email&id=<?php echo $_GET['id'] ?>" method="post" class="form-horizontal">
<div class="form-group">
<label for="inputPasswort" class="col-sm-2 control-label">Passwort</label>
<div class="col-sm-10">
<input class="form-control" id="inputPasswort" name="passwort" type="password" required>
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-sm-2 control-label">E-Mail</label>
<div class="col-sm-10">
<input class="form-control" id="inputEmail" name="email" type="email" value="<?php echo htmlentities($user['email']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputEmail2" class="col-sm-2 control-label">E-Mail (wiederholen)</label>
<div class="col-sm-10">
<input class="form-control" id="inputEmail2" name="email2" type="email" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">Speichern</button>
<a class="btn btn-danger" href='spielliste.php'>Abbrechen</a>
</div>
</div>
</form>
</div>
<!-- Änderung des Passworts -->
<div role="tabpanel" class="tab-pane" id="passwort">
<br>
<p>Zum Änderen deines Passworts gib bitte dein aktuelles Passwort sowie das neue Passwort ein.</p>
<form action="?save=passwort&id=<?php echo $_GET['id'] ?>" method="post" class="form-horizontal">
<div class="form-group">
<label for="inputPasswort" class="col-sm-2 control-label">Altes Passwort</label>
<div class="col-sm-10">
<input class="form-control" id="inputPasswort" name="passwortAlt" type="password" required>
</div>
</div>
<div class="form-group">
<label for="inputPasswortNeu" class="col-sm-2 control-label">Neues Passwort</label>
<div class="col-sm-10">
<input class="form-control" id="inputPasswortNeu" name="passwortNeu" type="password" required>
</div>
</div>
<div class="form-group">
<label for="inputPasswortNeu2" class="col-sm-2 control-label">Neues Passwort (wiederholen)</label>
<div class="col-sm-10">
<input class="form-control" id="inputPasswortNeu2" name="passwortNeu2" type="password" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">Speichern</button>
<a class="btn btn-danger" href='spielliste.php'>Abbrechen</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<?php
include("templates/footer.inc.php")
?>
UPDATE: new code editsr.php. Works fine with the tabs, only the changes displayed after refresh the site.
Look at your code carefully
on the top you are using the $user variable like below
$id = $_GET['id'];
$statement = $pdo->prepare("SELECT * FROM users WHERE id = :id");
$result = $statement->execute(array('id' => ['id']));
$user = $statement->fetch(); // here
and again on the bottom of php code you are using $user variable like below
$id = $_GET['id'];
$user = "SELECT * FROM users WHERE id = :id"; // here
?>
on the input you are trying to get like htmlentities($user['vorname']);
<div class="form-group">
<label for=inputVorname class="col-sm-2 control-label">Vorname</label>
<div class="col-sm-10">
<input class="form-control" id="inputVorname" name="vorname" type="text" value="<?php echo htmlentities($user['vorname']); ?>" readonly>
</div>
</div>
that's the problem of error in every input :)
New Code with only one form:
<?php
session_start();
require_once("inc/config.inc.php");
require_once("inc/functions.inc.php");
$id = $_GET['id'];
$statement = $pdo->prepare("SELECT * FROM users WHERE id = $id");
$result = $statement->execute(array('id' => ['id']));
$user = $statement->fetch();
include("templates/header.inc.php");
if(isset($_GET['save'])) {
$save = $_GET['save'];
if($save == 'personal_data') {
$vorname = trim($_POST['vorname']);
$nachname = trim($_POST['nachname']);
$adresse = trim($_POST['adresse']);
$plz = trim($_POST['plz']);
$ort = trim($_POST['ort']);
$geburtstag = trim($_POST['geburtstag']);
$handy = trim($_POST['handy']);
$email = trim($_POST['email']);
$liga = trim($_POST['liga']);
$verein = trim($_POST['verein']);
$bank = trim($_POST['bank']);
$iban = trim($_POST['iban']);
$passwortNeu = trim($_POST['passwortNeu']);
if($vorname == "" || $nachname == "" || $adresse == "" || $plz == "" || $ort == "" || $handy == "" || $liga == "" || $verein == "" || $email == "" || $passwortNeu == "" ){
$error_msg = "Bitte alle Angaben ausfüllen.";
} else {
$passwort_hash = password_hash($passwortNeu, PASSWORD_DEFAULT);
$statement = $pdo->prepare("UPDATE users SET vorname = :vorname, nachname = :nachname, adresse = :adresse, plz = :plz, ort = :ort, geburtstag = :geburtstag, handy = :handy, liga = :liga, verein = :verein, bank = :bank, iban = :iban, passwort = :passwort, email = :email, updated_at=NOW() WHERE id = $id");
$result = $statement->execute(array('vorname' => $vorname, 'nachname'=> $nachname,'adresse' => $adresse, 'plz' => $plz, 'ort' => $ort, 'geburtstag' => $geburtstag, 'handy' => $handy, 'liga' => $liga, 'verein' => $verein,'bank' => $bank, 'iban' => $iban, 'passwort' => $passwort_hash, 'email' => $email));
$success_msg = "Daten erfolgreich gespeichert.";
}
}
}
?>
<div class="container main-container">
<h1>Schiedsrichter Profil bearbeiten</h1>
<?php
if(isset($success_msg) && !empty($success_msg)):
?>
<div class="alert alert-success">
×
<?php echo $success_msg; ?>
</div>
<?php
endif;
?>
<?php
if(isset($error_msg) && !empty($error_msg)):
?>
<div class="alert alert-danger">
×
<?php echo $error_msg; ?>
</div>
<?php
endif;
?>
<div>
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active">Persönliche Daten</li>
</ul>
<!-- Persönliche Daten-->
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="data">
<br>
<form action="?save=personal_data" method="post" class="form-horizontal">
<div class="form-group">
<label for="inputVorname" class="col-sm-2 control-label">Vorname</label>
<div class="col-sm-10">
<input class="form-control" id="inputVorname" name="vorname" type="text" value="<?php echo htmlentities($user['vorname']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputNachname" class="col-sm-2 control-label">Nachname</label>
<div class="col-sm-10">
<input class="form-control" id="inputNachname" name="nachname" type="text" value="<?php echo htmlentities($user['nachname']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputAdresse" class="col-sm-2 control-label">Adresse</label>
<div class="col-sm-10">
<input class="form-control" id="inputAdresse" name="adresse" type="text" value="<?php echo htmlentities($user['adresse']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputPLZ" class="col-sm-2 control-label">PLZ</label>
<div class="col-sm-10">
<input class="form-control" id="inputPLZ" name="plz" type="text" value="<?php echo htmlentities($user['plz']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputOrt" class="col-sm-2 control-label">Ort</label>
<div class="col-sm-10">
<input class="form-control" id="inputOrt" name="ort" type="text" value="<?php echo htmlentities($user['ort']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputGeburtstag" class="col-sm-2 control-label">Geburtsdatum</label>
<div class="col-sm-10">
<input class="form-control" id="inputGeburtstag" name="geburtstag" type="text" value="<?php echo htmlentities($user['geburtstag']); ?>" placeholder="01.01.2000">
</div>
</div>
<div class="form-group">
<label for="inputHandy" class="col-sm-2 control-label">Handy</label>
<div class="col-sm-10">
<input class="form-control" id="inputHandy" name="handy" type="text" value="<?php echo htmlentities($user['handy']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-sm-2 control-label">E-Mail</label>
<div class="col-sm-10">
<input class="form-control" id="inputEmail" name="email" type="email" value="<?php echo htmlentities($user['email']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputLiga" class="col-sm-2 control-label">Liga</label>
<div class="col-sm-10">
<input class="form-control" id="inputLiga" name="liga" type="text" value="<?php echo htmlentities($user['liga']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputVerein" class="col-sm-2 control-label">Verein</label>
<div class="col-sm-10">
<input class="form-control" id="inputVerein" name="verein" type="text" value="<?php echo htmlentities($user['verein']); ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputBank" class="col-sm-2 control-label">Bankname</label>
<div class="col-sm-10">
<input class="form-control" id="inputBank" name="bank" type="text" value="<?php echo htmlentities($user['bank']); ?>" placeholder="Postfinance">
</div>
</div>
<div class="form-group">
<label for="inputIban" class="col-sm-2 control-label">IBAN</label>
<div class="col-sm-10">
<input class="form-control" id="inputIban" name="iban" type="text" value="<?php echo htmlentities($user['iban']); ?>" placeholder="CHxx xxxx xxxx xxxx xxxx x">
</div>
</div>
<div class="form-group">
<label for="inputPasswortNeu" class="col-sm-2 control-label">Neues Passwort</label>
<div class="col-sm-10">
<input class="form-control" id="inputPasswortNeu" name="passwortNeu" type="password" value="<?php echo htmlentities($user['passwort']); ?>" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">Speichern</button>
<a class="btn btn-danger" href='internal.php'>Abbrechen</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<?php
include("templates/footer.inc.php")
?>

While loop displays one record multiple times php and mysql issue

I am trying to fetch data from the database. But while loop is executing and displaying data multiple times. Please see the screen shot
<?php
$conn = mysqli_connect("localhost", "root", "","midata");
$sql = "SELECT * FROM campaign_profile ORDER BY id DESC";
?>
<div class="modal-body">
<form>
<?php
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result))
{
$name= $row['campaign_name'];
$issues= $row['issue_name'];
$ename= $row['unused_elements'];
$uquantity= $row['unused_number'];
$descript= $row['experience']; ?>
<div class="form-group">
<label for="recipient-name" class="control-label">Campaign Name:</label>
<input type="text" class="form-control c-square" id="recipient-name" value="<?php echo $row['campaign_name'];?>">
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">Issues:</label>
<input type="text" class="form-control c-square" id="recipient-name" value="<?php echo $row['issue_name'];?>">
</div>
<div class="row col-md-12">
<div class="form-group col-md-6">
<label for="recipient-name" class="control-label">Element Name:</label>
<input type="text" class="form-control c-square" id="recipient-name" class="col-md-6" value="<?php echo $row['unused_elements'];?>">
</div>
<div class="form-group col-md-6">
<label for="recipient-name" class="control-label">Unused Quantity:</label>
<input type="text" class="form-control c-square" id="recipient-name" class="col-md-6" value="<?php echo $row['unused_number'];?>">
</div>
</div>
<div class="form-group">
<label for="message-text" class="control-label">Description:</label>
<textarea class="form-control c-square" id="message-text" value="<?php echo $row['experience'];?>"></textarea>
</div>
<?php } ?>
</form>
</div>
I don't understand why this is happening. Do let me know what needs to be done
May be your table has more rows then you are expecting.
limit your query using this.
$sql = "SELECT * FROM campaign_profile ORDER BY id DESC limit 1";
or try to fetch data using id or primary key.
or change your code with given below.
<?php
$conn = mysqli_connect("localhost", "root", "","midata");
$sql = "SELECT * FROM campaign_profile ORDER BY id DESC";
?>
<div class="modal-body">
<form>
<?php
$name= "";
$issues= "";
$ename= "";
$uquantity= "";
$descript= "";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result))
{
$name= $row['campaign_name'];
$issues= $row['issue_name'];
$ename= $row['unused_elements'];
$uquantity= $row['unused_number'];
$descript= $row['experience'];
} ?>
<div class="form-group">
<label for="recipient-name" class="control-label">Campaign Name:</label>
<input type="text" class="form-control c-square" id="recipient-name" value="<?php echo $row['campaign_name'];?>">
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">Issues:</label>
<input type="text" class="form-control c-square" id="recipient-name" value="<?php echo $row['issue_name'];?>">
</div>
<div class="row col-md-12">
<div class="form-group col-md-6">
<label for="recipient-name" class="control-label">Element Name:</label>
<input type="text" class="form-control c-square" id="recipient-name" class="col-md-6" value="<?php echo $row['unused_elements'];?>">
</div>
<div class="form-group col-md-6">
<label for="recipient-name" class="control-label">Unused Quantity:</label>
<input type="text" class="form-control c-square" id="recipient-name" class="col-md-6" value="<?php echo $row['unused_number'];?>">
</div>
</div>
<div class="form-group">
<label for="message-text" class="control-label">Description:</label>
<textarea class="form-control c-square" id="message-text" value="<?php echo $row['experience'];?>"></textarea>
</div>
</form>
</div>
Try to analyze the query result by directly running the mysql select query by terminal or phpmyadmin tool. Please check the result of the query with php code, If both are same your table contains duplication data.
If want show single record means no need while loop just do it like below
Limit the record by adding limit 1
SELECT * FROM campaign_profile ORDER BY id DESC limit 1
PHP :
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($result);
$name= $row['campaign_name'];
$issues= $row['issue_name'];
$ename= $row['unused_elements'];
$uquantity= $row['unused_number'];
$descript= $row['experience'];

Data wont insert into Database

I'm trying to insert data from a form to my database but it doesn't seem to work. I've put an echo after the insert query so I can verify that the data was inserted but it doesn't echo what I've written. Is there a problem with my query or any part of my php?
My PHP:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "stat_system";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$fname = $lname = $mname = $contact = $age = $attain = $course = $school = $position = $exp = $ref = $batchtxt = $hiredate = $prevbpo = $remarks = $nho = $nonbpo = $holdAttain = $holdPos = "";
$error_flag = 0;
if (isset($_POST['submit'])) {
$holdAttain = (isset($_POST['cmbAttain']));
$holdPos = (isset($_POST['cmbPosition']));
if (!empty($_POST['firstname'])) {
$fname = $_POST['firstname'];
}
if (!empty($_POST['lastname'])) {
$lname = $_POST['lastname'];
}
if (!empty($_POST['middlename'])) {
$mname = $_POST['middlename'];
}
if (!empty($_POST['contact'])) {
$contact = $_POST['contact'];
}
if (!empty($_POST['age'])) {
$age = $_POST['age'];
}
if (isset($_POST['cmbAttain'])) {
$attain = $_POST['cmbAttain'];
}
if(isset($_POST['school'])) {
$school = $_POST['school'];
}
if(isset($_POST['course'])) {
$course = $_POST['course'];
}
if (isset($_POST['exp'])) {
$exp = $_POST['exp'];
}
if (!empty($_POST['remarks'])) {
$remarks = $_POST['remarks'];
}
if (isset($_POST['nonbpo'])) {
$nonbpo = $_POST['nonbpo'];
}
if (isset($_POST['prevbpo'])) {
$prevbpo = $_POST['prevbpo'];
}
if (!empty($_POST['ref'])) {
$ref = $_POST['ref'];
}
if (isset($_POST['hiredate'])) {
$hiredate = $_POST['hiredate'];
}
if (isset($_POST['batchtxt'])) {
$batchtxt = $_POST['batchtxt'];
}
if (!empty($_POST['nho'])) {
$nho = $_POST['nho'];
}
if($error_flag == 0){
$sql = mysqli_query($conn,"INSERT INTO applicants (appID, appLastName, appFirstName, appMidleName, Age, appPhoneNumber, appBatch, appExperience, appRemarks, appPreviousBPO, appSchool, appCourse, appGraduate, appNonBPO, appPosition, appHireDate, appNHO, appReferrer)
VALUES (NULL, '$lname', '$fname', '$mname', $age, '$contact', $batchtxt, '$exp', $remarks, '$school', '$course', '$attain', '$nonbpo', '$position', $hiredate, $nho, '$ref')");
echo "<script type='text/javascript'>
$(document).ready(function(){
$('#succModal').modal('show');
});
</script>";
$fname = $lname = $mname = $contact = $age = $attain = $course = $school = $batchtxt = $ref = $hiredate = $position = $exp = $prevbpo = $remarks = $nho = $nonbpo = $holdAttain = $holdPos = "";
}
else {
print '<script type="text/javascript">';
print 'alert("Please fill in all the fields!")';
print '</script>';
}
}
mysqli_close($conn);
?>
My HTML:
<div id="addApplicant" class="addApp-marginleft" style="height:1000px">
<form id="registration" class="form-horizontal" method="post" action="index.php">
<div class="row">
<div align="center">
<h3>Add Applicant</h3>
<br>
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>First name:</small></label>
<div class="col-sm-4">
<input required type="text" name="firstname" autocomplete="off" placeholder="Firstname" id="firstname" class="form-control" value="<?php echo $fname;?>">
</div>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small> Last name:</small></label>
<div class="col-sm-4">
<input required type="text" name="lastname" autocomplete="off" id="lastname" placeholder="Lastname" class="form-control" value="<?php echo $lname;?>">
</div>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Middle name:</small></label>
<div class="col-sm-4">
<input required type="text" name="middlename" autocomplete="off" id="middlename" placeholder="middlename" class="form-control" value="<?php echo $mname;?>">
</div>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Contact number:</small></label>
<span id="errmsg"></span>
<div class="col-sm-4">
<input required type="text" name="contactnum" autocomplete="off" onkeypress="return isNumber(event)" placeholder="Contact number" id="contact" class="form-control" maxlength="11" value="<?php echo $contact;?>"/>
</div>
<?php
echo '<script>';
echo 'function isNumber(evt) {';
echo 'evt = (evt) ? evt : window.event;';
echo 'var charCode = (evt.which) ? evt.which : evt.keyCode;';
echo 'if (charCode > 31 && (charCode < 48 || charCode > 57)) {';
echo 'return false;';
echo '}';
echo 'return true;';
echo '}';
echo '</script>';
?>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Age:</small></label>
<div class="col-sm-4">
<input required type="text" autocomplete="off" placeholder="age" name="age" id="age" class="form-control" value="<?php echo $age;?>">
</div>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Graduate:</small></label>
<div class="col-sm-4">
<select required name="cmbAttain" id="cmbAttain" class="form-control" onChange="disableCmb();">
<option value="">Choose</option>
<option value="Yes" <?php if($holdAttain == "Yes") echo "selected"; ?>>Yes</option>
<option value="No" <?php if($holdAttain == "No") echo "selected"; ?>>No</option>
</select>
</div>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>School:</small></label>
<div class="col-sm-4">
<input type="text" name="school" autocomplete="off" id="school" placeholder="School" class="form-control" value="<?php echo $school;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Course:</small></label>
<div class="col-sm-4">
<input type="text" name="course" autocomplete="off" id="course" placeholder="Course" class="form-control" value="<?php echo $course;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Batch:</small></label>
<div class="col-sm-4">
<input type="text" name="batchtxt" autocomplete="off" id="batchtxt" placeholder="Batch" class="form-control" value="<?php echo $batchtxt;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Experience:</small></label>
<div class="col-sm-4">
<input type="text" name="exp" autocomplete="off" id="exp" placeholder="Experience" class="form-control" value="<?php echo $exp;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Previous BPO:</small></label>
<div class="col-sm-4">
<input type="text" name="prevbpo" autocomplete="off" id="prevbpo" placeholder="Previous BPO" class="form-control" value="<?php echo $prevbpo;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Non-BPO:</small></label>
<div class="col-sm-4">
<input type="text" name="nonbpo" autocomplete="off" id="nonbpo" placeholder="Non-BPO" class="form-control" value="<?php echo $nonbpo;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Remarks:</small></label>
<div class="col-sm-4">
<input type="text" name="remarks" autocomplete="off" id="remarks" placeholder="Remarks" class="form-control" value="<?php echo $remarks;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Hire Date:</small></label>
<div class="col-sm-4">
<input type="date" name="hiredate" id="hiredate" class="form-control" autocomplete="off" value="<?php echo $hiredate;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Position:</small></label>
<div class="col-sm-4">
<select required name="cmbPosition" id="cmbPosition" class="form-control" data-size="5" >
<option selected value="">Choose</option>
<option value="Customer Service Representative" <?php if($holdPos == "Customer Service Representative") echo "selected"; ?>>Customer Service Representative</option>
<option value="Image Enhancer" <?php if($holdPos == "Image Enhancer") echo "selected"; ?>>Image Enhancer</option>
</select>
</div>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>NHO:</small></label>
<div class="col-sm-4">
<input type="date" name="nho" id="nho" class="form-control" autocomplete="off" value="<?php echo $nho;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Referrer:</small></label>
<div class="col-sm-4">
<input type="text" name="ref" autocomplete="off" id="ref" placeholder="Name of Referrer" class="form-control" value="<?php echo $ref;?>">
</div>
</div>
<div class="form-group" align="center" >
<div class="col-sm-4">
</div>
<div class="col-sm-4">
<div class="btn-group " role="group" aria-label="...">
<input id="submitbtn" type="submit" name="submit" class="btn btn btn-success" value="Submit" data-target="#succModal">
<input type="reset" name="reset" class="btn btn-warning" value="Clear">
</div>
</div>
<div class="col-sm-4">
</div>
</div>
</form>
</div>
<div class="container">
<!-- Register Success Modal -->
<div class="modal fade" id="succModal" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-body" align="center">
<p>REGISTRATION SUCCESSFUL</p>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
use $_POST instead of $_REQUEST..
like:
$lname = $_POST['lname'];
Just at a glance, you're NULL tests should be seperated by &&, not ||. The way you have it, it's only checking whether one (any) of them contain a value.
There is some errors with you query, just replace it with
$sql = "INSERT INTO applicants (appLastName, appFirstName, appMidleName, Age, appPhoneNumber, appBatch, appExperience, appRemarks, appPreviousBPO, appSchool, appCourse, appGraduate, appNonBpo, appPosition, appHireDate, appNHO, appReferrer) VALUES ('$lname','$fname','$mname',$age,'$con',$batch,'$exp','$rem','$prevbpo','$school','$course','$gradsit', '$nbpo', '$pos', '$hdate', '$nho', '$ref')";
here, there is no need to add appid as it is auto-incremented. Also there are some missing single quotes.
Skip to send value NULL in your insert statement
where you check whether $_POST is empty, if it is empty set $error_flag=1
Inside if (isset($_POST['submit'])) { print $_POST
Hope this will help to debug. If still issue, check your error_log

My code works fine when i run this code on apache. But when i implement it on iis, it doesnt work

<?php
session_start();
include "../../lib/mssql.connect.php";
$params = array($_POST['id']);
$sql = "SELECT * FROM adminIP WHERE id = ?";
//run the query
$result = sqlsrv_query($conn, $sql,$params);
while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
{
$id[] = array('id' =>$row['id']);
$name[] = array('adminname' =>$row['adminName']);
$ip[] = array('adminip' =>$row['adminIP']);
}
?>
<form method = "post" action="">
<div class="col-lg-10">
<label class="col-lg-2 control-label">Name</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="txtname" value="<? echo implode(",", $name[0]) ?>" disabled="">
</div> <br/>
<label class="col-lg-2 control-label">IP</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="txtip" value="<? echo implode(",", $ip[0]) ?>" data-content="Please enter your IP Address" data-container="body" data-toggle="popover" data-placement="bottom">
</div>
<div class="col-lg-10">
<input type="text" class="form-control" id="txtid" value="<? echo implode(",", $id[0]) ?>"style="display: none">
</div>
</div>
</form>
<?php
?>

Categories