unable to print value - php

It is MCQ exam code. Everything in the code is working because of $p3[8] i am not getting the required output. It is unable to print $p3[8] value also.
<?php
session_start();
error_reporting(1);
include("connect.php");
extract($_POST);
extract($_GET);
extract($_SESSION);
$username = $_SESSION['student'];
if(isset($testid))
{
//$_SESSION[sid]=$subid;
//echo 'sads';
$_SESSION['tid']=$testid;
header("location:test.php");
}
if(!isset($_SESSION['tid']))
{//echo 'qwerwq';
header("location:home.php");
}
?>
<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">
<title>Online Exam</title>
<link href="quiz.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
include("head.php");
$rs=mysqli_query($con, "select * from addmcq where test_id=$tid",$cn);
$r = mysqli_num_rows($rs);
if($r==0)
{
//header("location:test2.php");
}
if(!isset($_SESSION['qn']))
{
$_SESSION['qn']=0;
mysqli_query($con, "delete from mcq_user where sess_id='$username' and test_id=$tid") or die(mysql_error());
$_SESSION['trueans']=0;
}
else
{
if($submit=='Next MCQ' && isset($ans))
{
mysqli_data_seek($rs,$_SESSION['qn']);
$row= mysqli_fetch_row($rs);
mysqli_query($con, "delete from mcq_user where sess_id='$username', test_id=$tid and ques=$row[3]");
echo "ww";
mysqli_query($con, "insert into mcq_user(sess_id, test_id, que, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('$username', $tid,'$row[3]','$row[4]','$row[5]','$row[6]', '$row[7]','$row[8]','$ans')") or die(mysql_error());
if($ans==$row[8])
{
$_SESSION['trueans']=$_SESSION['trueans']+1;
}
$_SESSION['qn']=$_SESSION['qn']+1;
}
else if($submit=='Next EA' && isset($ans))
{
mysqli_data_seek($rs,$_SESSION['qn']);
$row= mysqli_fetch_row($rs);
mysqli_query($con, "delete from mcq_user where sess_id='$username', test_id=$tid and ques=$row[3]");
mysqli_query($con, "insert into mcq_user(sess_id, test_id, que, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('$username', $tid,'$row[3]','$row[4]','$row[5]','$row[6]', '$row[7]','$row[8]','$ans')") or die(mysql_error());
if($ans==$row[8])
{
$_SESSION['trueans']=$_SESSION['trueans']+1;
// echo $_SESSION['trueans'];
}
//echo "<h1 class=head1> Result</h1>";
$_SESSION['qn']=$_SESSION['qn']+1;
//$_SESSION['trueans'];
header("location:test2.php");
//echo '<br />Start short answer type questionsclick here';
}
else if($submit=='Previous MCQ' && isset($ans))
{
mysqli_data_seek($rs,$_SESSION['qn']);
$row= mysqli_fetch_row($rs);
mysqli_query($con, "insert into mcq_user(sess_id, test_id, que, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('$username', $tid,'$row[3]','$row[4]','$row[5]','$row[6]', '$row[7]','$row[8]','$ans')") or die(mysql_error());
if($ans==$row[8])
{
$_SESSION['trueans']=$_SESSION['trueans']+1;
}
$_SESSION['qn']=$_SESSION['qn']-1;
}
}
Main problem starts from here $p2 also not printing the value
$rs=mysqli_query($con, "select * from addmcq where test_id=$tid",$cn) or die(mysql_error());
$r = mysqli_num_rows($rs);
$p1= mysqli_query($con, "select * from mcq_user where test_id=$tid and sess_id=$username");
$p2 = mysqli_num_rows($p1);
//echo "qqqqq";
echo $p2;
if($r==0)
{
header("location:test2.php");
}
if($_SESSION['qn']>mysqli_num_rows($rs)-1)
{
//unset($_SESSION[qn]);
//echo "<h1 class=head1>Some Error Occured</h1>";
//session_destroy();
//echo "Please <a href=head.php> Start Again</a>";
exit;
}
mysqli_data_seek($rs,$_SESSION['qn']);
mysqli_data_seek($p1,$_SESSION['qn']);
echo $_SESSION['qn'];
$row= mysqli_fetch_row($rs);
$p3 initialization and displaying it to check.
$p3= mysqli_fetch_row($p1);
echo $p3[8];
//include("tt.html");
echo "<form name=myfm method=post action=test.php>";
echo "<table width=100%> <tr> <td width=30> <td> <table border=0>";
$n=$_SESSION['qn']+1;
echo "<tr><td><span class=style2>Que ". $n .": $row[3]</style>";
echo "<tr><td class=style8><input ";
if($p3[8]==1) echo "checked='checked'";
echo " type=radio name=ans value=1>$row[4]";
echo "<tr><td class=style8> <input ";
if($p3[8]==2) echo "checked='checked'";
echo" type=radio name=ans value=2>$row[5]";
echo "<tr><td class=style8><input ";
if($p3[8]==3) echo "checked='checked'";
echo" type=radio name=ans value=3>$row[6]";
echo "<tr><td class=style8><input ";
if($p3[8]==4) echo "checked='checked'";
echo" type=radio name=ans value=4>$row[7]";
if($n == 1)
echo "<tr><td><input type=submit name=submit value='Next MCQ'><td><tr></form>";
if($_SESSION['qn']<mysqli_num_rows($rs)-1 && $n > 1)
echo "<tr><td><input type=submit name=submit value='Previous MCQ'><input type=submit name=submit value='Next MCQ'><td><tr></form>";
if($_SESSION['qn']== mysqli_num_rows($rs)-1)
echo "<tr><td><input type=submit name=submit value='Previous MCQ'><input type=submit name=submit value='Next EA'><td><tr></form>";
echo "</table></table>";
?>
</body>
</html>

