Ok it’s really pissing me off I have no idea. As far I established that it looks for POST variables from the form and not GET so I tried and changed my code to this (sorry for pasting it all, but there might be smth wrong that i don’t see, and there are no specific errors)
<?php
include("config.php");
session_start();
?>
<?php
// define variables and initialize with empty values
$rateErr = $comErr = "";
$rating = $comment = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ($_POST["rating"] == "") {
$rateErr = "Rate the app";
}
else {
$rating= $_POST["rating"];
}
if (empty($_POST["comment"])) {
$comErr = "Missing";
}
else {
$comment = $_POST["comment"];
}
}
$appID = $appIDErr = "";
if ($_SERVER["REQUEST_METHOD"] == "GET") {
if (empty($_GET["id"])) {
$appIDErr = "Missing";
}
else {
$appID = $_GET["id"];
}
}
if ($rateErr == "" && $comErr == "" && $appIDErr == "") {
try {
$con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "INSERT INTO reviews (rating, content, appID, user) VALUES (:rating, :comment, :id, :username)";
$stmt = $con->prepare( $sql );
$stmt->bindValue( ":rating", $rating);
$stmt->bindValue( ":comment", $comment);
$stmt->bindValue( ":id", $appID);
$stmt->bindValue( ":username", $_SESSION['username']);
$stmt->execute();
return "Submitted successfully";
}
catch( PDOException $e ) {
echo $e->getMessage();
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>eduApp</title>
<link href='http://fonts.googleapis.com/css?family=Josefin+Slab:400,700' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Ubuntu' rel='stylesheet' type='text/css'>
<meta name="description" content="Educational apps for Android devices, smartphones, tablets. Language learning, coding, studying, various subjects." />
<meta name="keywords" content="Android app, educational, language learning, self study, mobile devices, smartphone, tablet" />
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<header>
<div id="tagline">
<h1>eduApp</h1>
<h2>mobile knowledge feed</h2>
<?php
if (!$_SESSION["loggedIn"]) echo " You Are Not Logged In <a href=login.php> Login </a>";
if ($_SESSION["loggedIn"]) echo " You are logged in as " .$_SESSION['username']. "<a href='logout.php'> Click Here </a> To Logout ";
?>
</div>
</header>
<nav>
<div id="wrapper">
<ul class="menu">
<li><a href="index.php">Home</a></li>
<li><a href="apps.php">Apps</a></li>
<li><a href="#">About us</a></li>
<li><a href="#">Contact us</a></li>
<li><a href="#">Tutor page</a></li>
</ul>
</div>
</nav>
<div id="contentwrapper">
<div id="main">
<?php
try {
$con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch (PDOException $e) {
echo $e->getMessage(); //catch and show the error
}
$stmt = $con->prepare( "SELECT * FROM apps WHERE appID = :id" );
$stmt->bindParam(":id", $_GET['id']);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
?>
<?php while( $row = $stmt->fetch()) { ?>
<h2><?php echo $row['app_name']; ?></h2>
<img class='float' src="<?php echo $row['picture']; ?>" alt="app image" />
<div id='txt'><p>Category: <?php echo $row['category']; ?></p>
<p><?php echo $row['description']; ?></p>
<a href="<?php echo $row['link']; ?>">Download</a></div>
<?php } ?>
<?php
$stmt2 = $con->prepare("SELECT AVG(rating) FROM reviews WHERE appID = :id GROUP BY appID");
$stmt2->bindParam(":id", $_GET['id']);
$results = $stmt2->fetch(PDO::FETCH_ASSOC);
$stmt2->execute();
if (empty($results)) {
echo 'No reviews added yet';
}
else {
$row = $stmt2->fetch();
echo "<p>Average rating " . $row['AVG(rating)'] . "</p>";
}
?>
<?php
if ($_SESSION["loggedIn"]) { ?>
<!--//include("form.php");
echo "<a href='form.php?id=".$_GET['id']."'>Add a review</a>";-->
<form method="POST"
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<select name="rating">
<option value=""></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<input type="radio" name="sex" value="male">Submit<br>
<span class="error"><?php echo $rateErr;?></span>
<span class="error"><?php echo $appIDErr;?></span>
<br />
<textarea rows="4" cols="50" name="comment" value="<?php echo htmlspecialchars($comment);?>">Enter text here...</textarea>
<span class="error"><?php echo $comErr;?></span>
<input type="submit" name="submit" value="Submit">
</form>
<?php }
else {
echo "You need to login to review this app";
}
?>
</div>
<aside>
Bo mialam goraczke i bardzo mala odpornosc na pylki
</aside>
</div>
<footer>
</footer>
</body>
</html>
This gives me a blank page and if I remove this bit :
$appID = $appIDErr = "";
if ($_SERVER["REQUEST_METHOD"] == "GET") {
if (empty($_GET["id"])) {
$appIDErr = "Missing";
}
else {
$appID = $_GET["id"];
}
}
It gives me again Integrity constraint violation: 1048 Column ‘appID’ cannot be null