Onclick funcs is not working properly in my function - php

I have written the function for updating my status from 0 to 1 but when I am clicking on button its updating the status of all the rows. I don't know what I did wrong.
function updateStatusenable($id,$value)
{
global $mysqli,$db_table_prefix;
$stmt = $mysqli->prepare("UPDATE gb_users
SET status = ?
WHERE
id = ?
LIMIT 1
");
$stmt->bind_param("ss", $id,$value);
$result = $stmt->execute();
$stmt->close();
return $result;
}
And this function I used in:
<?php
$value=1;
foreach($users as $v1)
{
echo "<tr>
<form action=\"admin_pending_approval_users.php\" method=\"post\">
<input type=\"hidden\" name=\"enable\" value=".$v1['id'].">
<td>".$v1['id']."</td>
<td>".getAccountTypeNameById($v1['account_type'])."</td>
<td>".$v1['name']."</td>
<td>".$v1['user_name']."</td>
<td>".getUserGroupsNameById($v1['group'])."</td>
<td>Rs.".fetchUserBalance($v1['id'])."</td>
<td>";
if($v1['status']==1)
{
echo "<button class=\"green tiny\"><span>ACTIVE</span></button>";
}
else
{
echo "<button class=\"orange tiny\" onclick=".updateStatusenable($v1['id'],$value)." ><span>DISABLED</span></button>";
}
echo "</td>
<td align='right'>
<img src=\"images/icons/small/white/magnifying_glass.png\"></button>
<img src=\"images/icons/small/white/create_write.png\"></button>
</td>
</form>
</tr>";
}

I think your binding order is wrong when you set it like below;
$stmt->bind_param("ss", $id,$value);
instead use
$stmt->bind_param("ss",$value, $id);
as u set status before id
Also use ajax to update so you can send requests for your actions from client to server.

As i dont want to use Ajax I have done some changes like and make it done in PHP.
<?php
$value=1;
foreach($users as $v1)
{
echo "<tr>
<form action=\"admin_pending_approval_users.php\" method=\"post\">
<input type=\"hidden\" name=\"id\" value=".$v1['id'].">
<td>".$v1['id']."</td>
<td>".getAccountTypeNameById($v1['account_type'])."</td>
<td>".$v1['name']."</td>
<td>".$v1['user_name']."</td>
<td>".getUserGroupsNameById($v1['group'])."</td>
<td>Rs.".fetchUserBalance($v1['id'])."</td>
<input type=\"hidden\" name=\"process\" value=\"1\" required>
<td>";
if($v1['status']==1)
{
echo "<button class=\"green tiny\"><span>ACTIVE</span></button>";
}
else
{
echo "<button class=\"orange tiny\" onClick=\"form.submit()\" ><span>DISABLED</span></button>";
}
echo "</td>
<td align='right'>
<img src=\"images/icons/small/white/magnifying_glass.png\"></button>
<img src=\"images/icons/small/white/create_write.png\"></button>
</td>
</form>
</tr>";
}
?>
And in the same php page i added one function as
if(!empty($_POST['process']))
{
$id = $_POST['id'];
updateStatusenable($id,1);
}
And in the end the funcs its remain same

Related

Refuses to get data

I am using this code to get the value of checked box in PHP but it doesn't work in the second code. The first one is just a list for testing. The second one will echo data from a database:
When applying the first code on the test list I will get the value of the box I checked but when applying it on the second part (one that gets data from database it returns Empty)
Get the checked boxes:
<?php
if(isset($_POST['DeleteCon']) )
{
if(!empty($_POST['lang']))
{
foreach($_POST['lang'] as $value)
{
echo "value : ".$value.'<br/>';
}
}
else
{
echo "value : Empty <br/>";
}
}
?>
Test list: it works on this section of the code:
<form method="post">
<?php
echo "<span>Select languages</span><br/>
<input type='checkbox' name='lang[]' value='PHP' class='table-row'> PHP <br/>
<input type='checkbox' name='lang[]' value='JavaScript'> JavaScript <br/>
<input type='checkbox' name='lang[]' value='jQuery'> jQuery <br/>
<input type='checkbox' name='lang[]' value='Angular JS'> Angular JS <br/>"
?>
</form>
But doesn't work on this one:
<form method="post">
<?php
include('database.php');
$sql = "SELECT id, ContactID ,FirstName, LastName, Phone FROM Contact WHERE ID='1'";
$result = $conn->query($sql);
if ( !empty($result->num_rows) && $result->num_rows > 0) { // ...
// output data of each row
echo "<form method='post'>";
while($row = $result->fetch_assoc()) {
echo "<tr>
<td id='delete'>
<input type='checkbox' name='lang[]' value='PHP' class='table-row'>
</td>
<td>". $row["FirstName"]. "</td>
<td>". $row["LastName"]. "</td>
<td>". $row["Phone"] ."</td></tr>";
}
echo "</form>";
} else {
echo "<tr>
<td id='delete'>
<input type='checkbox' id='row1' class='table-row'>
</td>
<td> 0 results </tr>";
}
$conn->close();
?>
</form>

Displaying data from other table in php while loop

I am trying to display options in select tag from another table while my
first, while loop continues, this is what I have tried so far !!
<?php
$query="SELECT * FROM `customerdata` WHERE takenby='$_SESSION[username]'";
$query_two="SELECT * FROM `vendordriver` WHERE vendoremail='$_SESSION[username]'";
$run_two=$db->query($query_two);
$run=$db->query($query);
while ($row=$run->fetch_assoc()) {
echo " <tr><td>$row[bookingid]</td>
<td>$row[drivername]</td>
<td>$row[cabtype]</td>
<td>$row[carnumber]</td><td><select>";
while ($row_two=$run_two->fetch_assoc()) {
echo "<option>$row_two[drivername]</option>";
}
echo" </select></td>
<td><input type='submit' class='btn btn-success' value='SEND '>
</td>";
}
?>
Any Suggestions Please !!!
You can try this code
<?php
$table_1 = null;
$username = $_SESSION['username'];
$query="SELECT * FROM customerdata WHERE takenby='$username'";
$run=$db->query($query);
if ($result->num_rows > 0) {
while ($row=$run->fetch_assoc()) {
$table_1 = "<tr><td>$row[bookingid]</td>";
$table_1 .= "<td>$row[drivername]</td>";
$table_1 .= "<td>$row[cabtype]</td>";
$table_1 .= "<td>$row[carnumber]</td><td><select>";
echo $table_1;
$query_two="SELECT * FROM vendordriver WHERE vendoremail='$username'";
$run_two=$db->query($query_two);
while ($row_two=$run_two->fetch_assoc()) {
echo "<option>$row_two[drivername]</option>";
}
echo" </select></td>
<td><input type='submit' class='btn btn-success' value='SEND '>
</td>";
}
}
?>
Define the result from the second query as an array variable then with in the first query loop with in that array to echo the drivers names here you execute second query before the first one
$query="SELECT * FROM `customerdata` WHERE takenby='$_SESSION[username]'";
$query_two="SELECT * FROM `vendordriver` WHERE vendoremail='$_SESSION[username]'";
$if(!isset($array_option)){$array_option=array();}
$run_two=$db->query($query_two);
$run=$db->query($query);
while ($row_two=$run_two->fetch_assoc()) {
array_push($array_option,$row_two[driver_name];
}
Then within looping through first query do this
while ($row=$run->fetch_assoc()) {
echo " <tr><td>$row[bookingid]</td>
<td>$row[drivername]</td>
<td>$row[cabtype]</td>
<td>$row[carnumber]</td><td><select>";
for($i=0;$i<count($array_option);$i++){
echo "<option>$array_option[$i]</option>"
}
Then the rest of the code
echo" </select></td><td><input type='submit' class='btn btn-success' value='SEND '></td>";
}
?>

Updating checkbox to the server and disable it

I have a question regarding my project.
I need to make a database update section which pulls down from the server and showing all the values then update it to the database.
My approach is, getting the value and then display it as a check box. When user select one of the checkboxes, it will change the character from N to Y and then disable it button so people cannot make further update. I can successfully implemented the pulldown and getting the info from the server. but i dont know how to implement the checkbox process, update it to the server and set disable on it.
Please help me
This is the JS section on update_fab.php
<script>
// ===================================== //
// ===================================== //
$(function(){
// SHOW RECORD
$('#show').click(function () {
$.post('data2.php', {
action: "show",
"hm": $('#headmark').val()
}, function(res) {
$('#result').html(res);
});
});
});
</script>
<?php
$result = oci_parse($conn, 'SELECT HEAD_MARK FROM FABRICATION');
oci_execute($result);
echo '<div class = "wrapper">';
echo '<div id = "contact_form">';
echo '<span>Head mark</span><label><SELECT name="headmark" id="headmark">'.'<br>';
echo '<OPTION VALUE=" ">'."".'</OPTION>';
while ($row = oci_fetch_array($result, OCI_ASSOC)) {
$HM = $row ['HEAD_MARK'];
echo "<OPTION VALUE='$HM'>$HM</OPTION>";
}
echo '</SELECT></label><br />';
?>
<input class ="showButton" type="button" value="show" name="SHOW RECORD" id="show"/>
and on the data2.php
// IF SHOW KEY HAS BEEN PRESSED
if ($_POST['action'] == 'show') {
$sql = "SELECT * FROM SUB_MASTER_DRAWING
WHERE SUB_MASTER_DRAWING.HEAD_MARK = '{$_POST["hm"]}'";
$query = oci_parse($conn, $sql);
$query_exec = oci_execute($query);
echo "<table border='1'>";
while ($row = oci_fetch_assoc($query)) {
echo '<div id="content">';
echo '<table cellspacing = "0"';
echo '<tr><th>Head Mark</th>
<th>Cutting</th>
<th>Assembly</th>
<th>Welding</th>
<th>Drilling</th>
<th>Finishing</th>
<th>Update</tr>';
echo "<tr><td><b>$row[HEAD_MARK]/$row[ID]</b></td>
<td><input type='checkbox' name='check'/></td>
<td><input type='checkbox' name='check'/></td>
<td><input type='checkbox' name='check'/></td>
<td><input type='checkbox' name='check'/></td>
<td><input type='checkbox' name='check'/></td>
<td><input type='button' value='UPDATE' name='updatefab'/></td>
</tr>";
if (isset($_POST['updatefab'])) {
echo '<script type = "text/javascript">submit pressed !</script>';
}
echo '<table cellspacing = "0"';
echo '</div>';
}
echo "</table>";
}

Deleting the last record instead of selected one and how can I add edit function to it

I'm fetching data from database table into page when I'm trying to delete a particular record instead its deleting the last record in table.
This is fetchtable.php. How can i add edit functionality in it? Please if possible give a detailed answer:
<?php
require 'conn.php';
$tfetch = "select ID,firstname,lastname,gender,email, password from signup";
if ($stmt = mysqli_prepare ($conn,$tfetch))
{
mysqli_stmt_execute ($stmt);
mysqli_stmt_bind_result ($stmt, $id, $fn, $ln, $gen, $email, $pass );
mysqli_stmt_store_result ($stmt);
}
mysqli_close ($conn);
?>
And this is the page in which I'm fetching:
<?php
include'fetchtable.php';
if(isset($_POST['del']))
{
require 'conn.php';
$id=$_POST['del_id'];
$stmt = "DELETE FROM signup WHERE ID =$id";
mysqli_query($conn,$stmt);
mysqli_execute($stmt);
$row=mysqli_affected_rows($conn);
if($row==1)
{
echo " sucess ! record was deleted ";
}
else
{
echo " record was not deleted ";
}
mysqli_close($conn);
}
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<?php
echo "<table border='1' cellpadding='2' cellspacing='2'";
echo "<tr><td>ID</td><td>First Name</td><td>Last Name</td><td>Gender</td><td>Email</td><td>Password</td><td>Delete</td>";
while (mysqli_stmt_fetch($stmt))
{
echo"<tr>";
echo "<td>".$id."</td>";
echo "<td>". "$fn" ."</td>";
echo "<td>". "$ln" ."</td>";
echo "<td>". "$gen"."</td>";
echo "<td>". "$email"."</td>";
echo "<td>". "$pass" ."</td>";
echo '<td> <input type="hidden" name="del_id" value="'.$id.'" /> <input type="submit" name="del" value="delete" /> </td>';
echo"</tr>";
}
?>
</form>
EDIT : Found solution just include the while loop in form rather than including while within a form
It's deleting the last record b/c you variable $id is same in both fetch and delete.
$delete_id = $_POST['del_id'];
$del_stmt = "DELETE FROM signup WHERE ID =$delete_id";
mysqli_query($conn,$del_stmt);
mysqli_execute($del_stmt);

how to show the correct result of the quiz

I m trying to make a quiz.It's working but not giving the right result. On a correct answer for example answer 1 variable rans should be incremented by one but it is incrementing after submitting the 2nd question, that's why the value of the 10th current answer is not including in the total correct answer.
<?php
require_once("global.inc.php");?>
<form name="test" method="post" action="test.php">
<?php
$qid=(!isset($_POST['q_id'])) ? 0 : $_POST['q_id'];
$rans=(!isset($_POST["rans"])) ? 0 : $_POST["rans"];
$totalquestion=(!isset($_POST["totalquestion"])) ?
0 : $_POST["totalquestion"];
echo $rans;
if(isset($_POST["submit"]))
{
echo "<table align='center' style='border:1px solid silver' width='80%'
bgcolor='green'>";
echo "<tr><td>Total Question Attempt</td><td>",$totalquestion,"</td><tr>";
echo "<tr><td>Correct Answer</td><td>",$rans,"</td></tr>";
echo "<tr><td>Wrong Answer</td><td>",$totalquestion-$rans,"</td></tr>";
echo "<tr><td>Correct Answer Percentage</td> <td>",$rans/$totalquestion*100,"%</td></tr>";
echo "<tr><td>Wrong Answer Percenntage</td><td>",($totalquestion-$rans)/$totalquestion*100,"%</td></tr>";
echo "</table><br><br>";
$query="select * from questions,answers
where questions.q_id=answers.q_id";
echo "<table cellpadding='5px' align='center' style='border:1px
solid silver'>";
echo "<tr><th colspan='4' id='heading'>Online Quiz Test
Question</td></tr>";
$result=mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo "<tr><td>",$row['q_id'],"</td><td colspan='2'>",$row['question'],"</td></tr><tr><td></td>";
echo "<td colspan='2'>A. ",$row['opt1'],"</td>";
echo "<td colspan='2'>B. ",$row['opt2'],"</td></tr>";
echo "<tr><td></td><td colspan='2'>C. ",$row['opt3'],"</td>";
echo "<td colspan='1'>D. ",$row['opt4'],"</td></tr>";
echo "<tr><td colspan='4' align='right'
style='color:orange'>Correct option is ",strtoupper($row['correct_ans']),"</td></tr>";
echo "<tr><td colspan='4' align='right'
style='color:orange'><hr></td></tr>";
}
echo "</table>";
echo "<p align='right'><a href='#' onclick='window.print()'>Print</a></p>";
echo "<div style='visibility:hidden;display:none'>";
}
?>
<form name="test" method="post" action="test.php">
<?php
if(!isset($a))
{
$a=0;
//unset($_SESSION['score']);
}
if(isset($_POST['next'])) {
$a=$_POST['a'];
$totalquestion=$_POST['totalquestion'];
if(isset($_POST['rans']))
$rans=$_POST['rans'];
}
$sql1="SELECT * FROM questions,answers
where questions.q_id=answers.q_id limit 1 offset $a";
$result=mysql_query($sql1);
$num = mysql_num_rows($result);
echo "<form method='post' action=''>";
if($result) {
while ($row = mysql_fetch_array($result))
{
$qid = $row["q_id"];
$questions = $row["question"];
$opt1 = $row["opt1"];
$opt2 = $row["opt2"];
$opt3 = $row["opt3"];
$opt4 = $row["opt4"];
$correct = $row["correct_ans"];
echo $rans;
?>
<p >Q.<?php echo $qid ?> <?php echo $questions;?></p>
<input type="radio" value="<?php echo $opt1;?>" name="choice"/><?php echo $opt1;?> <br/>
<input type="radio" value="<?php echo $opt2;?>" name="choice"/><?php echo $opt2;?><br/>
<input type="radio" value="<?php echo $opt3;?>" name="choice"/><?php echo $opt3;?><br/>
<input type="radio" value="<?php echo $opt4;?>" name="choice"/><?php echo $opt4;?><br/>
<input type="hidden" value="$answer" name="rightanswer[$qid]"/>
<?php
$b=$a+1;
$sql2="SELECT * FROM questions where q_id=$qid-1 ";
$result2=mysql_query($sql2);
while ($row2 = mysql_fetch_array($result2)) {
$ans=$row2['correct_ans'];
}
if(isset($_POST['choice'])) {
if($ans==$_POST['choice']){
//echo "<input type='hidden' name='rans' value='".($rans+1). "'>";
$rans=$rans+1;
}
else {
//echo "<input type='hidden' name='rans' value='" . $rans . "'>";
$rans=$rans;
}
}
//$query="select correct_ans from questions where q_id='$qid'";
//$result=mysql_query($query);
//while ($row = mysql_fetch_array($result)) {
//echo $row['correct_ans'];
echo "<input type='hidden' value='$b' name='a'>";
echo "<input type='hidden' value='count' name='count'>";
echo "<input type='hidden' name=qid value='$qid'>";
echo "<input type='hidden' name='totalquestion' value='".$totalquestion+1)."'>";
echo "<input type='hidden' name='rans' value='" . $rans . "'>";
echo "<input type='submit' name='next' value='next'> ";
echo "<input type='submit' name='submit' value='submit'><br><br>";
echo "</form>";
}
}
?>
Okay, your code is a bit of a mess.
You have random tautologies (like the $rans=$rans; which does absolutely nothing. If the answerer clicks "next" you're assigning $totalquestion twice. Definitely take a good, hard look and refactor this page.
But the answer to your question is probably because you're checking to see if they entered in the right answer at the bottom of the code -- after you've presented the results or the next question.
You've utilized the scripting capabilities of PHP without touching on any functions so it will evaluate top to bottom.
I'd move everything around: Move the handler for "next" to the top, underneath your default variable assignments, then put the check for right answer underneath that, then do the presentation of the next question, then the "submit"handler.
I'd break up the different units into functions for readability and reusability, also. For example, make a function to print out the specified question, make another one to validate the user entered in the right answer.

Categories