make sure that this query
$p1= mysqli_query($con, "select * from mcq_user where test_id=$tid and sess_id=$username");
will return a record which consists of atleast 8 column...
one more thing
if $username is a string than you should use single quote around it
$p1= mysqli_query($con, "select * from mcq_user where test_id=$tid and sess_id='$username'");

Related

Session variable and Dynamic Pagination in php

<form action='movies.php' method='POST'>
Language: <select name="language">
<option selected>hindi</option>
<?php
require("config.php");
$result="SELECT language FROM movies";
$q = mysqli_query($conn,$result) or die(mysql_error());
while ($row=mysqli_fetch_array($q)) {
$s1=$row["language"];
echo "<option>
$s1
</option>";
}
echo "<br>"
?>
</select>
<br /> <br />
<input type='submit' value='Submit' />
</form>
<?php
$lang=#$_POST['language'];
$_SESSION["lang1"]=$lang;
/*
if($lang){
$sql = "SELECT name,language FROM movies WHERE language='$lang'";
$result = mysqli_query($conn,$sql);
if (mysqli_num_rows($result)>0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<div class="query"> <img src="images\tiles\bb.jpg"> ';
echo "<h3> " . $row["name"]."</h3>". "<br>";
echo "</div>";
}
} else {
echo "0 results";
}
}
*/
$page=#$_GET['page'];
if($page==""|| $page=="1")
{
$page1=0;
}
else{
$page1=($page*3)-3;
}
$sql = "SELECT name,language FROM movies WHERE language='". $_SESSION['lang1']."' limit $page1,3 ";
$result = mysqli_query($conn,$sql);
$a= mysqli_num_rows($result);
while ($list=mysqli_fetch_array($result))
{
echo $list['name'] . " : " . $list['language'] . "<br />";
}
$sql = "SELECT name,language FROM movies WHERE language='$lang'";
$result = mysqli_query($conn,$sql);
$a= mysqli_num_rows($result);
$numrows=$a;
$rowsperpage=3;
$totalpages= ceil($numrows/$rowsperpage);
echo "</br>";
for($b=1;$b<=$totalpages;$b++)
{
?><?php echo $b." ";?> <?php
}
?>
I get proper output on movies.php which is the first 3 rows from database,but when i click on the dynamically created pagination link like movies.php?page=2 then there is no output on this page.This code works fine if i manually set the
$_SESSION["lang1"]="English"; then i get proper output but when i take input from the form it doesnt work.

