PHP: Incrementation Error - Need assistance here [duplicate] - php

This question already has answers here:
increment variable each time page gets called or opened?
(7 answers)
Closed 6 years ago.
Good day i have a form which its values are grabbed from a database,
<?php
$questionId = 1;
$pdo = new PDO("mysql:host=localhost;dbname=mydb", 'root', '');
$sth = $pdo->prepare("SELECT * FROM questions WHERE questionid = $questionId");
$sth->execute();
while ($row = $sth->fetch()):
?>
<form action="exam1.php" method="POST">
<?php echo "<h4>".$row['instructions'] ."</h4><br>"; ?><h4><?php echo $questionId. ". ".$row['name']; ?></h4>
<input type="radio" name="choices" id="choices" value="<?php echo $row['choice1']; ?>"><?php echo $row['choice1']; ?> ...(other choices)
<input type="submit" name="submitAnswers">
</form>
<?php endwhile;?>
what i want is how to increment the $questionId variable once the form is being submitted. i tried this
if (isset($_GET['choices'])) {
echo $_GET['choices'];
$questionId = $questionId + 1;
}
the question id updated but just once.
i need it to always update by adding 1 to it.

<form method="post" >
<input type="submit" name="submit">
</form>
<?php
session_start();
$_SESSION['id'];
if (isset($_POST['submit'])) {
$_SESSION['id']=$_SESSION['id']+1;
}
echo "Value is => ",$_SESSION['id'],"<br>";
?>

Related

