For loop showing only the final result - php

I have the following PHP query:
<?php
$row='';
$access = 0;
$user = (isset($_POST['nume']) && !empty($_POST['nume'])) ? $_POST['nume'] : "" ;
$email = (isset($_POST['email']) && !empty($_POST['email'])) ? $_POST['email'] : "" ;
$telefon = (isset($_POST['telefon']) && !empty($_POST['telefon'])) ? $_POST['telefon'] : "" ;
$cnp = (isset($_POST['cnp']) && !empty($_POST['cnp'])) ? $_POST['cnp'] : "" ;
$serieci = (isset($_POST['serieci']) && !empty($_POST['serieci'])) ? $_POST['serieci'] : "" ;
$mesaj = (isset($_POST['mesaj']) && !empty($_POST['mesaj'])) ? $_POST['mesaj'] : "" ;
$email_status=1;
$db = pg_connect("host=local port=5432 dbname=login_robinson user=robinson password=123");
$query = "INSERT INTO tcheckin(nume,email,telefon,cnp,serieci,mesaj) VALUES ('$user','$email','$telefon','$cnp','$serieci','$mesaj')";
$result = pg_query($query);
$test=$_SESSION[PRE.'nume'];
$test1=$_SESSION[PRE.'uid'];
?>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<link href="https://fonts.googleapis.com/css?family=Playfair+Display:400,700,900|Rubik:300,400,700" rel="stylesheet">
<title>Efectueaza check-in</title>
<?php
include('include/links.php');
include('include/scripts.php');
?>
</head>
<?php
$i=1;
$conn = pg_connect("host=local port=5432 dbname=login_robinson user=robinson password=123");
$test1=$_SESSION[PRE.'uid'];
$result = pg_query($conn, "SELECT nume FROM tregister WHERE id = '$test1'");
if (!$result) {
echo "An error occurred.\n";
exit;
}
$row = pg_fetch_row($result);
$result1 = pg_query($conn, "SELECT nr_camere FROM tregister WHERE nume = '$user'");
if (!$result1) {
echo "An error occurred.\n";
exit;
}
$camere = pg_fetch_row($result1);
echo $row[0];
for($i=1; $i<$camere[0]; $i++){
if(isset($_POST['submit'])){
echo "<script>
alert('Check-in efectuat cu success! Daca ai rezervat mai multe camere te rog sa completezi si check-in-ul pentru celelalte camere.');
window.location.href='acasa';
</script>";
echo "<script>
alert('S-a produs o eroare! Te rog mai verifica odata formularul!');
</script>";
}
}
?>
<body style="display: block !important;" ng-cloak="" ng-class="{ 'layout-fixed' : app.layout.isFixed, 'layout-boxed' : app.layout.isBoxed, 'layout-dock' : app.layout.isDocked, 'layout-material': app.layout.isMaterial, 'aside-offscreen' : app.sidebar.isOffscreen, 'aside-mini' : app.sidebar.isMini, 'aside-right' : app.sidebar.isRight, 'footer-hidden': app.footer.hidden, 'in-app': !$state.includes('page')}">
<div class="animated fadeOutZoom">
<div class="container container-sm animated fadeInDown">
<div class="app-view-header">Bine ai venit, <?php echo $row[0]; ?>!</div>
<div class="center-block mt-xl">
<div class="panel">
<div class="panel-body">
<p class="pv text-bold">Check-in pentru camera cu numarul <?php echo $i; ?></p>
<form action="#" method="post">
<div class="row">
<div class="col-md-12 form-group">
<label for="nume">Nume</label>
<input type="text" id="text " value=" <?php echo $row[0]; ?>" class="form-control " name="nume" disabled="disabled">
</div>
</div>
<div class="row">
<div class="col-md-12 form-group">
<label for="telefon">Telefon</label>
<input type="phone" id="telefon " class="form-control " name="telefon" required>
</div>
</div>
<div class="row">
<div class="col-md-12 form-group">
<label for="email">Email</label>
<input type="email" id="email" class="form-control " name="email" required>
</div>
</div>
<div class="row">
<div class="col-md-12 form-group">
<label for="cnp">CNP</label>
<input type="text" id="text " class="form-control "name="cnp" required>
</div>
</div>
<div class="row">
<div class="col-md-12 form-group">
<label for="serie">Serie de buletin</label>
<input type="text" id="text " class="form-control " name="serieci" required>
</div>
</div>
<div class="row">
<div class="col-md-12 form-group">
<label for="message">Scrie un mesaj *Optional</label>
<textarea name="mesaj" id="message" class="form-control " cols="5" rows="4"></textarea>
</div>
</div>
<div class="row">
<div class="col-md-6 form-group">
<input type="submit" value="Rezerva acum" class="btn btn-primary" name="submit">
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/js/bootstrap-datepicker.min.js"></script>
<script>
$('#arrival_date, #departure_date').datepicker({format: 'dd/mm/yyyy'});
</script>
</body>
</html>
<?php
ob_end_flush();
?>
What I want to do it's to see a page for a number of times. For example, if a user selected 4 rooms, I need make the check-in process 4 times, once for each room, I tried with a for loop but it's showing only the final result..what can I do? It's a little bit weird, I tried also with a while but still doesn't work..

