This is driving me nuts, I am learning PHP and this problem has got me all day. I am pulling some info from a users database into a file and then using it in a form to insert into another table.
I want to do this with the users ID and username, I immediately tried $userID = $_GET['id'];
which works great but does not when trying $username = $_GET['username']
. To get to this form I am echoing the user ID into the url, could this be why it will only get the ID?
If I echo the username it works, so a little stumped. I guess if I echoed the username into the url it would work and ID would not, but I don't want to echo the username or both to be honest unless I have too.
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
include_once '../includes/conn.php';
if(!$user->is_loggedin()){
$user->redirect('../users/login.php');
}
$stmt = $conn->prepare("SELECT id, username FROM users");
$stmt->execute();
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
$userID = $_GET['id'];
$username = $_GET['username'];
if(isset($_POST['submit'])){
$category = trim($_POST['category']);
$points = trim($_POST['points']);
$reason = trim($_POST['reason']);
if($category==""){
$error[] = "Select category.";
}else if($points==""){
$error[] = "Provide points.";
}else if($reason==""){
$error[] = "Provide reason.";
}else if(strlen($reason) < 6){
$error[] = "Reason must be at least 6 characters<br /><br />";
}else{
try{
$sql = "INSERT INTO infractions(userid,username,category,points,reason)VALUES(?,?,?,?,?)";
$stmt = $conn->prepare($sql);
$stmt->execute(array($userID, $username, $category, $points, $reason));
}
catch(PDOException $e){
echo $e->getMessage();
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>EpicOwl UK | CMS Users Add Infraction</title>
<meta charset="utf-8">
<link rel="shortcut icon" href="../images/favicon.ico" type="image/x-icon" />
<link rel="stylesheet" type="text/css" href="../css/main.css">
</head>
<body>
<div id="header">
<a href="index.php"><img id="logo" src="../images/logo.png" /></a>
<div id="navigation">
<ul>
<a href="../index.php"><li>Home</li></a>
<a href="../users/profile.php"><li>My Profile</li></a>
<a href="./index.php"><li>Admin Panel</li></a>
</ul>
</div>
</div>
<div id="content">
<form method="$_POST"><br />
<h2>Give <?php echo ($userRow['username']); ?> an Infraction</h2>
<label><strong>Category:</strong></label><br />
<select name="category">
<option value="Select Category">Select Category</option>
<option value="Language Used">Language Used</option>
<option value="Breaking Rules">Breaking Rules</option>
<option value="Double Posting">Double Posting</option>
</select><br /><br />
<label><strong>Number of points to award:</strong></label><br />
<input type="text" name="points" maxlength="50" /><br /><br />
<label><strong>Reason:</strong><label><br />
<textarea name="reason" rows="13" cols="60" maxlength="255"></textarea><br /><br />
<button type="submit" name="submit">Add Infraction</button><br /><br /><br />
</form>
</div>
<div id="footer">
<p class="copyright">© EpicOwl UK. All Rights Reserved.</p>
</div>
</body>
</html>