Using Loop in SQL Query

I want to add loop to my sql query so that all the data from the table gets updated at one click. Right now only the last row is getting updated.
<?php
include_once 'db_connect.php';
if(isset($_POST['btn-signup']))
{
$PersonID = mysql_real_escape_string($_POST['PersonID']);
$FirstName = mysql_real_escape_string($_POST['FirstName']);
$Status = mysql_real_escape_string($_POST['Status']);
$Date = mysql_real_escape_string($_POST['Date']);
$uname = trim($uname);
$email = trim($email);
$upass = trim($upass);
// email exist or not
$query = "SELECT Date FROM status WHERE Date='$Date'";
$result = mysql_query($query);
$count = mysql_num_rows($result); // if email not found then register
if($count == 0){
if(mysql_query("INSERT INTO status(PersonID,FirstName,Status, Date) VALUES('$PersonID','$FirstName','$Status','$Date')"))
{
?>
<script>alert('successfully Added ');</script>
<?php
}
else
{
?>
<script>alert('error while registering you...');</script>
<?php
}
}
else{
?>
<script>alert('Sorry Attendence already taken ...');</script>
<?php
}
}
echo "<!DOCTYPE html>\n";
echo "<html>\n";
echo "<head>\n";
echo " <meta charset=\"utf-8\">\n";
echo " <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n";
echo " <title>Attendance</title>\n";
echo "</head>\n";
echo "<body>\n";
echo " \n";
echo "\n";
echo "\n";
include_once 'dbconnect.php';
$query = "SELECT * FROM attandance ";
$result = mysql_query($query);
echo "<h2>Mark Attendance</h2>\n";
echo "<form method=\"POST\" action=\"\">\n";
echo "<table border=\"2\">\n";
echo " <tr>\n";
echo " <th>PersonId</th>\n";
echo " <th>First Name</th>\n";
echo " <th>Status</th>\n";
echo " <th>Date</th>\n";
echo " </tr>\n";
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$PersonID=$row['PersonID'];
$FirstName=$row['FirstName'];
echo "<td> <input type=\"text\" name=\"PersonID\" value=\" $PersonID\"></td>\n";
echo "<td> <input type=\"text\" name=\"FirstName\" value=\" $FirstName\"></td>\n";
echo "<td><select name=\"Status\">\n";
echo " <option value=\"Present\">Present</option>\n";
echo " <option value=\"Absent\">Absent</option>\n";
echo " </select></td>\n";
echo "<td> <input type=\"date\" name=\"Date\" value=\" $Date\"></td>\n";
echo "</tr>\n";
}
echo " </table>\n";
echo "<input id=\"button\" type=\"submit\" name=\"btn-signup\" value=\"Mark\">\n";
echo "</form>\n";
echo "\n";
echo "\n";
echo "<a href='sheet.php'>Attendence Sheet</a>\n";
echo "</body>\n";
echo "</html>\n";
echo "\n";
?>
I am confused as i have used loop in the 2nd part to get value from the database and now i want to use loop to add those value in the database table. How can i add the code to the Insert SQL query in the first part of my code.

How can I check value to be inserted in mysql database