Related

PHP doenst catch a simple POST

Here below is my code, i get strange results, for example the POST is not being caught by the PHP. If i add ($_SERVER['REQUEST_METHOD'] == 'POST') it is still not caught. It hould be a simple POST, what did i do wrong? The best i get out of this is the error message:
$url_message = 'Something went wro, please try again.';
Please help
if(isset($_POST["submit"])) {
$btc = $_POST['bitcoin'];
$eur = $_POST['euro'];
echo '1';
echo $btc;
echo '2';
echo $eur;
if($btc != "" && $eur != ""){
$user_idr = $_GET['id'];
$user_idreal = $LS->get_CU();
echo $LS->get_CU();
echo $user_idr;
if($user_idr == $user_idreal){
echo 'jemoder';
$cur = $btc;
$eur = $eur;
$user = $user_id;
$datetime = date('Y-m-d H:i:s');
$ip = $_SERVER['REMOTE_ADDR'];
if($LS->store_val($cur, $eur, $user, $datetime, $ip) != false){
$url_message = 'Gelukt';
header('Location: home.php?id=' . $header_n . '&mas=' . $url_message);
}else{
$url_message = 'niet gelukt';
header('Location: home.php?id=' . $header_n . '&mas=' . $url_message);
}
}else{
$header_n = $LS->get_CU();
$url_message = 'Something went wro, please try again.';
header('Location: home.php?id=' . $header_n . '&mas=' . $url_message);
}
}else{
$header_n = $LS->get_CU();
$url_message = 'Some fields were left blank.';
header('Location: home.php?id=' . $header_n . '&mas=' . $url_message);
}
}else{
echo 'hallo';
}
?>
<div class="col-md-9">
<div class="span9">
<div class="space-6"></div>
<div class="row-fluid">
<div class="span12">
<form class="form-horizontal formproperties" action="<?php echo $_SERVER['PHP_SELF'] ?>#form-textfield" method="POST">
<fieldset>
<div class="container">
<div class="row-fluid">
<div class="col-xs-12 col-sm-12 col-lg-12">
<div class="col-xs-12 col-sm-12 col-lg-6 logintextsupport text-center">
<h3 class="text-left">Buy <?php echo $form_titel;?></h3>
<hr />
<div class="form-group">
<label class="col-md-1 control-label" for="password"></label>
<div class="col-md-9">
<input id="<?php echo $form_id;?>" name="<?php echo $form_name;?>" type="text" placeholder="<?php echo $form_placeholder;?> *" class="form-control input-md" required="">
</div>
</div>
<div class="form-group">
<label class="col-md-1 control-label" for="password"></label>
<div class="col-md-9">
<input id="euro" name="euro" type="text" placeholder=" *" class="form-control input-md" required="">
</div>
</div>
<legend>Payment Method</legend>
<div class="form-group">
<label class="col-md-1 control-label" for="password"></label>
<div class="col-md-5">
<input type="radio" name="radio" id="radio1" /> <label for="radio1" class="inline">iDeal</label><br />
</div>
<div class="col-md-4">
<input type="radio" name="radio" id="radio3" /> <label for="radio3" class="inline">Ban Contact</label>
</div>
</div>
<hr />
<div class="form-group loginboxsupport">
<label class="col-md-12 col-lg-12" for="proceed"></label>
<div class="col-md-12 col-lg-12">
<button id="proceed" type="submit" name="submit" class="btn btn-success buttonali">SUBMIT</button>
</div>
</div>
<div style="height:0.5vw;"></div>
</div>
</div>
</div>
</div>
</fieldset>
</form>
The problem was in the HTML code, at the action there is the following code
action="<?php echo $_SERVER['PHP_SELF'] ?>#form-textfield"
So the action would be a link to itself, nothing on it. Had to add a user ID:
?id=<?php echo $user_id ?>
The complete action became:
action="<?php echo $_SERVER['PHP_SELF'] ?>?id=<?php echo $user_id ?>#form-textfield"
The total start of the form:
<form class="form-horizontal formproperties" action="home.php?id=<?php echo $user_id ?>#form-textfield" method="POST">