quiz: can not get the correct answer with rand() [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Reference - What does this error mean in PHP?
(38 answers)
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 3 years ago.
i got this code from technopoints, i want to get a random question from database but i cant work this out when i put the $fetchqry = "SELECT * FROM quiz ORDER BY rand()"; it does not get the correct result i think because of the rand(). i've been trying to fix this and it doesn't work and also how can i put an incremented number per question?
if (isset($_POST['click']) || isset($_GET['start'])) {
#$_SESSION['clicks'] += 1 ;
$c = $_SESSION['clicks'];
if(isset($_POST['userans'])) { $userselected = $_POST['userans'];
$fetchqry2 = "UPDATE 'quiz' SET 'userans'='$userselected' WHERE 'id'=$c-1";
$result2 = mysqli_query($db,$fetchqry2);
}
} else {
$_SESSION['clicks'] = 0;
}
//echo($_SESSION['clicks']);
?>
<div class="bump"><br><form><?php if($_SESSION['clicks']==0){ ?> <button class="button" name="start" float="left"><span>START QUIZ</span></button> <?php } ?></form></div>
<form action="" method="post">
<table>
<?php if(isset($c)) { $fetchqry = "SELECT * FROM 'quiz' ORDER BY rand()";
$result=mysqli_query($db,$fetchqry);
$num=mysqli_num_rows($result);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC); }
?>
<tr><td><h3><br><?php echo #$row['que'];?></h3></td></tr> <?php if($_SESSION['clicks'] && $_SESSION['clicks']){ ?>
<tr><td><input required type="radio" name="userans" value="<?php echo $row['option 1'];?>"> <?php echo $row['option 1']; ?><br>
<tr><td><input required type="radio" name="userans" value="<?php echo $row['option 2'];?>"> <?php echo $row['option 2'];?></td></tr>
<tr><td><input required type="radio" name="userans" value="<?php echo $row['option 3'];?>"> <?php echo $row['option 3']; ?></td></tr>
<tr><td><input required type="radio" name="userans" value="<?php echo $row['option 4'];?>"> <?php echo $row['option 4']; ?><br><br><br></td></tr>
<tr><td><button class="button3" name="click" >Next</button></td></tr> <?php }
?>
<form>
<?php if($_SESSION['clicks']){
$qry3 = "SELECT 'ans', 'userans' FROM 'quiz';";
$result3 = mysqli_query($db,$qry3);
$storeArray = Array();
while ($row3 = mysqli_fetch_array($result3, MYSQLI_ASSOC)) {
if($row3['ans']==$row3['userans']){
#$_SESSION['score'] += 1 ;
}
}
?>
<h2>Result</h2>
<span>No. of Correct Answer: <?php echo $no = #$_SESSION['score'];
session_unset(); ?></span><br>
<span>Your Score:&nbsp<?php echo $no*2; ?></span>
<?php } ?>```
I think your sql query should be like this:
$fetchqry = "SELECT * FROM quiz ORDER BY rand() LIMIT 1";
I'm not pretty sure if you have to show all the rows into quiz or only one, if its like this please try with LIMIT 1

unable to print php variable in php code [duplicate]

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 6 years ago.
i want to change value of 2nd input after submitting the value of first input but it's not happening. i want to print email in 2nd input value
<?php
$db = mysqli_connect('localhost','root','root','rahul');
if(isset($_POST['submit']))
{
$email = $_POST['email'];
$sql = mysqli_query($db,"select * from register where email='$email'");
if(mysqli_fetch_array($sql)==true)
{
echo '<input type="text" name="question" value="$email">'; /* 2nd input */
}
else
{
echo "no";
}
}
?>
<center><br><br>
<form action="" method="post" >
<input type="text" name="email"><br><br> /* first input*/
<input type="submit" name="submit">
</form>
echo '<input type="text" name="question" value="$email">'; : This will not work because you have to use double quotes to echo php var in it
Like this :
echo "<input type='text' name='question' value='$email'>";
or like this :
echo "<input type='text' name='question' value='".$email."'>";

Inserting Multiple data into MYSQL database

I am having problem inserting multiple data into MYSQL database. With the code below, I am only able to insert the data I have input. Let's say there are 3 questions and I must submit 3 inputs, it only submitted the last one.
<?php
include('questionDB.php');
if(isset($_POST['submit'])){
$questionID = $_POST['id'];
$answer = mysql_real_escape_string(htmlspecialchars($_POST['answer']));
$insert = mysql_query("INSERT INTO answers(survey_id, question_id, answer_body) VALUES ('1','" . $questionID . "', '" . $answer . "')");
if ($insert){
echo "Success";
} else {
echo "Failed";
}
}
$startTimeAuc = mysql_query("SELECT startTime FROM questions WHERE survey_id='1'");
$startTime = mysql_fetch_assoc($startTimeAuc);
$startTime = ($startTime['startTime']);
$endTimeAuc = mysql_query("SELECT endTime FROM questions WHERE survey_id='1'");
$endTime = mysql_fetch_assoc($endTimeAuc);
$endTime = ($endTime['endTime']);
$currentTimeAuc =(date("Y-m-d H:i:s"));
if( ( $currentTimeAuc >= $startTime && $currentTimeAuc <= $endTime)){
?>
<form name="auctionQuestion" method="post">
<?php
$auctionSurvey = "SELECT question_id, survey_id, question_body FROM questions
WHERE survey_id='1'";
$aucResult = mysql_query($auctionSurvey) or die (mysql_error());
while($auctionRow = mysql_fetch_assoc($aucResult)){
echo "<p class=\"questions\">". $auctionRow['question_body']."</p>". "<input type=\"text\" name=\"answer\" class=\"answerField\"><BR>";
?>
<input type="hidden" name="id" value="<?php echo $auctionRow ['question_id'] ?>">
<?php
}
?>
<input type="submit" class="submit" name="submit" value="Submit">
</form>
</div>
<?php
}
?>
Few points:
1. your code is vulnerable to sql injection, use prepared data or SQLI/PDO
2. All your questions and answers are given the same name that's why you get only one inserted. Try giving them different names (using -1/-2/-3 sufix etc.), or if I make this, I would have 3 questions and 3 answers in the same record in database instead of inserting 3 times.
3. See below part. You need to add variables to both answer and id. Otherwise they are getting the same name.
<input type=\"text\" name=\"answer\" class=\"answerField\">
<input type="hidden" name="id" value="<?php echo $auctionRow ['question_id'] ?>">

Delte record from db [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Can i delete a record using the following function? If yes how can i do this? I have to pass the function to the submit button?
function deleteImage($id = "")
{
if(isset($_POST['delete_image']))
{
$query = mysql_query("DELETE FROM store WHERE id='$id'");
}
return $query;
}
the html looks like this:
<?php
$images = mysql_query("SELECT * FROM store");
while($row = mysql_fetch_assoc($images)) {
?>
<form action="admin.php" method="post">
<?php
echo "<img src=".$row['image'].">";
?>
<input type="submit" name="delete_image" value="DELETE IMAGE">
</form>
<?php
}
?>
Thanks in advance!
You can do something like this , add hidden input in form inside while loop.
<input type="hidden" name="id" value="<?php echo $row['id']; ?>"
Then in action.php file
$id = $_POST['id'];
if(isset($_POST['delete_image']))
{
$query = mysql_query("DELETE FROM store WHERE id='$id'");
}
You should try this.
<?php
if(isset($_POST['delete_image']))
{
$query = mysqli_query("DELETE FROM store WHERE id='".$_POST['image_id']."'")or die(mysqli_error());
}
$images = mysqli_query("SELECT * FROM store");
while($row = mysqli_fetch_assoc($images))
{
?>
<form action="admin.php" method="post">
<?php echo "<img src=".$row['image'].">"; ?>
<input type="hidden" id="image_id_<?php echo $row['id']; ?>" value="<?php echo $row['id']; ?>" name="image_id"/>
<input type="submit" name="delete_image" value="DELETE IMAGE">
</form>
<?php
}
?>
I passed image id in hidden field.
This your view
<td>
<?php $images = mysql_query("SELECT * FROM store"); ?>
<form action="index.php" method="post">
<?php while($row = mysql_fetch_assoc($images)): ?>
<img src="<?php echo $row['image']; ?>">
<input type="hidden" name="deleting[]" value="<?php echo $row['id_from_database'] ?>">
<?php endwhile; ?>
<input type="submit" name="delete_image" value="DELETE IMAGE">
</form>
</td>
This your code
<?php
function deleteImage()
{
if(isset($_POST['delete_image'], $_POST['deleting']) && !empty($_POST['deleting']))
{
$ids = array_map('intval', $_POST['deleting']); // Type conversion to integer
$ids = implode(', ', $ids); // Bringing to id1,id2...
$query = mysql_query('DELETE FROM images WHERE id IN(' . $ids . ')'); // Deleting
return (bool)$query;
}
}
?>
Now you need set checkbox which images you want to delete and press submit.

Display 3 items per row - while loop - php/mysql [duplicate]

This question already has answers here:
display only 3 foreach result per row
(2 answers)
Closed 1 year ago.
I am currently working on a loop to display items from a mysql table. Is there a simple way to display 3 items per row. So far I managed to display all the items in a single row inside an html table . I would appreciate any help;
code (without html table tags) below:
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<?php
$database_name = "vog";
$conn = mysql_connect("localhost","root","toor");
mysql_select_db($database_name);
$sql = "select * from client1";
$result = mysql_query($sql) or die(mysql_error());
$num = mysql_num_rows($result); //Ελεγχος αν υπάρχουν εγγραφές!
?>
<?php
if($num){
while ($row = mysql_fetch_array($result))
{
echo $img_id = $row['img_id'];
?>
<form name="add2cart" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php echo $row['img_name'];?>
<?php echo "<img src=".$row['img_path'].">";?>
<select name="color">
<option>bnw</option>
<option>sepia</option>
</select>
<input type="hidden" name="img_name" value="<?php echo $row['img_name']; ?>">
<input type="submit" name="add2cart" value="Add to cart"></input>
<br />
</form>
<?php
}
}
else{
echo "Δεν υπάρχουν εγγραφές με τα κριτήρια που επιλέξατε";
}
//add2cart section
if(isset($_POST['add2cart'])){
$img_name = $_POST['img_name'];
$color = $_POST['color'];
$sql_order ="insert into orders(item_id, img_name, color)
values(' ', '$img_name', '$color')";
$result = mysql_query($sql_order);
}
?>
Declare a variable before your loop like: $currentRow = 1 and then inside, and at the end, of your loop add $currentRow++
You can then check if your row is divisible by 3 if($currentRow % 3 == 0) and put a break in.

Categories