Duplicating instead of updating - php

Hello guys
I have a db with guides that in admin mode can be edited. I have just remade the input area and all is good except when logged in as admin i cant update guides, it simply creates a new guide instead of simply updating.
Please be gentle with me as i am a beginner in the coding world, + i would love some fresh eyes on this :) thank you very much
my dashboard code
<?php include("header.php"); ?>
<?php
if(!isset($_SESSION['isLogin']) && $_SESSION['isLogin'] != "YES"){
die("<script> window.location = 'login.php' </script>");
}
$error=false;
$success=false;
if(isset($_GET) && !empty($_GET)) {
$id = base64_decode($_GET['id']);
$user_id = $_SESSION['userInfo']['id'];
$selectSql = "SELECT * FROM guides WHERE 1 = 1 AND user_id = " . $user_id . " AND id = " . $id;
$result = $conn->query($selectSql);
$id = 0;
$title = $step2 = $step3 = $step4 = $step5 = $step6 = $step7 = $step8 = $step9 = '';
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$id = $row['id'];
$title = $row['title'];
$step2 = $row['step2'];
$step3 = $row['step3'];
$step4 = $row['step4'];
$step5 = $row['step5'];
$step6 = $row['step6'];
$step7 = $row['step7'];
$step8 = $row['step8'];
$step9 = $row['step9'];
}
}
}
if(isset($_POST) && !empty($_POST)){
$user_id = $_SESSION['userInfo']['id'];
if($_POST['id']){
$sqlInsert = 'UPDATE guides SET title = "'.htmlentities($_POST["title"]).'", step2 = "'.htmlentities($_POST["step2"]).'", step3 = "'.htmlentities($_POST["step3"]).'", step4 = "'.htmlentities($_POST["step4"]).'", step5 = "'.htmlentities($_POST["step5"]).'", step6 = "'.htmlentities($_POST["step6"]).'", step7 = "'.htmlentities($_POST["step7"]).'", step8 = "'.htmlentities($_POST["step8"]).'", step9 = "'.htmlentities($_POST["step9"]).'" WHERE id = ' . $_POST['id'] . ' AND user_id = ' . $_SESSION['userInfo']['id'];
}else{
$sqlInsert = 'INSERT INTO guides(user_id, title, step2, step3, step4, step5, step6, step7, step8, step9)VALUES ("' .$user_id. '", "'.htmlentities($_POST["title"]).'", "'.htmlentities($_POST["step2"]).'", "'.htmlentities($_POST["step3"]).'", "'.htmlentities($_POST["step4"]).'", "'.htmlentities($_POST["step5"]).'", "'.htmlentities($_POST["step6"]).'", "'.htmlentities($_POST["step7"]).'", "'.htmlentities($_POST["step8"]).'", "'.htmlentities($_POST["step9"]).'")';
}
if ($conn->query($sqlInsert) === TRUE) {
if($_POST['id']){
$success = "Your guide has been updated successfully!";
}else{
$success = "Your guide has been added successfully!";
}
$_SESSION['success'] = $success;
header("Location: dashboard.php");
}else{
$error[] = "Error Message: ".$conn->error;
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Guideory - share your knowledge</title>
<link href="css/style.css" rel="stylesheet" type="text/css" media="all"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-
scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="Guideory - share your knowledge" />
<!--web-fonts-->
<link href="https://fonts.googleapis.com/css?family=Raleway" rel="stylesheet">
<link rel="stylesheet" href="css/style.css">
<!--web-fonts-->
</head>
<body>
<div class="header">
</div>
<!---header--->
<!---main--->
<div class="main">
<div class="main-section">
<div class="login-form">
<h2>Share a piece of your knowledge</h2>
<br>
<h4>You can create up to 8 steps, not including the title.
Atleast one step is required. When writing your guide, remember that other
people have to be able to read it, so be as specific as possible.</h4>
<form role="form" method="post">
<div id="step-1">
<ul>
<li class="text-info" id="title">Title:</li>
<li><input type="text" value="<?php echo $title;
?>" name="title" id="title" placeholder="Enter the title for your guide here"
required></li>
<div class="clear"></div>
</ul>
<ul>
<li class="text-info" id="step2">Step 1:</li>
<li><textarea name="step2" id="step2"
placeholder="Enter the description for step 1 here" required><?php echo
$step2; ?></textarea></li>
<div class="clear"></div>
</ul>
<br>
<ul>
<li class="text-info">Step 2:</li>
<li><textarea name="step3" placeholder="Enter the
description for step 2 here"><?php echo $step3; ?></textarea></li>
<div class="clear"></div>
</ul>
<ul>
<li class="text-info">Step 3:</li>
<li><textarea name="step4" placeholder="Enter the
description for step 3 here"><?php echo $step4; ?></textarea></li>
<div class="clear"></div>
</ul>
<ul>
<li class="text-info">Step 4:</li>
<li><textarea name="step5" placeholder="Enter the
description for step 4 here"><?php echo $step5; ?></textarea></li>
<div class="clear"></div>
</ul>
<ul>
<li class="text-info">Step 5:</li>
<li><textarea name="step6" placeholder="Enter the
description for step 5 here"><?php echo $step6; ?></textarea></li>
<div class="clear"></div>
</ul>
<ul>
<li class="text-info">Step 6:</li>
<li><textarea name="step7" placeholder="Enter the
description for step 6 here"><?php echo $step7; ?></textarea></li>
<div class="clear"></div>
</ul>
<ul>
<li class="text-info">Step 7:</li>
<li><textarea name="step8" placeholder="Enter the
description for step 7 here"><?php echo $step8; ?></textarea></li>
<div class="clear"></div>
</ul>
<ul>
<li class="text-info">Step 8:</li>
<li><textarea name="step9" placeholder="Enter the
description for step 8 here"><?php echo $step9; ?></textarea></li>
<div class="clear"></div>
</ul>
<input type="submit" value="Create guide">
</form>
</div>
</div>
</div>
</body>
</html>

There is no input with name id so $_POST['id'] doesn't exist and that's why there's an insert instead of update.
And some extra hints
!isset($_SESSION['isLogin']) && $_SESSION['isLogin'] != "YES"
You probably want isset($_SESSION['isLogin']) here since when the variable is not set it can never be YES
isset($_GET) && !empty($_GET)
You can drop isset here and only use empty.
while ($row = $result->fetch_assoc()) {
Only the last row is stored in those variables, since you are overwriting them.

Related

How can i automatically save the image inside a website when i click the add to cart?

how can I automatically save this pizza image, and save it to my local folder? I can easily save the other information, but I'm encountering automatically saving the image itself in a local folder?
I'm fetching the other data from different table, but the I can't save the image itself. I'm have no intention of using foreign key for no.
backend.php
if (isset($_POST['addcart'])) {
$con = connection();
$fetch = singleInfo();
$name = $fetch['name'];
$price = $fetch['price'];
$image = $fetch['image'];
$new_image = '../images/' . $image;
$stmt = $con->prepare("INSERT INTO `cart`(`name`, `price`,`image`) VALUES ('$name','$price','$new_image')");
$stmt->execute();
}
index.php
<?php
session_start();
require('../backend/clientbackend.php');
$fetch = singleInfo();
$current_price = $fetch['price'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./style/style.css">
<title>E-Commerce</title>
</head>
<body>
<nav>
<div class="left">
<h4 class="navbar-header"> Branding </h4>
<ul>
<li>Home</li>
<li>Shop</li>
<li>About</li>
</ul>
</div>
<div class="right">
<button class="loginButton">Login</button>
</div>
</nav>
<article>
<form method="post" class="product-description">
<div class="left">
<div class="left-title" name="name"> <?php echo $fetch['name']; ?> </div>
<div class="left-info">
<p class="left-description"> <?php echo $fetch['desc']; ?> </p>
<span class="price" name="price"> $ <strong> <?php echo $fetch['price']; ?></strong> </span>
</div>
<div class="left-increment">
<div class="addition">+</div>
<input type="number" class="current_value" value="1" min="1">
<div class="subtraction">-</div>
<button class="gotoCart" name="addcart" type="submit"> Add To Cart </button>
Go to cart
</div>
</div>
<div class="right">
<div class="right-image">
<img name="image" src="<?php echo '../uploads/' . $fetch['image']; ?>" alt="">
</div>
</div>
</form>
</article>
<footer>
<div class="footer-container">
<div class="box1">
<h3>Ecommerce Branding</h3>
<span>School Activity</span>
</div>
<div class="box2">
<h3>Colegio De San Lorenzo
</h3>
<span>Congressional Ave, Project 8, Quezon City, Metro Manila</span>
</div>
<div class="box3">
<h3>Emman Cruz</h3>
<span> zurcemozz#gmail.com</span>
</div>
</div>
</footer>
<script>
const addBtn = document.querySelector('.addition');
const subBtn = document.querySelector('.subtraction');
let currentValue = document.querySelector('.current_value');
let stock = 1;
addBtn.addEventListener("click", function() {
stock = stock + 1
currentValue.value = stock;
console.log(currentValue.value);
})
subBtn.addEventListener("click", function() {
if (stock <= 0) {
stock = 0;
} else {
stock = stock - 1
currentValue.value = stock;
console.log(currentValue.value);
}
})
</script>
</body>
</html>
you can copy the image file from '../uploads/' to '../images/' and then you save it .
you can do this with copy function
copy documentations
copy() example :
<?php
$image = '../uploads/'.$fetch['image'];
$new_image= '../images/'.$fetch['image'];
if (!copy($image , $new_image)) {
echo "failed to copy $image ...\n";
}
?>

How to keep session variables between pages with forms?

I'm writing a program that has 3 pages.
On page 1 there is an option for the user to select a quantity of a breakfast product he wants to purchase. After selecting a quantity the user hits the submit button, if the user is not registered, it will take him to Page 2 for him to register. If the user is registered, it will direct them to Page 3.
However, if the user goes to Page 2 first and does not have a quantity selected from Page 1 it will redirect him to Page 1 after he registers and press submit, and then once they select a quantity and hit submit on Page 1 it will go to Page 3.
I'm struggling to maintain my session variables between the pages because two of them have forms that get overwritten if the user ever goes back to that page.
Page 1:
<?php
session_start();
$_SESSION['name']= $_POST['name'];
$_SESSION['email']= $_POST['email'];
$platter_quantity = $_SESSION['platter_quantity'];
$yogurt_quantity = $_SESSION['yogurt_quantity'];
$waffles_quantity = $_SESSION['waffles_quantity'];
?>
<!DOCTYPE html>
<head>
<title>Product Page</title>
<link rel="stylesheet" type"text/css" href="settings.css">
</head>
<html>
<body>
<ul>
<li><a class="active" href="product.php">Product</a></li>
<li>Registration</li>
<li>Invoice</li>
<li style="float:right">Login</li>
</ul>
<?php
$action = '';
if (!empty($_SESSION['name']) or !empty($_SESSION['email'])) {
$action = "invoice.php";
}
else {
$action = "registration.php";
}
?>
<form action="<?php echo $action; ?>" method="post">
<div class="container">
<img src="images/platter.jpg" alt="Breakfast Platter" style="float: left; width: 400px; height: 300px;";>
<h1>Breakfast Platter</p>
<p>The breakfast platter option comes with two fried eggs, four pancakes, and a bunch of bacon.</p>
Quantity: <input type="number" name="platter_quantity" min="0">
<p value="10.99" name="platter_price">Price: $10.99</p>
</div>
<div class="container">
<img src="images/yogurt.jpg" alt="Yogurt Parfait" style="float: left; width: 400px; height: 300px;">
<h1>Yogurt Parfait</p>
<p>The yogurt parfait option comes with two cups of yogurt, oats, and a mixture of berries.</p>
Quantity: <input type="number" name="yogurt_quantity" min="0">
<p value="6.99" name="yogurt_price">Price: $6.99</p>
</div>
<div class="container">
<img src="images/waffles.jpg" alt="Waffles" style="float: left; width: 400px; height: 300px;";>
<h1>Waffles</p>
<p>The waffles option comes with two buttermilk waffles with butter and syrup.</p>
Quantity: <input type="number" name="waffles_quantity" min="0">
<p value="$4.99" name="waffles_price">Price: $4.99</p>
</div>
<br>
<button class="button" type="submit" name="submit">Submit</button>
</form>
</body>
</html>
Page 2:
<?php
session_start();
$name = $_SESSION['name'];
$email = $_SESSION['email'];
$_SESSION['platter_quantity'] = $_POST['platter_quantity'];
$_SESSION['yogurt_quantity'] = $_POST['yogurt_quantity'];
$_SESSION['waffles_quantity'] = $_POST['waffles_quantity'];
?>
<!DOCTYPE html>
<head>
<title>Registration Page</title>
<link rel="stylesheet" type"text/css" href="settings.css">
</head>
<html>
<body>
<ul>
<li>Product</li>
<li><a class="active" href="registration.php">Registration</a></li>
<li>Invoice</li>
<li style="float:right">Login</li>
</ul>
<br>
<?php
$action = '';
if (!empty($_SESSION['platter_quantity']) or !empty($_SESSION['yogurt_quantity']) or !empty($_SESSION['waffles_quantity'])) {
$action = "invoice.php";
}
else {
$action = "product.php";
}
?>
<form action="<?php echo $action; ?>" method="post">
Name: <input type="text" name="name" pattern="[A-Za-z]" required><br><br>
E-mail: <input type="text" name="email" pattern="/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+#[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/" required><br><br>
<input type="submit">
</form>
<br>
<?php
print_r($_SESSION);
echo "<br>Platter: " . $_SESSION["platter_quantity"] . "<br>";
echo "Yogurt: " . $_SESSION["yogurt_quantity"] . "<br>";
echo "Waffles: " . $_SESSION["waffles_quantity"];
?>
</body>
</html>
Page 3:
<?php
session_start();
$name = $_SESSION['name'];
$email = $_SESSION['email'];
$platter_quantity = $_SESSION['platter_quantity'];
$yogurt_quantity = $_SESSION['yogurt_quantity'];
$waffles_quantity = $_SESSION['waffles_quantity'];
?>
<!DOCTYPE html>
<head>
<title>Invoice Page</title>
<link rel="stylesheet" type"text/css" href="settings.css">
</head>
<html>
<body>
<ul>
<li>Product</li>
<li>Registration</li>
<li><a class="active" href="invoice.php">Invoice</a></li>
<li style="float:right">Login</li>
</ul>
<h1>Hi! Welcome <?php echo $_SESSION['name']; ?>! </h1>
<?php
print_r($_SESSION);
echo "<br>Platter: " . $platter_quantity . "<br>";
echo "Yogurt: " . $yogurt_quantity . "<br>";
echo "Waffles: " . $waffles_quantity;
?>
</body>
</html>
What's the best way for me to implement this using session variables without using a database?
I tried doing this as well, but it did not seem to work:
<?php
session_start();
if (empty($_SESSION['name']) or empty($_SESSION['email'])) {
$_SESSION['name'] = $POST_['name'];
$_SESSION['email'] = $POST_['email'];
}
else {
$name = $_SESSION['name'];
$name = $_SESSION['email'];
}
if (empty($_SESSION['platter_quantity']) or empty($_SESSION['yogurt_quantity']) or ($_SESSION['waffles_quantity'])) {
$_SESSION['platter_quantity'] = $POST_['platter_quantity'];
$_SESSION['yogurt_quantity'] = $POST_['yogurt_quantity'];
$_SESSION['waffles_quantity'] = $POST_['waffles_quantity'];
}
else {
$platter_quantity = $_SESSION['platter_quantity'];
$yogurt_quantity = $_SESSION['yogurt_quantity'];
$waffles_quantity = $_SESSION['waffles_quantity'];
}
?>
You never insert $_POST or $_GET without first checking if they're set isset($_POST['variable']), and you can use checks here as well - do a check for the existence of $_POST-variables, and if they exist, use them, and if not, assign the $_SESSION-variables. So on page 3, you will have something like:
$name = $_SESSION['name'] = (isset($_POST['name']) ? $_POST['name'] : ((isset($_SESSION['name']) ? $_SESSION['name'] : '')));
And so on for the other variables. What this does is checks for $_POST, and if it's set, it updates the $_SESSION-variable, and if it's not set, it just updates the $_SESSION-variable with the already existing $_SESSION-variable, and if that doesn't exist either, it sets both variables $name and $_SESSION['name'] to empty, which you then can check for later in the script (and redirect etc.)

PHP issue with displaying a mySQLi result [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm having a rather confusing issue with a result that will not display. I'm creating a mailshot application and I'm trying to populate the email with the recipients name as well as an advert. The result works fine when I store it for use with the email that it sends to but it wont display inside the email body. Its a little hard to explain but here is the code that I am using. I have removed a lot of the email body as it was pretty large, where the ... are that is where I've take a load out.
<?php require (__DIR__.'/connections/connections.php');
session_start();
if(isset($_SESSION["UserID"])){
}else{
header('Location: login.php');
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>AzTecks Staff | Search Results</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="css/coin-slider.css" />
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
</head>
<body>
<div class="main">
<div class="header">
<div class="header_resize">
<div class="menu_nav">
<ul>
<li><span>Staff Home</span></li>
<li><span>Register Client</span></li>
<li class="active"><span>Register Applicant</span></li>
<li><span>Add Vacancy</span></li>
<li><span>Logout</span></li>
</ul>
</div>
<div class="logo">
<h1><span>AzTecks</span> <small style=" height: 12px; font-size: 11px;"> We Advise, We Avertise,</small><small style=" height: 12px; font-size: 11px;"> We Guarantee Not To Compromise</small></h1>
</div>
<div class="clr"></div>
<div class="slider">
<div class="clr"></div>
</div>
<div class="clr"></div>
</div>
</div>
<div class="content">
<div class="content_resize">
<div class="mainbar" style="margin-top:0px;">
<?php
echo "<div class=\"article\"><h2>Sending emails, please wait...</h2></div><br />";
/*Variables for mail shot query*/
$Keywords = $_SESSION['aKeywords'];
$Lname = $_SESSION['aLname'];
$Fname = $_SESSION['aFname'];
$CurrentJob = $_SESSION['aCurrentJob'];
$DesiredJob = $_SESSION['aDesiredJob'];
$CurrentSalary = $_SESSION['aCurrentSalary'];
$DesiredSalary = $_SESSION['aDesiredSalary'];
$Town = $_SESSION['aTown'];
$Country = $_SESSION['aCountry'];
$QualLevel = $_SESSION['aQualLevel'];
$Languages = $_SESSION['aLanguages'];
$TPC = $_SESSION['aTPC'];
$TechnicalTerms = $_SESSION['aTechnicalTerms'];
$ApplicantDivision = $_SESSION['aApplicantDivision'];
$query = "SELECT * FROM Applicants WHERE (? IS NULL OR CV_Text LIKE ?) AND (? IS NULL OR Applicant_Last_Name LIKE ?) AND (? IS NULL OR Applicant_First_Name LIKE ?) AND (? IS NULL OR Applicant_Current_Job_Title LIKE ?) AND (? IS NULL OR Applicant_Desired_Job_Title LIKE ?) AND (? IS NULL OR Applicant_Current_Salary >= ?) AND (? IS NULL OR Applicant_Desired_Salary >= ?) AND (? IS NULL OR Applicant_Town LIKE ?) AND (? IS NULL OR Applicant_Country LIKE ?) AND (? IS NULL OR Applicant_Qualification_Level LIKE ?) AND (? IS NULL OR Applicant_Languages LIKE ?) AND (? IS NULL OR T_P_C LIKE ?) AND (? IS NULL OR Applicant_Division LIKE ?) AND (? IS NULL OR Technical_Terms LIKE ?)";
$KeywordsW = '%'.$Keywords.'%';
$LnameW = '%'.$Lname.'%';
$FnameW = '%'.$Fname.'%';
$CurrentJobW = '%'.$CurrentJob.'%';
$DesiredJobW = '%'.$DesiredJob.'%';
$TownW = '%'.$Town.'%';
$CountryW = '%'.$Country.'%';
$QualLevelW = '%'.$QualLevel.'%';
$LanguagesW = '%'.$Languages.'%';
$TPCW = '%'.$TPC.'%';
$TechnicalTermsW = '%'.$TechnicalTerms.'%';
$ApplicantDivisionW = '%'.$ApplicantDivision.'%';
$stmt = $con->prepare($query);
$stmt->bind_param("ssssssssssiiiissssssssssssss", $Keywords, $KeywordsW, $Lname, $LnameW, $Fname, $FnameW, $CurrentJob, $CurrentJobW, $DesiredJob, $DesiredJobW, $CurrentSalary, $CurrentSalary, $DesiredSalary, $DesiredSalary, $Town, $TownW, $Country, $CountryW, $QualLevel, $QualLevelW, $Languages, $LanguagesW, $TPC, $TPCW, $ApplicantDivision, $ApplicantDivisionW, $TechnicalTerms, $TechnicalTermsW);
$stmt->execute() or die("Something went wrong, could not search :-(");
$result = $stmt->get_result();
$count = mysqli_num_rows($result);
if ($count == 0) {
$output = 'Sorry, no results found!';
echo $output;
}
else {
while($row = $result->fetch_object()) {
$id = $row->Applicant_ID;
$queryResult = $con->query("SELECT Contact_Email FROM Client_Contacts WHERE Contact_ID = {$_SESSION['coID']}");
$ContactDetails = $queryResult->fetch_object();
$email = $ContactDetails->Contact_Email;
$firstname = $row->Applicant_First_Name;
$lastname = $row->Applicant_Last_Name;
$mail_body = "<!doctype html>
<html>
...
Hello ".$ContactDetails->Contact_First_Name." ".$ContactDetails->Contact_Last_Name."<br /><br /><br />
Below is a potential applicant for your consideration.<br /><br />".$row->Applicant_Advert."<br /><br />
...</html>";
$subject = $_SESSION['eSubject'];
$headers = "From:natalie#aztecksonline.net\r\nContent-type: text/html\r\n";
$to = $email;
$mail_result = mail($to,$subject,$mail_body,$headers);
}
}
if($mail_result) {
echo "<script>window.alert(\"Mail Shot Sent!\");</script>";
header('location: index.php');
} else {
echo "Something went wrong :-(";
}
?>
</div>
<div class="sidebar">
<div class="searchform">
<form id="formsearch" name="formsearch" method="post" action="#">
<span>
<input name="editbox_search" class="editbox_search" id="editbox_search" maxlength="80" value="Search Applicants" type="text" />
</span>
<input name="button_search" src="images/search.gif" class="button_search" type="image" />
</form>
<br />
<div class="clr"><div id="google_translate_element"></div>
<script type="text/javascript">
function googleTranslateElementInit() {
new google.translate.TranslateElement({pageLanguage: 'en', layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, 'google_translate_element');
}
</script>
</div>
</div>
<div class="clr"></div>
<div class="gadget">
<h2 class="star"><span>Sidebar</span> Menu</h2>
<div class="clr"></div>
<ul class="sb_menu">
<li>Staff Home</li>
<li>Register Client</li>
<li>Register Applicant</li>
<li>Add Vacancy</li>
<li>Logout</li></ul>
</div>
<div class="gadget">
<h2 class="star"><span>Recent Vacancies</span></h2>
<div class="clr"></div>
<ul class="ex_menu">
<?php
if($cat_side_result = $con->query("SELECT Vacancy_ID, Vacancy_Job_Title, Vacancy_Location FROM Vacancies LIMIT 6")) {
if($cat_side_result->num_rows) {
while($cat_side_row = $cat_side_result->fetch_object()) {
echo '<li>'.$cat_side_row->Vacancy_Job_Title.'<br /> In '.$cat_side_row->Vacancy_Location.'</li>';
mysqli_close($con);
}
}
}
?>
</ul>
</div>
</div>
<div class="clr"></div>
</div>
</div>
<div class="fbg">
<div class="fbg_resize">
<div class="col c1">
<h2>Clients Recently Joined</h2>
<img src="images/Small_Company_logo_ABP.jpg" width="75" height="75" alt="" class="gal" /> <img src="images/Jumpahead1.jpg" width="75" height="75" alt="" class="gal" /> <img src="images/Keopple_logo_small.jpg" width="75" height="75" alt="" class="gal" /> <img src="images/Phantom_small.jpg" width="75" height="75" alt="" class="gal" /> <img src="images/graves-capital_small.jpg" width="75" height="75" alt="" class="gal" /> <img src="images/global-financial-logo_small.gif" width="75" height="75" alt="" class="gal" /> </div>
<div class="col c2">
<h2><span>Services</span> Overview</h2>
<p>At AzTecks we are committed to insuring you have total confidentiality, and do not share any data or information without your say so, please read our privacy agreement for more information.</p>
<ul class="fbg_ul">
<li>More about us</li>
<li>Privacy agreement</li>
<li>Contact us</li>
</ul>
</div>
<div class="col c3">
<h2><span>Contact</span> Us</h2>
<p>If you have any querys about us or have any questions please feel free to contact us.</p>
<p class="contact_info"> <span>Address:</span>1 Shaw Street<br />
Worcester , Worcestershire , UK<br />
<span>Postcode:</span> WR1 3QQ<br />
<span>Telephone:</span> 01905 700158<br />
<span>E-mail:</span>info#aztecksonline.net</p>
</div>
<div class="clr"></div>
</div>
</div>
<div class="footer">
<div class="footer_resize">
<p class="lf">© Copyright AzTecks.</p>
<div style="clear:both;"></div>
</div>
</div>
</div>
</body>
</html>
the $email populates fine as it sends the email with no issue but where it says in the email body $ContactDetails->Contact_First_Name, it does not display at all.
Have I done something rather dumb or is there something else wrong?
The query should look like this:
SELECT Contact_Email, Contact_First_Name, Contact_Last_Name FROM Client_Contacts WHERE Contact_ID = {$_SESSION['coID']}
You forgot to add Contact_First_Name and Contact_Last_Name into it so it wasn't even fetching them.
Start by adding at the top.
error_reporting(E_ALL);
ini_set('display_errors', '1');
And then add a die(); or exit(); before you actually execute any database updates and then check what is the error in the errors shown by php.
Normally this is because of an illegal way of executing the SQL in question. Try it out.
At first, you should write a readable code.
Now it's a really bad spaghetti code with crazy indentation (read about PSRs).
After that, separate your PHP from HTML, and move interaction with database to a different layer.

Redirecting to PHP file in Flask

I am running a localhost website using Flask and python. I have some php files that I want to run when the users click a button. Problem is that Flask isn't recognizing the PHP file as PHP code and the code is showing up as text on the webpage. It's showing the text of all the echo statements, but the words in those statements correspond to variable in the code that allow the user to login and logout of the website. What do I do?
Python Code:
#app.route('/example.php')
def phpexample():
return render_template('example.php')
This shows a html page with text resulting from the echo statements.
The PHP code (example.php):
<?php
require ('steamauth/steamauth.php');
?>
<html>
<head>
<title>Eliminate Phishers! Join Steap now</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<!--[if lte IE 8]><script src="css/ie/html5shiv.js"></script><![endif]-->
<script src="js/jquery.min.js"></script>
<script src="js/skel.min.js"></script>
<script src="js/init.js"></script>
<noscript>
<link rel="stylesheet" href="css/skel.css" />
<link rel="stylesheet" href="css/style.css" />
<link rel="stylesheet" href="css/style-wide.css" />
</noscript>
<!--[if lte IE 8]><link rel="stylesheet" href="css/ie/v8.css" /><![endif]-->
</head>
<body>
<!-- Header -->
<div id="header">
<span class="logo icon fa-paper-plane-o"></span>
<h1>Welcome. This is Steap</h1>
<p>A website designed to help eliminate phishers
<br />
and hackers on Steam.</p>
</div>
<!-- Main -->
<div id="main">
<header class="major container small">
<h3>
<?php
if(!isset($_SESSION['steamid'])) {
echo "welcome guest! <br />\n please login ";
steamlogin(); //login button
} else {
include ('steamauth/userInfo.php');
$url = $steamprofile['profileurl'];
if ($steamprofile['personastate'] == 0) {
$state = '<span style="color:#616161";>(Offline)</span>';
$picture = '<span style="color:#616161";><img src="'.$steamprofile['avatarfull'].'" title = "" alt = ""></span>';
} else if ($steamprofile['personastate'] == 1) {
$state = '<span style="color:#006EFF";>(Online)</span>';
$picture = '<span style="border: 10px dotted #006EFF;"><img src="'.$steamprofile['avatarfull'].'" title = "" alt = ""></span>';
} else if ($steamprofile['personastate'] == 2) {
$state = '<span style="color:#006EFF";>(Busy)</span>';
$picture = '<span style="border-color:#006EFF border-style: solid";><img src="'.$steamprofile['avatarfull'].'" title = "" alt = ""></span>';
} else if ($steamprofile['personastate'] == 3) {
$state = '<span style="color:#006EFF";>(Away)</span>';
$picture = '<span style="border-color:#006EFF border-style: solid";><img src="'.$steamprofile['avatarfull'].'" title = "" alt = ""></span>';
} else if ($steamprofile['personastate'] == 4) {
$state = '<span style="color:#006EFF";>(Snooze)</span>';
$picture = '<span style="border-color:#006EFF border-style: solid";><img src="'.$steamprofile['avatarfull'].'" title = "" alt = ""></span>';
} else if ($steamprofile['personastate'] == 5) {
$state = '<span style="color:#006EFF";>(Looking to Trade)</span>';
$picture = '<span style="border-color:#006EFF border-style: solid";><img src="'.$steamprofile['avatarfull'].'" title = "" alt = ""></span>';
} else if ($steamprofile['personastate'] == 6) {
$state = '<span style="color:#006EFF";>(Looking to Play)</span>';
$picture = '<span style="border-color:#006EFF border-style: solid";><img src="'.$steamprofile['avatarfull'].'" title = "" alt = ""></span>';
}
//Protected content
echo "Welcome back" . "</br> </br>" . $picture ."</br>". $steamprofile['personaname'] . "</br>" .$state . "</br>". "Steam ID: ". $steamprofile['steamid'] . "</br>";
echo 'Steam Profile' . "</br> </br>" . "<form action=\"steamauth/logout.php\" method=\"post\"><input value=\"Logout\" type=\"submit\" /></form>"; // Display their avatar!
}
?>
</h3>
</header>
<footer class="major container small">
<ul class="actions">
<li>Get Phishers</li>
</ul>
</footer>
</div>
<!-- Footer -->
<div id="footer">
<div class="container small">
<header class="major last">
<h2>Questions or comments?</h2>
</header>
<p>Program not working? Not detecting the phishers properly? <br \> Send us a message. We'll be sure to back to you as soon as possible.</p>
<form method="post" action="#">
<div class="row collapse-at-2">
<div class="6u">
<input type="text" name="name" placeholder="Name" />
</div>
<div class="6u">
<input type="email" name="email" placeholder="Email" />
</div>
</div>
<div class="row">
<div class="12u">
<textarea name="message" placeholder="Message" rows="6"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
<ul class="actions">
<li><input type="submit" value="Send Message" /></li>
</ul>
</div>
</div>
</form>
<ul class="icons">
<li><span class="label">Twitter</span></li>
<li><span class="label">Facebook</span></li>
<li><span class="label">Instagram</span></li>
<li><span class="label">Github</span></li>
<li><span class="label">Dribbble</span></li>
</ul>
<ul class="copyright">
<li>© Steap 2014 All rights reserved.</li><li>Design: HTML5 UP</li>
</ul>
</div>
</div>
</body>
</html>
Maybe you should run php server (such as Apache or another) on other port (such as 8080) and when you call any php file, make request from your flask server to php server. And result getting from php server show with flask server. I hope, you can find the way how to send request from flask to other server.
render_template() isn't support PHP.
You can use subprocess to run PHP script:
import subprocess as sp
#app.route('/example.php')
def phpexample():
out = sp.run(["php", "example.php"], stdout=sp.PIPE)
return out.stdout
Flask is not compatible with php. So it can't read php code.
Have you considered using JQuery Ajax?
Here's an example:
You have a file called get_name.php witch contains:
<?php echo "Hello, my name is John"; ?>
Using Jquery ajax function I call the get_name.php
$.ajax({
url : 'get_name.php',
success : function(data) {
console.log(data);
}
});
The output in the console would be:
Hello, my name is John
So, with the returned data you can do whatever you want.

Can't get login feature to work

I'm currently learning PHP and am creating a small CMS feature that includes a login area. I have used the code below which includes an include header file that contains the doctype/head info and the opening tag. It also includes the header content. I also have a connection file for connecting to the db.
My header include code is:
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title><?php echo $pagetitle ?></title>
<link rel="stylesheet" href="../stylesheets/foundation.css">
<link rel="stylesheet" href="../stylesheets/app.css">
<style>#import url(http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700,800); #import url (http://fonts.googleapis.com/css?family=Kreon:100,200,300,400);</style>
<script src="../javascripts/modernizr.foundation.js"></script>
</head>
<body>
<div class="subHeader">
<div class="row">
<div class="four columns logo">
<img src="../images/logo.png" alt="logo" />
</div>
<div class="eight columns navigation right">
<ul class="navigationMain">
<li class="<?php if($navcurrent == "home"){echo "navigationActive";} ?>">Home</li>
<li class="<?php if($navcurrent == "services"){echo "navigationActive";} ?>">Services</li>
<li class="<?php if($navcurrent == "work"){echo "navigationActive";} ?>">Recent Work</li>
<li class="<?php if($navcurrent == "about"){echo "navigationActive";} ?>">About</li>
<li class="<?php if($navcurrent == "contact"){echo "navigationActive";} ?>">Contact</li>
</ul>
</div>
<div class="twelve columns titlesection">
<h2><?php echo $headTitle ?></h2>
<h4><?php echo $headsubTitle ?></h4>
</div>
</div><!--End Feature Row-->
</div><!--End Feature-->
<div class="underbar">
<div class="bordertriangle"></div>
<div class="row">
<div class="eight columns"> </div>
<div class="three columns right socialcontainer">
<ul class="socialicons">
<li><a><img id="linkedinIcon" src="../images/socialli.png" alt="linkedin icon" /></a></li>
<li><a><img id="twitterIcon" src="../images/socialtw.png" alt="twitter icon" /></a></li>
<li><a><img id="facebookIcon" src="../images/socialfb.png" alt="facebook icon" /></a></li>
</ul>
</div>
</div>
When I open the admin page, the username password form, header and footer appear as they should. If I test the errors, they return as they should. However, when I successfully log in using a valid username and password, no content appears except the what is included in the header file. Can anyone point me in the direction of what i might be doing wrong? Any help would be much appreciated. I am a relative noob to PHP...
<?php
$pagetitle = "Admin";
$navcurrent = "home";
$headTitle = "ADMIN AREA";
$headsubTitle = "SITE ADMINISTRATION AREA";
include_once('../includes/connection.php');
include_once('../includes/headeradmin.php');
if (isset($_SESSION['logged_in'])) {
echo('Successfully Logged In');
} else {
if (isset($_POST['username'], $_POST['password'])){
$username = $_POST['username'];
$password = $_POST['password'];
if (empty($username) or empty($password)){
$error = 'An Error Has Occurred - All Fields Are Required';
}
else{
$query = $pdo->prepare('SELECT * FROM users WHERE user_name = ? AND user_password = ?');
$query->bindValue(1, $username);
$query->bindValue(2, $password);
$query->execute();
$num = $query->rowCount();
if ($num == 1) {
$_SESSION['logged_in'] = true;
header('location: index.php');
exit();
}
else{
$error = 'The username/password you entered was incorrect - Please try again';
}
}
}
?>
<div class="row">
<div class="four columns centered">
<?php if (isset($error)) { ?>
<h5 style="color: #e63333;"><?php echo $error; ?></h5>
<br />
<br />
<?php } ?>
<form action="index.php" method="post">
<input type="text" name="username" placeholder="Username" />
<input type="password" name="password" placeholder="Password" />
<input type="submit" value="Login" />
</form>
</div>
</div>
You can't use the header('location: index.php'); line if you've already output content (i.e - html code) to the browser when you included the header in this line include_once('../includes/headeradmin.php');
read the documentation of header - Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP
you need to redirect the user with the header() function before you output the head html of the admin page

Categories