How to Populate fields using URL parametres in using php and mysql

I have a URL link, and upon clicking the link, it has to populate the fields as soon as the page is opened, below is the URL link and the form that has to be populated with few fields,fields such as names, idocode and email has to be populated, and the rest by the user and the submit button has no effect, what am I doing wrong,
URL: 'http://www.domain.com/register/registration.php?token=$token&stud_id=stud_id'
<?php
error_reporting(1);
session_start();
include 'includes/connect.php';
$student_id = $_GET["id"];
$_SESSION['student_id'] = $student_id;
$token = $_GET["tokenk"];
$_SESSION['token'] = $token;
if (isset($_GET["id"]) && isset($_GET["tk"])) {
$sql = "SELECT * FROM student WHERE student_id=" . $student_id . " and token='" . $token . "'";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if (!empty($result)) {
$sql = "UPDATE student SET first_name=:first_name, middle_name=:middle_name, last_name=:last_name WHERE student_id=:student_id";
$stmt->bindValue(':first_name', $first_name);
$stmt->bindValue(':middle_name', $middle_name);
$stmt->bindValue(':last_name', $last_name);
$stmt->bindValue(':student_id', $student_id);
$result = $stmt->execute();
} else {
}
}
if ($_POST["Submit"] == "Submit") {
//echo '<pre>';print_r($_POST["name"]);exit;
$ccode = $_POST["idode"];
$first_name = $_POST['first_name'];
$middle_name = $_POST['middle_name'];
$last_name = $_POST['last_name'];
$course = $_POST["course"];
$email = $_POST['email'];
$password = $_POST["password"];
$confirm_password = $_POST["confirm_password"];
//echo "<br/>In post"; die();
//echo $name.'>>>>'.$email.'>>>>'.$idcode;exit;
add_student_session($_POST);
$validate = add_student_validator($_POST);
//echo '<pre>';print_r($validate);exit;
if (isset($validate) && $validate) {
//echo "<br/>Validated: TRUE<br/>"; die();
$sql = "SELECT COUNT(*) AS num FROM student WHERE student_id = :student_id";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':idcode', $idcode);
$stmt->bindValue(':first_name', $first_name);
$stmt->bindValue(':middle_name', $middle_name);
$stmt->bindValue(':last_name', $last_name);
$stmt->bindValue(':course', $course);
$stmt->bindValue(':date_of_birth', $date_of_birth);
$stmt->bindValue(':email', $email);
$stmt->bindValue(':password', $password);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($row['num'] > 0) {
$_SESSION["already_exist"] = TRUE;
} else {
//echo "<br/>Validated: FALSE<br/>"; die();
$sql = "INSERT INTO student (course, date_of_birth, password) VALUES (:course, :date_of_birth, :password)";
$stmt = $pdo->prepare($sql);
//echo "<br/>CCODE: ".$idcode;exit;
$stmt->bindValue(':course', $course);
$stmt->bindValue(':date_of_birth', $date_of_birth);
$stmt->bindValue(':password', $password);
$stmt->bindValue(':confirm_password', $confirm_password);
if ($_POST["password"] != $_POST["confirm_password"]) {
echo 'passwords do not match';
}
$result1 = $stmt->execute();
//echo $result ;exit;
if ($result1) {
echo '<div><p class="text-success">Created Successfully!</p></>';
}
clear_session();
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Register</title>
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css"/>
<link rel="stylesheet" href="css/bootstrap.min.custom.css" type="text/css"/>
<script src="js/bootstrap.min.js"></script>
<script language="javascript" src="js/calendar.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="Absolute-Center is-Responsive">
<div class="col-md-10 col-md-offset-2 well">
<form class="form-horizontal" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
role="form">
<legend>Registration</legend>
<div class="form-group">
<label for="name" class="col-xs-2 control-label">CCODE:</label>
<div class="col-xs-10">
<input class="form-control" type="text" name="idcode" required
placeholder="CCODE/EMPLOYEE CODE/ ID CODE"
value="<?php echo #$_POST['idcode']; ?>" readonly/>
<label for="idcode" generated="true" class="error">
<?= isset($error_hash["idcode"]) ? $error_hash["idcode"] : "" ?>
</label>
</div>
</div>
<div class="form-group">
<label for="birthday" class="col-xs-2 control-label">Name:</label>
<div class="col-xs-10">
<div class="form-inline">
<div class="form-group">
<input class="form-control" type="text" name="first_name" required
placeholder="First Name"
value="<?php echo #$_POST['first_name']; ?>"/>
<label for="first_name" generated="true" class="error">
<?= isset($error_hash["first_name"]) ? $error_hash["first_name"] : "" ?>
</label>
</div>
<div class="form-group">
<input class="form-control" type="text" name="middle_name" required
placeholder="Middle Name"
value="<?php echo #$_POST['middle_name']; ?>"/>
<label for="middle_name" generated="true" class="error">
<?= isset($error_hash["middle_name"]) ? $error_hash["middle_name"] : "" ?>
</label>
</div>
<div class="form-group">
<input class="form-control" type="text" name="last_name" required
placeholder="Last Name" value="<?php echo #$_POST['last_name']; ?>"/>
<label for="middle_name" generated="true" class="error">
<?= isset($error_hash["last_name"]) ? $error_hash["last_name"] : "" ?>
</label>
</div>
</div>
</div>
</div>
<div class="form-group">
<label for="name" class="col-xs-2 control-label">Designation:</label>
<div class="col-xs-10">
<input class="form-control" type="text" name="course" required
placeholder="Designation"
value="<?= isset($_SESSION["course"]) ? $_SESSION["course"] : ""; ?>"/>
<label for="course" generated="true" class="error">
<?= isset($error_hash["course"]) ? $error_hash["course"] : "" ?>
</label>
</div>
</div>
<div class="form-group">
<label for="name" class="col-xs-2 control-label">Date-Of-Birth:</label>
<div class="col-xs-8">
<select name="dd">
<option value="">Date</option>
<?php
for ($i = 1; $i <= 31; $i++) {
echo "<option value='$i'>" . $i . "</option>";
}
?>
</select>
<select name="mm">
<option value="">Month</option>
<?php
for ($i = 1; $i <= 12; $i++) {
echo "<option value='$i'>" . $i . "</option>";
}
?>
</select>
<select name="yy">
<option value="">Year</option>
<?php
for ($i = 1960; $i <= 2020; $i++) {
echo "<option value='$i'>" . $i . "</option>";
}
?>
</select>
</div>
</div>
<div class="form-group">
<label for="name" class="col-xs-2 control-label">Email:</label>
<div class="col-xs-10">
<input class="form-control" type="email" name="email" required placeholder="Email"
value="<?php echo #$_POST['email']; ?>" readonly/>
<label for="email" generated="true" class="error">
<?= isset($error_hash["email"]) ? $error_hash["email"] : "" ?>
</label>
</div>
</div>
<div class="form-group">
<label for="name" class="col-xs-2 control-label">Password:</label>
<div class="col-xs-10">
<input class="form-control" type="password" name="password" required
placeholder="Password"
value="<?= isset($_SESSION["password"]) ? $_SESSION["password"] : ""; ?>"/>
<label for="name" generated="true" class="error">
<?= isset($error_hash["password"]) ? $error_hash["password"] : "" ?>
</label>
</div>
</div>
<div class="form-group">
<label for="name" class="col-xs-2 control-label">Confirm Password:</label>
<div class="col-xs-10">
<input class="form-control" type="password" name="confirm_password" required
placeholder="Confirm Password"
value="<?= isset($_SESSION["confirm_password"]) ? $_SESSION["confirm_password"] : ""; ?>"/>
<label for="name" generated="true" class="error">
<?= isset($error_hash["confirm_password"]) ? $error_hash["confirm_password"] : "" ?>
</label>
</div>
</div>
</form>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="form-actions">
<input type="submit" name="Submit" value="Submit" class="btn btn-primary"/>
</div>
<br>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</html>

Submit button won't work in PHP

PHP Code
<?php
if (!isset($_SESSION)) { session_start(); }
include "connect.php";
include "functions.php";
if (!isset($_SESSION['login']) || $_SESSION['login'] !== true) {
header('location: no_acces.php');
exit();
} else {
$id_user = $_SESSION['userid'];
$q_user = mysqli_query($conn, "SELECT * FROM users WHERE id = $id_user");
if (mysqli_num_rows($q_user) === 1) {
$r_user = mysqli_fetch_assoc($q_user);
} else {
unset($_SESSION['login']);
unset($_SESSION['userid']);
header('location: no_acces.php');
exit();
}
}
$error = "";
$userQuery = mysqli_query($conn, "SELECT username FROM users");
$user = mysqli_fetch_assoc($userQuery);
$id = $_GET['id'];
if (isset($_POST['edit_contact'])) {
$roepnaam = $_POST['roepnaam'];
$naam = $_POST['naam'];
$land = $_POST['land'];
$bedrijf = $_POST['bedrijf'];
$adres1 = $_POST['adres1'];
$adres2 = $_POST['adres2'];
$stad = $_POST['stad'];
$postcode = $_POST['postcode'];
$provincie = $_POST['provincie'];
$telefoon = $_POST['telefoon'];
$email = $_POST['email'];
$captcha= $_POST['g-recaptcha-response'];
if(!$captcha){
$error = "Er is een fout opgetreden";
}
if ($error == "") {
$insertUser = ("UPDATE address SET
roepnaam = '$roepnaam', naam = '$naam', bedrijf = '$bedrijf', telefoon = '$telefoon', email = '$email', adres1 = '$adres1', adres2 = '$adres2', stad = '$stad', postcode = '$postcode', provincie = '$provincie', land = '$land' WHERE id = $id");
if (mysqli_query($conn, $insertUser)) {
$_SESSION['edit_contact'] = true;
header('location: address_book.php');
} else {
$error = "Er is een fout opgetreden";
}
}
}
?>
HTML Code
<!DOCTYPE html>
<html lang="en">
<body>
<form action="" method="post">
<?php if ($error !== "") { ?>
<div class="row">
<div class="col-md-12 error">
<?php echo $error; ?>
</div>
</div>
<?php } ?>
<label for="firstName" class="control-label">Naam:</label>
<div class="row ">
<div class="col-md-6">
<input type="text" class="form-control" id="firstName" placeholder="Roepnaam" name="roepnaam" value="<?php if (isset($_POST['roepnaam'])) { echo $_POST['roepnaam']; } ?>" required/>
</div>
<div class="col-md-6">
<input type="text" class="form-control" id="lastName" placeholder="Naam" name="naam" value="<?php if (isset($_POST['naam'])) { echo $_POST['naam']; } ?>" required/>
</div>
</div>
<label for="username" class="control-label">Bedrijf:</label>
<div class="row ">
<div class="col-md-12">
<input type="text" class="form-control" id="username" placeholder="Bedrijf" name="bedrijf" value="<?php if (isset($_POST['bedrijf'])) { echo $_POST['bedrijf']; } ?>" required/>
</div>
</div>
<label for="password" class="control-label">Telefoonnummer:</label>
<div class="row ">
<div class="col-md-12">
<input type="text" class="form-control" id="password" placeholder="Telefoonnummer" name="telefoon" value="<?php if (isset($_POST['telefoon'])) { echo $_POST['telefoon']; } ?>" required/>
</div>
</div>
<label for="email" class="control-label">Email:</label>
<div class="row ">
<div class="col-md-12">
<input type="text" class="form-control" id="email" placeholder="E-mailadres" name="email" value="<?php if (isset($_POST['email'])) { echo $_POST['email']; } ?>" required/>
</div>
</div>
<label for="adres1" class="control-label">Adres:</label>
<div class="row">
<div class="col-md-12">
<input type="text" class="form-control" id="adres1" placeholder="Adres 1" name="adres1" value="<?php if (isset($_POST['adres1'])) { echo $_POST['adres1']; } ?>" required/>
</div>
</div>
<div class="row padding-top-10">
<div class="col-md-12">
<input type="text" class="form-control" id="adres2" placeholder="Adres 2" name="adres2" value="<?php if (isset($_POST['adres2'])) { echo $_POST['adres2']; } ?>"/>
</div>
</div>
<div class="row">
<div class="col-md-3">
<label for="postcode" class="control-label">Postcode:</label>
</div>
<div class="col-md-5">
<label for="city" class="control-label">Stad:</label>
</div>
<div class="col-md-4">
<label for="regio" class="control-label">Regio:</label>
</div>
</div>
<div class="row ">
<div class="col-md-3">
<input type="text" class="form-control" id="postcode" placeholder="Postcode" name="postcode" value="<?php if (isset($_POST['postcode'])) { echo $_POST['postcode']; } ?>" required/>
</div>
<div class="col-md-5">
<input type="text" class="form-control" id="city" placeholder="Stad" name="stad" value="<?php if (isset($_POST['stad'])) { echo $_POST['stad']; } ?>" required/>
</div>
<div class="col-md-4">
<input type="text" class="form-control" id="regio" placeholder="Provincie" name="provincie" value="<?php if (isset($_POST['provincie'])) { echo $_POST['provincie']; } ?>" required/>
</div>
</div>
<label for="land" class="control-label">Land:</label>
<div class="row ">
<div class="col-md-12">
<input type="text" class="form-control" id="password" placeholder="Land" name="land" value="<?php if (isset($_POST['land'])) { echo $_POST['land']; } ?>" required/>
</div>
</div>
<div class="row">
<div class="col-md-8 padding-top-10 ">
<div class="g-recaptcha " data-sitekey="6LcCsBoTAAAAAK72uzyJSrgWwD8xuF6jFIfgFaHX"></div>
</div>
</div>
<div class="row">
<div class="col-md-2 padding-top-10">
<input type="submit" name="edit_contact" class="btn btn-succes" value="Wijzigen">
</div>
<div class="col-md-2 padding-top-10">
<input type="text" name="delete_contact" action="delete_contact.php" class="btn btn-succes" value="Contact verwijderen">
</div>
</div>
</form>
</body>
</html>
PHP Code
<?php
if (!isset($_SESSION)) { session_start(); }
include "connect.php";
include "functions.php";
if (!isset($_SESSION['login']) || $_SESSION['login'] !== true || !isset($_SESSION['userid']) || $_SESSION['userid'] == "") {
header('location: login.php');
exit();
} else {
session_regenerate_id();
}
$id = $_GET['id'];
$query = "DELETE FROM address WHERE id= $id";
mysqli_query ($query);
if (mysql_affected_rows() == 1) {
header('location: addressbook.php');
} else {
echo "Verwijderen mislukt";
}
?>
I'm trying to make a delete button for my contacts within the addressbook. but everytime I click "Contact verwijderen" the webpage resets it self and the contact won't be deleted. Could anyone help me to fix this?
You input is a text input and you don't have a form asociated with it,create one and change the type of submit to submit
<form action="delete_contact.php" method="post">
//other inputs
<input type="submit" name="delete_contact" class="btn btn-succes" value="Contact verwijderen">
</form>
You are mixing MySQL and MySQLi functions:
mysqli_query ($query);
if (mysql_affected_rows() == 1)
You cannot mix MySQL with MySQLi, your code should be:
mysqli_query ($query);
if (mysqli_affected_rows($conn) == 1)
Add a normal link to delete the contact, you don't need a form.
<a href="delete_contact.php?id=<?php echo $id ?>">
Contact verwijderen
</a>

Update statement not working properly and changes the other records

I'm wondering why my update statement changes the venue_type of all the other records whenever I saved the edited record.
Been working on this update statement for quite sometime now and I just can't get it to work. It would be great if you guys could help me on this.
Thanks for the help! :)
<body>
<head>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/smoothscroll.js"></script>
<script src="js/resetOnClick.js"></script>
<link href="css/bootstrap.min.css" rel="stylesheet"/>
<link href="css/styles.css" rel="stylesheet"/>
<link href="css/notifbox.css" rel="stylesheet"/>
</head>
<center><label class="control-label" style="font-size:30px">Reservation</label></center>
<center><label class="control-label" style="font-size:15px">Edit Record</label></center>
<br/>
<br/>
<?php
$id = isset($_GET['id'])? $_GET['id'] : "";
include('config/config1.php');
$sel = "SELECT idReservation, venue.venue_type, reservation.reservation_date, reservation.reservation_time
FROM venue
INNER JOIN reservation ON reservation.Venue_idVenue = venue.idVenue where idReservation = '$id';";
$rsvtn = isset($_POST['idReservation']);
$query = mysqli_query($conn,$sel);
while($detail = mysqli_fetch_array($query))
{
$rid = $detail['idReservation'];
$ven = $detail['venue_type'];
$res_d = $detail['reservation_date'];
$res_t = $detail['reservation_time'];
?>
<form class="form-horizontal form-label-left" method="post">
<div class="form-group">
<!--BACK Button-->
Back
<!--BACK Button-->
</div>
<!--<input type="hidden" name="submitted" value="true"/>-->
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Venue<span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="venue_type" data-content="<?php echo $ven;?>" class="form-control col-md-7 col-xs-12" name="venue_type" placeholder="Venue" required="required" type="text" value="<?php echo $ven;?>">
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Reservation Date<span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="reservation_date" data-content="<?php echo $res_d;?>" class="form-control col-md-7 col-xs-12" name="reservation_date" placeholder="Reservation Date" required="required" type="date" value="<?php echo $res_d;?>">
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Reservation Time<span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="reservation_time" data-content="<?php echo $res_t;?>" class="form-control col-md-7 col-xs-12" name="reservation_time" placeholder="Reservation Time" required="required" type="time" value="<?php echo $res_t;?>">
</div>
</div>
<div class="ln_solid"></div>
<div class="form-group">
<div class="col-md-6 col-md-offset-3">
<input type="hidden" name="updRsvtn" value="<?php echo $detail['idReservation'];?>">
<input type="submit" class="btn btn-primary submits" value="Save Edited" onClick="Reservation Records.php" >
</div>
</div>
<?php
}
?>
<?php
if(isset($_POST['updRsvtn']))
{
include('config/config1.php');
$ven = $_POST['venue_type'];
$res_d = $_POST['reservation_date'];
$res_t = $_POST['reservation_time'];
if(!mysqli_query($conn,"UPDATE reservation, venue SET venue_type = '$ven', reservation_date = '$res_d', reservation_time = '$res_t' WHERE idReservation = '$id'"))
{
echo "Not Queried: Wrong variables !";
}
else
{
header("Location: Reservation Records.php");
}
mysqli_close($conn);
}
?>
</form>
<table class = "notif pos">
<thead>
<tr>
<th><center/><b>*Old Record</b></th>
</tr>
</thead>
<tbody>
<tr>
<td><center/><b>Venue: <?php echo $ven;?></td>
</tr>
<tr>
<td><center/><b>Reservation Date: <?php echo $res_d;?></td>
</tr>
<tr>
<td><center/><b>Reservation Time: <?php echo $res_t;?></td>
</tr>
</tbody>
</table>
</body>
if its not working that means your their is not value in
$_GET['id']. then $id is having blank value that's why your query
update for all rows.
if(isset($_POST['updRsvtn']))
{
$id = isset($_GET['id'])? $_GET['id'] : "";
$ven = $_POST['venue_type'];
$res_d = $_POST['reservation_date'];
$res_t = $_POST['reservation_time'];
if(!mysqli_query($conn,"UPDATE reservation, venue SET
venue_type = '$ven', reservation_date = '$res_d',
reservation_time = '$res_t' WHERE idReservation = '$id'"))
{
echo "Not Queried: Wrong variables !";
}
else
{
header("Location: Reservation Records.php");
}
mysqli_close($conn);
}
You could try JOIN in your query
UPDATE reservation t1
JOIN venue t2 ON (t1.Venue_idVenue = t2.idVenue)
SET t1.reservation_date = '$res_d',
t1.reservation_time = '$rest_t',
t2.venue_type = '$ven'
WHERE t1.idReservation = '$id'"

Checking if file is larger than 1 MB

I would like to ask your help. I wrote a code which checks if all the fields are filled in and if the file is not larger than 1 MB. If everything is correct the file successfuly uploads to MySQL database. But if the file size is larger than 1 MB the code stops working. The error doesnt show up and all the fields become empty. Here`s the code (some of it is in Lithuanian, sorry):
<?php
error_reporting(E_ERROR);
session_start();
if (isset($_SESSION['login'])){
include 'config.php';
$username = $_SESSION['login'];
$result = mysqli_query($db,"SELECT * FROM users WHERE username='$username'");
$rws = mysqli_fetch_array($result);
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$autorius = mysqli_real_escape_string($db,$_POST['autorius']);$pavadinimas = mysqli_real_escape_string($db,$_POST['pavadinimas']);$puslapiai = mysqli_real_escape_string($db,$_POST['puslapiai']);$tema = mysqli_real_escape_string($db,$_POST['tema']);$pmintis = mysqli_real_escape_string($db,$_POST['pmintis']);$pveikejai = mysqli_real_escape_string($db,$_POST['pveikejai']);$aveikejai = mysqli_real_escape_string($db,$_POST['aveikejai']);$epizodas = mysqli_real_escape_string($db,$_POST['epizodas']);$nuomone = mysqli_real_escape_string($db,$_POST['nuomone']);$apie = mysqli_real_escape_string($db,$_POST['apie']);$foto = mysqli_real_escape_string($db,$_POST['foto']);$user_id = $rws['id'];
if (!empty($autorius) && !empty($pavadinimas) && !empty($puslapiai) && !empty($tema) && !empty($pmintis) && !empty($pveikejai) && !empty($aveikejai) && !empty($epizodas) && !empty($nuomone) && !empty($apie) && isset($foto) && $_FILES['foto']['size'] > 0 && $_FILES['foto']['size'] < 1000001) {
$fileName = $_FILES['foto']['name'];$tmpName = $_FILES['foto']['tmp_name'];$fileSize = $_FILES['foto']['size'];$fileType = $_FILES['foto']['type'];
$fp = fopen($tmpName, 'r');
$foto = fread($fp, filesize($tmpName));
$foto = addslashes($foto);
fclose($fp);
$query = "INSERT INTO books (id_user, autorius, pavadinimas, puslapiai, tema, pmintis, pveikejai, aveikejai, epizodas, nuomone, apie, foto, name, type, size) ".
"VALUES ('$user_id', '$autorius', '$pavadinimas', '$puslapiai', '$tema', '$pmintis', '$pveikejai', '$aveikejai', '$epizodas', '$nuomone', '$apie', '$foto', '$fileName', '$fileType', '$fileSize')";
$result = mysqli_query($db,$query);
$success = "Knygos aprašymas įkeltas";
echo $_FILES['foto']['size'];
}
else if (empty($autorius) || empty($pavadinimas) || empty($puslapiai) || empty($tema) || empty($pmintis) || empty($pveikejai) || empty($aveikejai) || empty($epizodas) || empty($nuomone) || empty($apie)) {
$error = "Užpildykite visus laukelius!";
}
else if (empty($foto)){
$error = "Pasirinkite viršelio nuotrauką!";
}
else if ($_FILES['foto']['size'] > 1000001){
$error = "Viršelio nuotraukos dydid neturi viršyti 1 MB!";
}
}
?>
<?php include 'bin/includes/header.html'; ?>
<div class="container">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Įkelti knygos aprašymą
<small>Ikelkite savo knygos aprašymą</small>
</h1>
<ol class="breadcrumb">
<li>Pagrindinis
</li>
<li class="active">Įkelti knygos aprašymą</li>
</ol>
</div>
</div>
<div class="row">
<div class="col-md-8">
<form name="upload" id="upload" method="post" enctype="multipart/form-data">
<div class="control-group form-group">
<?php
if(isset($success)) {
echo '<label style="color: #44FF00;">';
echo $success;
echo '</label>';
}
else {
echo '<label style="color: #FF3700;">';
echo $error;
echo '</label>';
} ?>
<div class="controls">
<input type="text" class="form-control" name="autorius" maxlength="60" placeholder="Autorius" value="<?php if(!empty($_POST['autorius'])) echo $_POST['autorius'];?>">
<p class="help-block"></p>
</div>
<div class="controls">
<input type="text" class="form-control" name="pavadinimas" maxlength="255" placeholder="Pavadinimas" value="<?php if(!empty($_POST['pavadinimas'])) echo $_POST['pavadinimas'];?>">
<p class="help-block"></p>
</div>
<div class="controls">
<input type="text" class="form-control" name="puslapiai" placeholder="Puslapių skaičius" value="<?php if(!empty($_POST['puslapiai'])) echo $_POST['puslapiai'];?>">
<p class="help-block"></p>
</div>
<div class="controls">
<input type="text" class="form-control" name="pveikejai" maxlength="999" placeholder="Pagrindiniai veikėjai" value="<?php if(!empty($_POST['pveikejai'])) echo $_POST['pveikejai'];?>">
<p class="help-block"></p>
</div>
<div class="controls">
<input type="text" class="form-control" name="aveikejai" maxlength="999" placeholder="Antraeiliai veikėjai" value="<?php if(!empty($_POST['aveikejai'])) echo $_POST['aveikejai'];?>">
<p class="help-block"></p>
</div>
<div class="controls">
<textarea placeholder="Tema" rows="2" class="form-control" name="tema" maxlength="999" style="resize:none"><?php if(!empty($_POST['tema'])) echo $_POST['tema'];?></textarea>
<p class="help-block"></p>
</div>
<div class="controls">
<textarea placeholder="Pagrindinė mintis" rows="2" class="form-control" name="pmintis" maxlength="999" style="resize:none"><?php if(!empty($_POST['pmintis'])) echo $_POST['pmintis'];?></textarea>
<p class="help-block"></p>
</div>
<div class="controls">
<textarea placeholder="Siužetas" rows="5" class="form-control" name="apie" maxlength="2999" style="resize:none"><?php if(!empty($_POST['apie'])) echo $_POST['apie'];?></textarea>
<p class="help-block"></p>
</div>
<div class="controls">
<textarea placeholder="Įsimintiniausias epizodas" rows="3" class="form-control" name="epizodas" maxlength="2999" style="resize:none"><?php if(!empty($_POST['epizodas'])) echo $_POST['epizodas'];?></textarea>
<p class="help-block"></p>
</div>
<div class="controls">
<textarea placeholder="Nuomonė apie knygą" rows="3" class="form-control" name="nuomone" maxlength="999" style="resize:none"><?php if(!empty($_POST['nuomone'])) echo $_POST['nuomone'];?></textarea>
<p class="help-block"></p>
</div>
<div class="controls">
<label>Įkelti viršelį (max 1MB)</label>
<input name="foto" type="file" id="foto">
<p class="help-block"></p>
</div>
<button type="submit" class="btn btn-primary">Įkelti</button>
</div>
</form>
</div>
</div>
<?php include 'bin/includes/footer.html'; ?>
<?php } else {
header("location: index.php");
}
Thanks for your help!
You should use this to get the file size:
$size = filesize($_FILES['foto']['tmp_name']);
This returns the filesize in byte. One MB are 1048576 bytes. You should check for the function not returning false.
In general: You should use exceptions for this purpose, it is much simplier to check for a condition and then check again. If you just want to use if-else-statements, you should rearrange it (check for errors first and then upload the thing in the else-case).
Why are you not saving the files in filesystem? This is a lot easier...

Categories