Update
My Page is not pulling information from the form for some reason it wants to keep all the fields empty even though if it is checked it should change the value of the QuestionOptionId. What i want it to do is when you check the radio button it saves the value to the session so it can be submitted later like a quiz. For some reason my code wont change the value or even obtain the value of QuestionOptionId
Code below
<?php
session_start();
//check if the user is already logged in.
if (!isset($_SESSION['username'])) {
header('Location: login.php');
}
$QuestionOptionId = "";
//get value from post data and store into session
if (isset($_POST['QuestionOptionId'])){
$_SESSION['QuestionOptionId'] = $_POST['QuestionOptionId'];
}
//get back from session
if (isset($_SESSION['QuestionOptionId'])){
$QuestionOptionId = $_SESSION['QuestionOptionId'];
}
print $_SESSION['username'];
?>
<?php
$con=mysqli_connect("localhost","root","root","root") or die();
//execute query
$query ="SELECT UserId from user where username = '".$_SESSION['username']."'";
$result=mysqli_query($con, $query);
$UserId=$result->num_rows;
while ($row = $result->fetch_assoc()) {
echo $row['UserId'];
$UserId=$row['UserId'];//etc...
}
?>
<body>
<header>
<p class="text-center">
Welcome :<?php echo $_SESSION['username'];
echo $UserId;
?>
</p>
<?php
$query="SELECT * from testview";
$res=mysqli_query($con, $query);
$rows=$res->num_rows;
$i=1;
while($result=mysqli_fetch_array($res))
{?>
<?php if($i==1){?>
<div id='question<?php echo $i;?>' class='cont'>
<form name="QuestionTime"Method="post" action="push.php">
<p class='questions' id="qtext<?php echo $i;?>"> <?php echo $i?>.<?php echo $result['QuestionText'];?></p>
<input <?php if ($QuestionOptionId=='3'){ echo 'checked="checked"';} ?>type="radio" value="1" id='radio1_<?php echo $result['QuestionOptionId'];?>'name="1"/>
<?php
$query="SELECT OptionChoiceName FROM testview WHERE OptionChoiceId=3";
$res=mysqli_query($con, $query);
$rows=$res;
$i=1;
while($result=mysqli_fetch_array($res))
echo $result['OptionChoiceName'];?>
<input <?php if ($QuestionOptionId=='4'){ echo 'checked="checked"';} ?> type="radio" value="2"id='radio1_<?php echo $result['QuestionId'];?>'name="1"/>
<?php
$query="SELECT OptionChoiceName FROM testview WHERE OptionChoiceId=4";
$res=mysqli_query($con, $query);
$rows=$res;
$i=1;
while($result=mysqli_fetch_array($res))
echo $result['OptionChoiceName'];
?>
<br><br>Selected Value: <?php echo $QuestionOptionId; ?>
<br/>
<?php
$_SESSION['UserId']=$UserId;
?>
<button id='next<?php echo $i;?>' class='next btn btn-success' type='submit'>Finish</button>
</div>
<?php } $i++;} ?>
</form>
</div>
</div>
</header></header>
any help would be grand thanks
You were not fethcing QuestionOptionId & QuestionId. Try with this query -
SELECT OptionChoiceName, QuestionOptionId, QuestionId FROM testview WHERE OptionChoiceId=3
This should be -
if(isset($_SESSION['QuesitonOptionId']))
{
$QuestionOptionId= $_SESSION['QuesitonOptionId'];
}
Related
My add_id is a primary key. I have displayed all the addresses from the same cus_id but with different add_id. I want to delete a specified row of address but when I press the delete button, the page refresh but no data was deleted. Please look at my codes, thank you.
These are the codes involved, and my db:
<?php
$cus_id = $_SESSION['id'];
//To show all the addresses with the same cus_id
$result2 = mysqli_query($connect, "SELECT * FROM customer_address WHERE cus_id='$cus_id'");
?>
<?php
if (isset($_GET['del'])) {
$add_id = $_GET["id"];
mysqli_query($connect, "DELETE FROM customer_address WHERE add_id='$add_id'");
}
?>
<?php
while ($row1 = mysqli_fetch_assoc($result2)) {
?>
<div class="addrow">
<div class="add_box">
<p id="name_row"><?php echo $row1['name']; ?> </p>
<p id="phone_row"><?php echo $row1['contact']; ?> </p>
<p id="add_row"><?php echo $row1['address']; ?></p>
</div>
<div class="btn_box">
<input type="button" name="editbtn" class="editbtn" value="Edit">
<input type="button" name="deletebtn" class="deletebtn" value="Delete">
<input type="button" name="defaultbtn" class="defaultbtn" value="Set As Default">
</div>
</div>
<?php
}
?>
<?php
if (isset($_GET['del'])) {
$add_id = $_GET["id"];
mysqli_query($connect, "DELETE FROM customer_address WHERE add_id='$add_id'");
echo ("<script>location.href = 'cus_address.php?msg=$msg';</script>");
}
?>
Firstly, you need to change from '$cus_id' to '".$cus_id."' because $cus_id is parameter.
<?php
$cus_id = $_SESSION['id'];
//To show all the addresses with the same cus_id
$sql = "SELECT * FROM customer_address WHERE cus_id='".$cus_id."'";
$result2 = mysqli_query($connect, $sql);
?>
When delete data, you need to add both cust_id and add_id on query follow as below:
<?php
if (isset($_GET['del']))
{
$cust_id = $_GET["id"];
$add_id = $_GET["add_id"];
mysqli_query($conn, "DELETE FROM customer_address WHERE add_id='".$add_id."' and cus_id='".$cust_id."'");
}
?>
Next, check data exist before looping and add "&add_id="
<?php
if(mysqli_num_rows($result2) > 0)
{
while($row1 = mysqli_fetch_assoc($result2))
{
?>
<div class="addrow">
<div class="add_box">
<p id="name_row"><?php echo $row1["name"]; ?> </p>
<p id="phone_row"><?php echo $row1["contact"]; ?> </p>
<p id="add_row"><?php echo $row1["address"]; ?></p>
</div>
<div class="btn_box">
<input type="button" name="editbtn" class="editbtn" value="Edit">
<input type="button" name="deletebtn" class="deletebtn" value="Delete">
<input type="button" name="defaultbtn" class="defaultbtn" value="Set As Default">
</div>
</div>
<?php
}
}else{
//Display... when no data have been found
}
?>
I am doing project for my university. I create a page where user can send friend request. Here I fetch data from another table and put button for each row data.
My problem is that when one button click other row button also was change to friend request. I need a solution for it.
How to make one add friend request button is equal to one row id and how to avoid other button affected whenever click particular row.
My code is included below. I hope you guys will help me. Thanks in advance.
<?php
session_start();
$_SESSION['myid'];
$mysqli=new MySQLi('127.0.0.1','root','','learning_malaysia');
$sql = "SELECT * FROM tutor_register INNER JOIN tutorskill ON tutor_register.register_ID = tutorskill.register_ID ORDER BY
tutor_register.register_ID='".$_SESSION['myid']."'desc";
$result= mysqli_query($mysqli,$sql);
if(mysqli_num_rows($result)>0)
{
while($row = mysqli_fetch_array($result))
{
$register_ID=$row["register_ID"];
$username = $row['username'];
$profile = $row['profile'];
$email = $row['email'];
$address=$row['address'];
$gender=$row['gender'];
$main_subject=$row["main_subject"];
$subject_add=$row["subject_add"];
$rate_main=$row["rate_main"];
$rate_add=$row["rate_add"];
$qualification=$row["qualification"];
?>
<table><form method="post">
<tr class="border_bottom">
<td height="230"><img src='<?php echo $profile;?>'width="200" height="200"/> </td><td><td></td></td>
<?php
if($register_ID == $_SESSION['myid']){
?>
<td><label>Your Profile</label></td>
<?php
} else {
?>
<form method="post">
<td><button class='friendBtn unfriend' name="" data-type="unfriend">Unfriend</button>
<input type="hidden" name="id" value="<?php echo $row['register_ID'];?>" />
<input type="submit" name="addfriend" data-type='addfriend' id="addfriend" value="<?php
if($_SESSION['status'] == 'yes'){
echo 'Request Sent';
}
else {
echo 'Addfriend';}
?>" data-uid=<?php echo $row['register_ID'];?>/></td> </form>
<?php
}
}
?>
</tr>
</div>
</table>
</form>
<?php
if(isset($_POST['id']) ) {
$user_id = $_SESSION['myid'];
$friend_id = $_POST['id'];
$sql="INSERT INTO friends(user_id,status,friend_id)" ."VALUES('$user_id','yes','$friend_id') ";
if($mysqli->query($sql)=== true) {
$_SESSION['status']="yes";
$_SESSION['id']=$row['id'];
} else {}
}
}
?>
</body>
</html>
You need to replace the following block in your code:
<input type="submit" name="addfriend" data-type='addfriend' id="addfriend" value="<?php
if($_SESSION['status'] == 'yes'){
echo 'Request Sent';
}
else {
echo 'Addfriend';}
?>" data-uid=<?php echo $row['register_ID'];?>/>
With the one mentioned below. This will solve your problem.
<input type="submit" name="addfriend" data-type='addfriend' id="addfriend" value="<?php
if($_SESSION['status'] == 'yes' && $row['register_ID']==$_SESSION['id']){
echo 'Request Sent';
}
else {
echo 'Addfriend';}
?>" data-uid=<?php echo $row['register_ID'];?>/>
I have a script that prints in the screen all the data of a table. Associated to each row of data, I have a delete button, and I would like that, when a button of any row is cliked, the row is deleted. To do so, I have got the following code:
$con = mysqli_connect("","","","");
$result = mysqli_query($con,"SELECT * FROM `clientes_pmt`");
while($row = mysqli_fetch_array($result)){
?> <button name="delete" value="<?php echo $row['id']; ?>" type="submit"><img src="paginas/borrar.jpg" /></button>
<a href="page<?php echo $row['nombre']; ?>">
<div><p><?php echo $row['nombre']; ?></p></div>
<div><p><?php echo $row['pais']; ?></p></div>
</a>
<section class="clearboth"></section><br><?php
}
if(isset($_POST['delete'])){
$id = $_POST['delete'];
$result = mysqli_query($con,"DELETE FROM `clientes_pmt` WHERE id = '$id'");
}
mysqli_close($con);
I receive no errors but the row is not being deleted.
Enclose the button inside a form element and set appropriate action and method attributes, something like:
<form action="/delete.php" method="POST">
<button ...> [your button]
</form>
You have to modify these line
$con = mysqli_connect("localhost","root","","YOUR DATABASE NAME");
To see the result you can put a header() to the end of this part of code:
if(isset($_POST['delete'])){
$id = $_POST['delete'];
$result = mysqli_query($con,"DELETE FROM `clientes_pmt` WHERE id = '$id'");
header('location:your_page_where_the_rows_are.php');
}
I think it may be because the $id was not getting added into the query.
Try bellow?
if(isset($_POST['delete'])){ $id = $_POST['delete']; $result = mysqli_query($con,"DELETE FROM clientes_pmt WHERE id = '".$id."'); }
Use Form tag and Header for redirect your page on same page.
Try This -
<?php
$con = mysqli_connect("","","","");
$result = mysqli_query($con,"SELECT * FROM `clientes_pmt`");
while($row = mysqli_fetch_array($result)){
?>
<form action="" method="post" >
<button name="delete" value="<?php echo $row['id']; ?>" type="submit"><img src="paginas/borrar.jpg" /></button>
</form>
<a href="page<?php echo $row['nombre']; ?>">
<div><p><?php echo $row['nombre']; ?></p></div>
<div><p><?php echo $row['pais']; ?></p></div>
</a>
<section class="clearboth"></section><br><?php
}
if(isset($_POST['delete'])){
$id = $_POST['delete'];
$result = mysqli_query($con,"DELETE FROM `clientes_pmt` WHERE id = '$id'");
header('location:'.$_SERVER['PHP_SELF']);
}
mysqli_close($con);
?>
This is kind of the error I'm getting:
Database query failed.
I've uploaded this webpage: http://widgetcorp.bugs3.com/public/edit_subject.php?subject=1
Here's my file:
<?php require_once("../includes/session.php"); ?>
<?php require_once("../includes/db_connection.php"); ?>
<?php require_once("../includes/functions.php"); ?>
<?php require_once("../includes/validation_functions.php"); ?>
<?php find_selected_page(); ?>
<?php
if (!$current_subject)
{
// subject ID was missing or invalid or
// subject couldn't be found in database
redirect_to("manage_content.php");
}
?>
<?php
if (isset($_POST['submit']))
{
// validations
$required_fields = array("menu_name", "position", "visible");
validate_presences($required_fields);
$fields_with_max_lengths = array("menu_name" => 30);
validate_max_lengths($fields_with_max_lengths);
if (empty($errors))
{
// Perform Update
$id = $current_subject["id"];
$menu_name = mysql_prep($_POST["menu_name"]);
$position = (int) $_POST["position"];
$visible = (int) $_POST["visible"];
$query = "UPDATE subjects SET ";
$query .= "menu_name='{$menu_name}', ";
$query .= "position={$position}, ";
$query .= "visible={$visible} ";
$query .= "WHERE id={$id} ";
$query .= "LIMIT 1";
$result = mysqli_query($connection, $query);
if ($result && mysqli_affected_rows($connection) >= 0)
{
// Success
$_SESSION["message"] = "Subject updated.";
redirect_to("manage_content.php");
}
else
{
// Failure
$message = "Subject update failed.";
}
}
}
// else
// {
// // This is probably a GET request
// }
?>
<?php include("../includes/layouts/header.php"); ?>
<div id="main">
<div id="navigation">
<?php
echo navigation($current_subject, $current_page);
?>
</div>
<div id="page">
<?php
// echo message();
// $message is just a variable, doesn't use the SESSION
if(!empty($message))
{
echo "<div class=\"message\">" . htmlentities($message) . "</div>";
}
?>
<?php echo form_errors($errors); ?>
<h2>Edit Subject: <?php echo htmlentities($current_subject["menu_name"]); ?></h2>
<form action="edit_subject.php?subject=<?php echo htmlentities($current_subject["menu_name"]); ?>" method="post">
<p>Menu name:
<input type="text" name="menu_name" value="<?php echo htmlentities($current_subject["menu_name"]); ?>" />
</p>
<p>Position:
<select name="position">
<?php
$subject_set = find_all_subjects();
$subject_count = mysqli_num_rows($subject_set);
for ($count=1; $count <= $subject_count; $count++)
{
echo "<option value=\"{$count}\"";
if ($current_subject["position"] == $count)
{
echo " selected";
}
echo ">{$count}</option>";
}
?>
</select>
</p>
<p>Visible:
<input type="radio" name="visible" value="0" <?php if ($current_subject["visible"] == 0) { echo "checked"; } ?> /> No
<input type="radio" name="visible" value="1" <?php if ($current_subject["visible"] == 1) { echo "checked"; } ?> /> Yes
</p>
<input type="submit" name="submit" value="Edit Subject" />
</form>
<br />
Cancel
Delete Subject
</div>
The problem is somewhere else and not with your UPDATE query actually. If you see the link you posted, you are passing subject parameter with url, whose value is 1 which is integer.
Now when you click submit it's changing the url to http://widgetcorp.bugs3.com/public/edit_subject.php?subject=About%20Widget%20Corp .
Here as you see the subject parameter is not integer but string value name of subject. And that is causing the problem.
You are getting error as it's not retrieving the subject data from database correctly because of wrong id type. You just need to make sure the form is being posted to right url, which would be http://widgetcorp.bugs3.com/public/edit_subject.php?subject=1.
You need to correct the action parameter on the <form> tag for that.
Look for the line below in your code:
<form action="edit_subject.php?subject=<?php echo htmlentities($current_subject["menu_name"]); ?>" method="post">
And change it to
<form action="edit_subject.php?subject=<?php echo htmlentities($current_subject["id"]); ?>" method="post">
If you see, now the form will be submitted to http://widgetcorp.bugs3.com/public/edit_subject.php?subject=1, which is the correct url.
It is simple but I am not getting values of radio buttons for updation when I press submit.
It is giving an error in foreach statement like :Warning: Invalid argument supplied for foreach() Please say how to get the values of radio button after pressing submit button,
here I am updating the status of state .
<?php
include("db.php");
?>
<html>
<body>
<?php
$state=$_POST['state'];
if(isset($_POST['submit']))
{
$result1 = mysql_query("select state,id,status from states ");
while($rr1=mysql_fetch_array($result1))
{
//getting values of radio buttons
$myval=$rr1['id'];
echo $myval;
$val=$_POST['yes.$rr1["id"]'];
echo $val;
$val1=$val.$myval;
$vall=yes.$rr1['id'];
foreach($vall as $values)
{
echo $values;
$update=mysql_query("UPDATE states SET status='". $values."'
WHERE id='$myval' ");
}
}
}
echo "ok";
?>
<form action="" name="form" id="form" method="post" >
<?php
//session_start();
include("db.php");
$result = mysql_query("select state,id,status from states ");
while($info1=mysql_fetch_assoc( $result))
{
echo $info1['city'];
echo "<br>";
/*echo "<br>";
echo "company Name:".$info1['company_name'];
echo "<br>";
echo "salary:".$info1['maxsalary'];
echo "<br>";
//echo $info1['company_name'];*/
?>
<label><?php echo $info1['state']; ?></label>
<input type="radio" <?php if($info1['status']=="yes"){ echo
"checked='checked'"; } ?> name="yes.<?php echo $info1[ 'id']; ?>[]"
value="yes">Yes
<input type="radio" <?php if($info1['status']=="no"){ echo
"checked='checked'"; } ?> name="yes.<?php echo $info1[ 'id']; ?>[]"
value="no">no
<br/>
<?php } ?>
<br />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
I think you need to write
$vall=yes.$rr1['id'];
to
$vall="yes".$rr1['id'];
Thanks