<?php
$tid = $_GET['tid'];
$id = $_SESSION['userid'];
$sql1 = "SELECT * FROM topics WHERE id='$tid' LIMIT 1";
$res1 = mysqli_query($connect, $sql1) or die(mysqli_error($connect));
while ($row = mysqli_fetch_array($res1, MYSQLI_ASSOC)) {
$title = $row['topic_title'];
$creator = $row['topic_creator'];
}
$sql = "SELECT * FROM users WHERE id='$creator' LIMIT 1";
$user_query = mysqli_query($connect, $sql) or die(mysqli_error($connect));
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
$name = $row["first"].$row["last"];
}
echo $name;
?>
I'm a little new to PHP, but I've done things exactly like this, but this time I'm getting an error. Everything here works except for $name. I checked my SQL tables and made sure users exist and that there's first and a last area. I don't see what else could be wrong.
Notice: Undefined variable: name in * on line **
Thank you.
Try this code on for size:
<?php
$tid = $_GET['tid'];
$id = $_SESSION['userid'];
$tid = mysqli_escape_string($connect, $tid);
$sql1 = "SELECT * FROM topics WHERE id='{$tid}' LIMIT 1";
$res1 = mysqli_query($connect, $sql1) or die(mysqli_error($connect));
// Check for rows first.
if($res1 and mysqli_num_rows($res1)){
// Use if as while is pointless on LIMIT 1
if($row = mysqli_fetch_array($res1, MYSQLI_ASSOC)) {
$title = $row['topic_title'];
$creator = $row['topic_creator'];
$creator = mysqli_escape_string($connect, $creator);
$sql = "SELECT * FROM users WHERE id='{$creator}' LIMIT 1";
$user_query = mysqli_query($connect, $sql) or die(mysqli_error($connect));
// Check for rows first.
if($user_query and mysqli_num_rows($user_query)){
// Use if as while is pointless on LIMIT 1
if ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
$name = $row["first"].$row["last"]; // NO HIT!
}
echo $name;
}else{
echo 'no rows found (query 2).';
}
}
}else{
echo 'no rows found (query 1).';
}
?>
Variable $name is undefined because the $name = ...; line is not reached. So make sure you $sql query actually returns results. It has to in order to define $name.
Related
This is for a quiz application.
This is my comparison where $number is the question number and will increase every time the user submits an answer and we will assume $totalquestions = 3 for now as the category has only 3 questions.
I've echoed both values to make sure they are what I expect them to be and done var_dump($number == $totalQuestions); which returns true when $number = 3 but does not execute my code to redirect to the finish page once all questions are done and I have no clue why.
Any help would be great as I've been staring at this problem for hours!
<?php include('server.php'); ?>
<?php
if(!isset($_SESSION['geoScore'])){
$_SESSION['geoScore'] = 0;
}
//Get quiz category
$activeCategoryID = (int) $_GET['c'];
$query = "SELECT * FROM `category` WHERE CategoryID = $activeCategoryID";
$result = mysqli_query($db, $query);
$categoryName = mysqli_fetch_assoc($result);
echo $categoryName['CategoryName'];
//Get question number
$number = (int) $_GET['n'];
$query = "SELECT * FROM `questions` WHERE QuestionID = $number AND CategoryID = $activeCategoryID";
$result = mysqli_query($db, $query);
$question = mysqli_fetch_assoc($result);
//Get choices for question
$query = "SELECT * FROM `answers` WHERE QuestionID = $number";
$choices = mysqli_query($db, $query);
//Get total questions for category
$query = "SELECT * FROM `questions` WHERE CategoryID = $activeCategoryID";
$result = mysqli_query($db, $query);
$totalQuestions = mysqli_num_rows($result);
echo $totalQuestions;
echo $number;
var_dump($number == $totalQuestions); // returns true because values are equal
$result = mysqli_query($db, $query);
$row = mysqli_fetch_assoc($result);
$correctChoice = $row['AnswerID'];
echo $correctChoice;
if($_SERVER["REQUEST_METHOD"] == "POST"){
$number = $_POST['number'];
$selectedChoice = $_POST['choice'];
$next = $number+1;
$activeCategory = $_POST['activeCategoryID'];
$query = "SELECT * FROM `questions` WHERE CategoryID = activeCategoryID";
$result = mysqli_query($db, $query);
$totalQuestions = mysqli_num_rows($result);
//Get correct choice
$query = "SELECT * FROM `answers` WHERE QuestionID = $number AND Correct = 1";
$result = mysqli_query($db, $query);
$row = mysqli_fetch_assoc($result);
$correctChoice = $row['AnswerID'];
if($correctChoice == $selectedChoice){
$_SESSION['geoScore']++;
}
//Check to see if questions have finished
if($number == $totalQuestions){
header("Location: finish.php");
}else{
header("Location: geoQuiz.php?n=".$next."&c=".$activeCategory);
}
}
?>
EDIT: Previous working version that did not dynamically get the quiz category
<?php
if(!isset($_SESSION['geoScore'])){
$_SESSION['geoScore'] = 0;
}
//Get question number
$number = (int) $_GET['n'];
$query = "SELECT * FROM `questions` WHERE QuestionID = $number AND CategoryID = 1";
$result = mysqli_query($db, $query);
$question = mysqli_fetch_assoc($result);
//Get choices for question
$query = "SELECT * FROM `answers` WHERE QuestionID = $number";
$choices = mysqli_query($db, $query);
//Get total questions for category
$query = "SELECT * FROM `questions` WHERE CategoryID = 1";
$result = mysqli_query($db, $query);
$totalQuestions = mysqli_num_rows($result);
if($_SERVER["REQUEST_METHOD"] == "POST"){
$number = $_POST['number'];
$selectedChoice = $_POST['choice'];
$next = $number+1;
$_SESSION['activeCategory'] = "Geography";
$query = "SELECT * FROM `questions` WHERE CategoryID = 1";
$result = mysqli_query($db, $query);
$totalQuestions = mysqli_num_rows($result);
//Get correct choice
$query = "SELECT * FROM `answers` WHERE QuestionID = $number AND Correct = 1";
$result = mysqli_query($db, $query);
$row = mysqli_fetch_assoc($result);
$correctChoice = $row['AnswerID'];
echo $correctChoice;
echo $selectedChoice;
if($correctChoice == $selectedChoice){
$_SESSION['geoScore']++;
}
echo $geoScore;
//Check to see if questions have finished
if($number == $totalQuestions){
header("Location: finish.php");
}else{
header("Location: geoQuiz.php?n=".$next);
}
}
?>
The above code was my previous version which worked but I would needed to have had multiple php pages for each quiz category so I tried to change this to take the category using $_GET[] in the link from index page and it seems to all work apart from when the questions are finished and to head to finish.php
I have a form that users enter data in and it gets entered into a mysql database. The issue is when they have entered a "%" sign or other special characters it causes problems when my website is trying to display the record. It actually causes nothing to be shown for that record when displaying results. How do I fix this?
$query = "SELECT * FROM makerperk WHERE pid='$pid' LIMIT 1";
$result = mysqli_query($connection, $query) or die(mysqli_error($connection));
while($row = mysqli_fetch_assoc($result)) {
$makerid = $row['makerid'];
$name = $row['name'];
$title = $row['title'];
$perkdescription = $row['perkdescription'];
$image = $row['image'];
$perktype = $row['perktype'];
$restrictions = $row['restrictions'];
}
I think you should use PHP mysqli_real_escape_string
/*Escape input variable:*/
$pid = mysqli_real_escape_string($connection, $pid);
/*Run query with escaped string:*/
$query = "SELECT * FROM makerperk WHERE pid='$pid' LIMIT 1";
$result = mysqli_query($connection, $query) or die(mysqli_error($connection));
while($row = mysqli_fetch_assoc($result)) {
$makerid = $row['makerid'];
$name = $row['name'];
$title = $row['title'];
$perkdescription = $row['perkdescription'];
$image = $row['image'];
$perktype = $row['perktype'];
$restrictions = $row['restrictions'];
}
how would i get this to display a message in place of the Query if no result is found i updated the code but its just showing "N"
<?php
$hostname = "...";
$username = "";
$password = "";
$db = "";
$dbconnect=mysqli_connect($hostname,$username,$password,$db);
if ($dbconnect->connect_error) {
die("Database connection failed: " . $dbconnect->connect_error);
}
$query=mysqli_query($dbconnect,"SELECT DISTINCT companyname,client_id,feedback,status from review WHERE status=1 ORDER BY RAND() LIMIT 4");
$rows_get = mysqli_num_rows($query);
if ($rows_get >0)
{
$query2=mysqli_query($dbconnect,"SELECT DISTINCT companyname,client_id,feedback,status from review WHERE status=1 ORDER BY RAND() LIMIT 4");
$row1 = mysqli_fetch_assoc($query2);
$row2 = mysqli_fetch_assoc($query2);
$row3 = mysqli_fetch_assoc($query2);
$row4 = mysqli_fetch_assoc($query2);
$row5 = mysqli_fetch_assoc($query2);
}else {
$row1 = "N0 Data";
$row2 = "N0 Data";
$row3 = "N0 Data";
$row4 = "N0 Data";
$row5 = "N0 Data";
}
?>
Do as follows:
After $query insert this:
$rows_get = mysqli_num_rows($query);
if ($rows_get >0)
{
//do all database operation
}else {
echo " No data found";
}
Hope this helps.
Amend your code for example..
if ($row_get>0){
//i assume you are getting multiple rows
while ($data =mysqli_fetch_assoc ($query))
{
//run this loop and you will get all you rows.
}
}
i have two button on my homepage one is time-in and the other is time-out,
i want to prevent the user/student to time-in using same id if he did not put time-out on his last time-in to create valid entry. Hope you can help me.
here is my php code:
<?php
include_once('connection.php');
if(isset($_POST['submit0'])){
$rfid = $_POST['rfid'];
$time=date("H:i:s");
$sql = mysqli_query($conn, "SELECT * FROM stud WHERE rfid_num = '$rfid'");
$count = mysqli_num_rows($sql);
if ($count == 0 ) {
header("location:notexist.php");
} elseif (empty($row['timeout'])) {
header("location:page say the user/student need to put timeout first before time-in again");
} else {
while( $row = mysqli_fetch_array($sql)) {
$rfid=$row['rfid_num'];
$id=$row['id'];
$name0 = $row['name'];
$course0 = $row['course'];
$image = $row['image'];
$InsertSql = "INSERT INTO student_att(rfid_num,id,name,course,image,timein) VALUES ('$rfid','$id','$name0','$course0','$image','$time')";
$res = mysqli_query($conn, $InsertSql);
}
}
}
?>
this is my answer just wanna share it, i just add select student_att table
to fetch the data and check if timeout column is empty.
<?php
include_once('connection.php');
if(isset($_POST['submit0'])){
$rfid = $_POST['rfid'];
$time=date("H:i:s");
$sql = mysqli_query($conn,"select * from stud where rfid_num ='$rfid' ");
$count = mysqli_num_rows($sql);
if ($count == 0) {
header("location:notexist.php");
}else{
while( $row = mysqli_fetch_array($sql)) {
$rfid=$row['rfid_num'];
$id=$row['id'];
$name0 = $row['name'];
$course0 = $row['course'];
$image = $row['image'];
$sql1 = mysqli_query($conn,"select * from student_att where rfid_num ='$rfid' order by number DESC limit 1 ");
while( $row = mysqli_fetch_array($sql1)) {
if(empty($row['timeout'])){
header("location:logout.php");
}else{
$InsertSql = "INSERT INTO student_att(rfid_num,id,name,course,image,timein) VALUES ('$rfid','$id','$name0','$course0','$image','$time')";
$res = mysqli_query($conn, $InsertSql);
}
}
}
}
}
?>
<?php
$query = "SELECT bobot FROM `record_result` WHERE `participantid` = $idParticipant AND `questionid` = 1";
$query1 = "SELECT bobot FROM `record_result` WHERE `participantid` = $idParticipant AND `questionid` = 2";
$comments = mysql_query($query);
$comments1 = mysql_query($query1);
while($row = mysql_fetch_array($comments, MYSQL_ASSOC)) {
$bobot = $row['bobot'];
$bobot = htmlspecialchars($row['bobot'],ENT_QUOTES);
}
while($row = mysql_fetch_array($comments1, MYSQL_ASSOC)) {
$bobot1 = $row['bobot'];
$bobot1 = htmlspecialchars($row['bobot'],ENT_QUOTES);
}
?>
I want to make this code can looping until 10. I hope that there aren't many variable, ex: $query, $query1, $query2, ..., $query10, $comments, $comments1, $comments2, ..., $comments10, $bobot, $bobot1, $bobot2, ..., $bobot10. Someone help me, please ...
You're almost there. But I have to mention that you should start using parameterized queries with prepared statements instead of constructing your queries manually.
$id = 1;
while($id <= 10) {
// construct your query
$query = "SELECT bobot FROM `record_result` WHERE `participantid` = $idParticipant AND `questionid` = $id";
// execute and get results
$comments = mysql_query($query);
// iterate over records in result
while($row = mysql_fetch_array($comments, MYSQL_ASSOC)) {
$bobot = $row['bobot'];
$bobot = htmlspecialchars($row['bobot'],ENT_QUOTES);
}
// increment the id for next cycle through the loop
$id = $id + 1;
}