Please I'm a newbie in php and mysql I want you to help me on how to check if value to be inserted into a database is greater than or lesser than, if greater than it should insert pass, thanks.
this the code
$query="select * from ats_question";
$rs=mysql_query("select * from ats_question where test_id=$tid",$cn) or die(mysql_error());
if(!isset($_SESSION[qn]))
{
$_SESSION[qn]=0;
mysql_query("delete from ats_useranswer where sess_id='" . session_id() ."'") or die(mysql_error());
$_SESSION[trueans]=0;
}
else
{
if($submit1=='Next Question' && isset($ans))
{
mysql_data_seek($rs,$_SESSION[qn]);
$row= mysql_fetch_row($rs);
mysql_query("insert into ats_useranswer(sess_id, test_id, que_des, ans1,ans2,ans3,ans4,
true_ans,your_ans)values('".session_id()."', $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or die(mysql_error());
if($ans==$row[7])
{
$_SESSION[trueans]=$_SESSION[trueans]+1;
}
$_SESSION[qn]=$_SESSION[qn]+1;
}
else if($submit=='Get Result' && isset($ans))
{
mysql_data_seek($rs,$_SESSION[qn]);
$row= mysql_fetch_row($rs);
mysql_query("insert into ats_useranswer(sess_id, test_id, que_des, ans1,ans2,ans3,ans4,true_ans,
your_ans) values
('".session_id()."', $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or die(mysql_error());
if($ans==$row[7])
{
$_SESSION[trueans]=$_SESSION[trueans]+1;
}
echo "<h1 class=head1> Result</h1>";
$_SESSION[qn]=$_SESSION[qn]+1;
echo "<Table align=center><tr class=tot><td>Total Question<td> $_SESSION[qn]";
echo "<tr class=tans><td>True Answer<td>".$_SESSION[trueans];
$w=$_SESSION[qn]-$_SESSION[trueans];
echo "<tr class=fans><td>Wrong Answer<td> ". $w;
echo "</table>";
mysql_query("insert into ats_result(login,test_id,grade,score) values
('$login',$tid,grade,$_SESSION[trueans])")
or die(mysql_error());
echo "<h1 align=center><a href=ats_review.php> Review Question</a> </h1>";
unset($_SESSION[qn]);
unset($_SESSION[sid]);
unset($_SESSION[tid]);
unset($_SESSION[trueans]);
exit;
}
}
$rs=mysql_query("select * from ats_question where test_id=$tid",$cn) or die(mysql_error());
if($_SESSION[qn]>mysql_num_rows($rs)-1)
{
unset($_SESSION[qn]);
echo "<h1 class=head1>Some Error Occured</h1>";
session_destroy();
echo "Please <a href=index.php> Start Again</a>";
exit;
}
mysql_data_seek($rs,$_SESSION[qn]);
$row= mysql_fetch_row($rs);
echo "<div align=\"center\" class=\"style2\"><strong> Ats Examination </strong></div>";
echo "<div id=\"\"></div>";
echo"<p id=\"note\"></p>";
echo "<form name=myfm id=myfm1 method=post action=ats_exam.php>";
echo "<table width=100%> <tr> <td width=30> <td> <table border=0>";
$n=$_SESSION[qn]+1;
echo "<tR><td><span class=style2>Que ". $n .": $row[2]</style>";
echo "<tr><td class=style6><input type=radio name=ans value=1>$row[3]";
echo "<tr><td class=style6> <input type=radio name=ans value=2>$row[4]";
echo "<tr><td class=style6><input type=radio name=ans value=3>$row[5]";
echo "<tr><td class=style6><input type=radio name=ans value=4>$row[6]";
if($_SESSION[qn]<mysql_num_rows($rs)-1)
echo "<tr><td><input type=submit name=submit1 value='Next Question'> </form>";
echo "<tr><td><input type=submit name=submit value='Get Result'></form>";
echo "</table></table>";
?>
i want to check if the trueans is greater than 15 it should insert pass if not fail in the grade value
mysql_query("insert into ats_result(login,test_id,grade,score) values
('$login',$tid,grade,$_SESSION[trueans])")
Thanks I have finally get solution to my question this what my code now look like mysql_query("insert into ats_result(login,test_id,score,grade)values('$login','$tid',$_SESSION[trueans],IF($_SESSION[trueans]>=10,'Pass','Fail')"); thanks all for the response and challenges throw to me.

Unable to store date to database in php

<!DOCTYPE html>
<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/
jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js">
</script>
<script>
$(document).ready(function() {
$(".datepicker").datepicker()
});
</script>
</head>
<body>
<?php
$status = $_POST['status'];
$driver_name= $_POST['driver_name'];
$from=date('y-m-d',strtotime($_POST['date_from']));
$to=date('y-m-d',strtotime($_POST['date_to']));
$conn = mysqli_connect('localhost', 'root', '', 'punbus') or
die("Database not connected" . mysqli_error());
if(isset($_POST['sub'])) {
foreach($status as $k=>$s){
$ins = "insert into driver_status(driver_name,status,date_from,date_to)
VALUES
('".$driver_name[$k]."','$s','".$from[$k]."','".$to[$k]."')";
$quer=mysqli_query($conn,$ins);
}
if($quer){
echo "Updated";
}else{
echo"NOT".mysqli_error($conn);
}
}
$sel = 'select Driver_name from driver_master';
$query = mysqli_query($conn, $sel);
echo "<form action='driver_status.php' method='post'>";
echo "<table cellpadding=5>";
echo "<tr>";
echo "<th>Driver Name</th>";
echo "<th>Status</th>";
echo "<th>From</th>";
echo "<th>To</th>";
echo "</tr>";
while($row=mysqli_fetch_assoc($query)){
echo "<tr><td>".$row['Driver_name']
."<input type=\"hidden\" name=\"driver_name[]\"
value=\"".$row['Driver_name']."\"/></td>";
$sel1='select d_status from status';
$query1=mysqli_query($conn,$sel1);
echo "<td><select name=\"status[]\">";
while($row1=mysqli_fetch_assoc($query1)){
echo "<option value=\"".$row1['d_status']."\">".$row1['d_status']."
</option>";
}
echo "</select></td>";
echo "<td>".'<input type="text" name="date_from[]" class="datepicker">'."</td>";
echo "<td>".'<input type="text" name="date_to[]" class="datepicker">'."</td>";
echo "</tr>";
}
echo "</table>";
echo '<input type="submit" name="sub" value="Update"/>';
echo "</form>";
?>
</body>
</html>
I want to store date to database from jquery datepicker. above is my code and error is Warning: strtotime() expects parameter 1 to be string, array given. Can i store date to database as array.Plz help.
Change Input name to :-
echo "<td>".'<input type="text" name="date_from" class="datepicker">'."</td>"; // remove []
echo "<td>".'<input type="text" name="date_to" class="datepicker">'."</td>"; // remove []
Use Dateformat like this
Try: date("Y-m-d") which uses the numeric equivalents.
#vove This is code-
<!DOCTYPE html>
<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/
jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js">
</script>
<script>
$(document).ready(function() {
$(".datepicker").datepicker()
});
</script>
</head>
<body>
<?php
$status = $_POST['status'];
$driver_name= $_POST['driver_name'];
$from=date('y-m-d',strtotime($_POST['date_from']));
$to=date('y-m-d',strtotime($_POST['date_to']));
$conn = mysqli_connect('localhost', 'root', '', 'punbus') or
die("Database not connected" . mysqli_error());
if(isset($_POST['sub'])) {
$ins = "insert into driver_status(driver_name,status,date_from,date_to)
VALUES
('$driver_name','$s','$from','$to')";
$quer=mysqli_query($conn,$ins);
}
if($quer){
echo "Updated";
}else{
echo"NOT".mysqli_error($conn);
}
$sel = 'select Driver_name from driver_master';
$query = mysqli_query($conn, $sel);
echo "<form action='driver_status.php' method='post'>";
echo "<table cellpadding=5>";
echo "<tr>";
echo "<th>Driver Name</th>";
echo "<th>Status</th>";
echo "<th>From</th>";
echo "<th>To</th>";
echo "</tr>";
while($row=mysqli_fetch_assoc($query)){
echo "<tr><td>".$row['Driver_name']
."<input type=\"hidden\" name=\"driver_name\"
value=\"".$row['Driver_name']."\"/></td>";
$sel1='select d_status from status';
$query1=mysqli_query($conn,$sel1);
echo "<td><select name=\"status\">";
while($row1=mysqli_fetch_assoc($query1)){
echo "<option value=\"".$row1['d_status']."\">".$row1['d_status']."
</option>";
}
echo "</select></td>";
echo "<td>".'<input type="text" name="date_from" class="datepicker">'."</td>";
echo "<td>".'<input type="text" name="date_to" class="datepicker">'."</td>";
echo "</tr>";
}
echo "</table>";
echo '<input type="submit" name="sub" value="Update"/>';
echo "</form>";
?>

submit button will not show the result by one click it will show it by second click

When I click the update button it will not update the table but when i Click it again it will be updated how can I correct it?
<html>
<head>
<title>pharmacy</title>
<form method="post">
<?php
session_start();
$connection=Mysql_connect('localhost','admin','123');
Mysql_select_db('db',$connection);
$pname=$_SESSION['s1'];
$query="select * from request where ph='$pname'";
if(array_key_exists('rqlist',$_POST))
{
if(!$connection)
{
echo 'connection is invalid';
}
else
{
$result=mysql_query($query);
if($result )
{
$num=Mysql_num_rows($result);
$num1=Mysql_num_fields($result);
echo $num;
if($num>0)
{
echo "<table border=2> Table Request".$_SESSION['s1'];
echo"<tr>
<td>Id</td><td>Drug</td><td>Quantity</td><td>Doctor</td><td> </td><td>pharmacy</td><td>name</td><td>lastname</td><td>Situation</td>
</tr>";
for($i=0;$i<$num;$i++)
{
$row=mysql_fetch_row($result);
echo "<tr>";
for($j=0;$j<$num1;$j++)
{
echo"<td>$row[$j]</td>";
}
echo"<td><input type='Checkbox' name='p[$i]' value='on' unchecked /> </td>";
echo"</tr>";
}
}
}
}//else
}//array
if(array_key_exists('update',$_POST)){
$result=mysql_query($query);
$qut="select qun from $pname";
$rt=mysql_query($qut);
if($result && $rt)
{
$num=Mysql_num_rows($result);
$num1=Mysql_num_fields($result);
//num2=Mysql_num_rows($rt);
if($num>0)
{
echo "<table border=2> Table Request".$_SESSION['s1'];
echo"<tr>
<td>Id</td><td>Drug</td><td>Quantity</td><td>Doctor</td><td></td><td>pharmacy</td><td></td><td>Situation</td>
</tr>";
for($i=0;$i<$num;$i++)
{
$row=mysql_fetch_row($result);
$row2=mysql_fetch_row($rt);
$dr[$i]=$row[1];
$qu[$i]=$row[2];
$st[$i]=$row[7];
$qun[$i]=$row2[0];
$query8="select * from $pname where name='$dr[$i]' and qun>$qu[$i] ";
$result8=mysql_query($query8);
$num8=Mysql_num_rows($result8);
if($num8!=0)
{
if($row[5]!='ready' && $row[5]!='transfer from $pname')
{
$query9="update request set situation='ready' where drug='$dr[$i]' ";
$qu="update $pname set qun=$qun[$i]-$qu[$i] where name='$dr[$i]' ";
$result9=mysql_query($query9);
$resultqu=mysql_query($qu);
echo $qu;
}
}
else{
if($row[5]!='ready'&&$row[5]!='transfer from $pname')
{
$query10="update request set situation='transfer from $pname' where drug='$dr[$i]' ";
$result10=mysql_query($query10);
}
}
echo "<tr>";
for($j=0;$j<$num1;$j++)
{
echo"<td>$row[$j]</td>";
}
echo"<td><input type='Checkbox' name='p[$i]' value='on' unchecked /></td>";
echo"</tr>";
}//for$i
echo"</table>";
}//if num
}
}
?>
<input type="submit" name="rqlist" value="Request list"/>
<input type="submit" name="update" value="Update the list"/>
</form>
</head>
</html>
Try taking out the request list button to see it if anything changes

Categories