Complications with carrying variables through forms - php

I have a database created in phpmyadmin that I have synced up to a php page, which is meant to show some information, but not all, unless you click a button which takes you to a more in depth description of one row of the table.
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo "<tr>\n";
echo "<td>".$row["VAR"]."</td>\n";
echo "<td>".$row["VAR2"]."</td>\n";
echo "<td>".$row["VAR3"]."</td>\n";
$more = '<td><form role = "form" class = "form-inline" action =
"page2.php" method = "get"><input type = "submit" value = "More">
</form></td>';
echo $more;
echo "</tr>\n";
}
}
The while loop ensures that the "More" button appears on each row of the table, as instructed. However, when I try and pass the variable through to page2.php, it gives me an undefined index error. The main goal is just to query the data so only the row in which the button was pressed is shown on page2.php.
$sql = "SELECT * FROM `table` WHERE `VAR` = '$getVAR'";
$result = $conn->query($sql);
I've tried numerous $_GET commands to try and get the specific VAR to the next page but nothing seems to work. Does anyone have any ideas? TIA

You have some issues on the FORM related to the information you send: mainly you miss to pass the value of the row for which you want to see more:
$more = '<td><form class = "form-inline" action = "page2.php" method = "get">
<input type="hidden" name="id_value" value="'.$row['VAR'].'">
<input type = "submit" value = "More">
</form></td>';
Change the $row variable to the one you want to pass as parameter for the query (it's unclear from your question)

Set the value of the value of the submit button to $row["VAR"], add a name attribute. Try this:
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo "<tr>\n";
echo "<td>".$row["VAR"]."</td>\n";
echo "<td>".$row["VAR2"]."</td>\n";
echo "<td>".$row["VAR3"]."</td>\n";
$more = '<td><form role = "form" class = "form-inline" action =
"page2.php" method = "get"><input name="VAR" type = "submit" value = "'.$row["VAR"].'">
</form></td>';
echo $more;
echo "</tr>\n";
}
}
Then you add this for the detail page:
$getVAR = $_GET["VAR"];
$sql = "SELECT * FROM `table` WHERE `VAR` = '$getVAR'";
$result = $conn->query($sql);

Related

Quiz app score resetting

Hi here my processing file for my quizzer application. My code is realizing the answer to every question, however everytime I click the right answer and it sets the score to one. It goes to the next question, and then its resetting the session score to 0, and then adding one. The result is if I have a score of 3, I actually get a score of one. Where do i put the score variable so it doesent keep resetting everytime i process? If i echo $_SESSION['score']; at the end, I always get 0 or 1.
$_SESSION['score'] = 0;
if (isset($_POST['submit']))
{
$number = $_POST['number'];
$selected_choice = $_POST['choice'];
$next = $number + 1;
// get total questions
$query = "SELECT * FROM questions";
// get result
$results = mysqli_query($connection,$query);
$total = mysqli_num_rows($results);
// query to get right answer
$query = "SELECT * FROM choices WHERE question_number = $number AND is_correct = 1";
$result = mysqli_query($connection,$query);
$row = mysqli_fetch_assoc($result);
// set correct choice
$correct_choice = $row['id'];
// Compare
if($correct_choice == $selected_choice)
{
// answer is correct
$_SESSION['score']++;
}
// check is last question
if($number == $total)
{
header("Location:final.php");
exit();
} else
{
header("Location:question.php?n=$next");
}
}
which processes the question.php
<ul class = "choices">
<?php
while($row = mysqli_fetch_assoc($choices)): ?>
<li><input name = "choice" type = "radio" value = "<?php echo $row['id']; ?>" /> <?php echo $row['text']; ?></li>
<?php endwhile ?>
</ul>
<input type = "submit" name = "submit" value = "submit">
<input type = "hidden" name = "number" value = "<?php echo $number ?>" >
</form>
You are just saying yourself, that you reset the score to 0 on a new page load.
$_SESSION['score'] = 0;
And then you either do nothing to it or add 1 - so how could that score be anything different than 0 or 1? Stop resetting the score.
You're setting the value to 0 every time you load the page:
$_SESSION['score'] = 0;
Instead, check if the value exists before setting it. Perhaps something as simple as:
if (!isset($_SESSION['score'])) {
$_SESSION['score'] = 0;
}
That way whenever the page loads it will set the value only if it hasn't already been set.

PHP MYSQLi: Admin edit user level

I have a users.php where admin can see all registered users information and he is able to change their levels with a form that goes to edit_level.php.
It's not working properly, when I change the level of a user, it is affecting the wrong user.
This is my form in users.php (I didn’t include all the user info , just the level part)
$sql = "SELECT * FROM users ORDER BY username ASC";
$result = mysqli_query($conn,$sql) or die(mysqli_error());
if(mysqli_num_rows($result) > 0){
while ($row = mysqli_fetch_assoc($result)){
$level = $row['level'];
<form method='post' action='edit_level.php?ed=$id'>
<input type='hidden' name='id' value='$row[id]'>
<input type='text' name='level' value='$row[level]'>
<input type='submit' name='submit' value='Change Level'>
}
This is edit_level.php
if(isset($_POST['submit'])){
$ed_id = $_GET['ed'];
$level = $_POST['level'];
$sql = "UPDATE users SET level ='$level' WHERE id='$ed_id'";
if(mysqli_query($conn,$sql)){
echo "<p>User Level has been sucessfully udpated! <a href='users.php'>Click here to return to User List.</a>";
}
else{
echo "<p><b>ERROR:User level has not been updated!";
}
}
Edit:
I actually just copied the same format of a delete function I have, but the delete was a link not a form.
<a href='deleteaccount.php?del=$id'>Delete</a> and then in the php:
$del_id = $_GET['del'];
$sql = "DELETE FROM users WHERE id='$del_id'";
Not sure how to do the same but with a form. I need a form because I want to be able to type a new value.
I think problem is wrong naming. Below on action attribute you are sending $id value with ed name, but on hidden input you are sending $id value with id name.
If you are using ?ed=$id on action why you are using hidden $id? Or if you are using hidden $id why you are using ?ed=$id on action area.
<form method='post' action='edit_level.php?ed=$id'>
<input type='hidden' name='id' value='$row[id]'>
You should remove ed or id then if your edit_level.php gets $id value by $_GET or $_POST you should fix it like below
edit_level.php
if you used hidden field you can get $id like $_POST["id"]
if you used edit_level.php?ed=$id you can get $id like $_GET["ed"]
Decide what you want. But don't forget to change you action page while you are doing these changes.

Dropdown menu not displaying change when I change product

I want to access the value selected by user for further processing.
Hence I am using post method to pass the values of whole form.
But GET to access cust_id so that I can reflect change in
further parts of my form. Hence I had to post the following line:
<select id='fullname' onChange="window.location='sp_menu.php?product='+this.value" name='fullname'>
outside php code. But now, once I select some option from dropdown menu, URL changes accordingly, but dropdown menu does not reflects the change
<?php
$query = "SELECT Cust_id, Cust_Name, Cust_City FROM Customers";
$result = mysql_query($query);
?>
<select id='fullname' onChange="window.location='sp_menu.php?product='+this.value" name='fullname'>
<?php
while($row = mysql_fetch_assoc($result)) {
echo '<option value="'.$row['Cust_id'].'">'.$row['Cust_Name'].','.$row['Cust_City'].'</option>';
}
echo '</select>';
?>
How can I, in the same form, access the address of the particular customer id from database when user selects customer name from this dropdown menu?
I think you mean when you change dropdown, the value is not retained, it obviously won't be because your page is being refresh, you need to GET the value from url and put a selected attribute to have that value selected.
Do it this way:
<?php
$query = "SELECT Cust_id,Cust_Name,Cust_City FROM Customers" ;
$result = mysql_query($query);
//checking if GET variable is set, if yes, get the value
$selected_option = (isset($_GET['product'])) ? $_GET['product'] : '';
//we will store all the dropdown html code in a variable and display it later
$select = "<select id='fullname' onChange=\"window.location='sp_menu.php?product='+this.value\" name='fullname'>";
while($row = mysql_fetch_assoc( $result )) {
//checking if the cust_id matches the GET value,
//if yes, add a selected attribute
$selected = ($selected_option==$row['Cust_id'])?'selected':'';
echo '<option value="'.$row['Cust_id'].'"'. $selected. '>' . $row['Cust_Name'] .' , '.$row['Cust_City']. '</option>';
}
$select .= '</select>';
//display the dropdown
echo $select;
?>

A while-loop of a drop-down menu box, and having the submit button be outside of this while-loop

I currently have a drop-down menu box that takes an element from a column from my mySQL database and displays it in the drop-down box. The user can also select from five of the pre-determined options. When submit is clicked, the changes get reflected in the database.
There is no issue with my code, but I would like to make it more intuitive for the user. At the moment, upon clicking submit, the page refreshes, and the page loses its scroll position (once refreshed, the page goes to the top).
How would one make a submit button outside of the while-loop? I want to have the submit button at the bottom of the page, which would mean it will be outside of the iterating while loop, right? How could I do this?
Attached is my code:
<?
if (isset($_POST['formSubmit'])){
$rating = $_POST['rating'];
$accountID = $_POST['accountID'];
mysql_query("UPDATE Spreadsheet SET rating='$rating' WHERE accountID='$accountID'");
}
while ($row = mysql_fetch_array($query)){ ?>
<form name ="rating" method ="POST" action ="" > <?
echo "<input type = 'hidden' name = 'accountID' value = '" . $row['accountID'] . "' >";
?>
<select name="rating">
<? $values = array('0 - No rating','1 - Very Bad','2 - Bad','3 - Average','4 - Above Average');
for ($i =0; $i < count($values); $i = $i + 1){
echo "<option value = \"$i\"";
if ($row['rating'] == $i) {
echo "selected=\"selected\"";
}
echo ">" . $values[$i] . "</option>";
}
?>
</select>
<input type ="Submit" name ="formSubmit" value ="Submit" />
</form>
}
I suggest you work this out with AJAX. Instead of submitting it as form, send it as a AJAX request. Something like:
$('select[name="rating"]').change(function () {
var rating = $("select[name="rating"] option:selected").val();
$.post(
'YOUR-URL.php',
{'rating' : rating},
function(response) {
//do what you got to do
}
)
});

Display checked checkbox record from database

I have looked through similar problems and solution but somehow only half way help me with my problem. I'm trying to make a form to checked more than one record from MySQL database and display the checked record to another page. Somehow I managed to do the page with check boxes but I don't know how to display the record checked. It can only display the first row of the record or all the records regardless which box are checked.
This is checkbox page
$columns = count($fieldarray);
//run the query
$result = mysql_query(
"SELECT * FROM request_item
ORDER BY request_item.IllNo DESC LIMIT 0, 6") or die(mysql_error());
$row = mysql_num_rows($result);
while($row=mysql_fetch_array($result))
{
{
$rows[] = $row['IllNo'];
}
foreach($rows as $value);
echo "";
echo " ";
echo $row['IllNo'];
echo "";
}
echo "";
?>
This is display record checked
$columns = count($fieldarray);
//run the query
$result = mysql_query(
"SELECT * FROM request_item
ORDER BY request_item.IllNo DESC LIMIT 0, 6") or die(mysql_error());
$row = mysql_num_rows($result);
while($row=mysql_fetch_array($result))
{
$rows[]=$row['IllNo'];
foreach($rows as $value);
if ($rows= 'checked') {
echo "";
echo $value;
}
Any help are welcome. Thank you.
There's actually a lot of problems with that script including syntax errors, calling the wrong variable name, form not opening where it should, invoking PHP after you already have, etc...
To get a good answer to you, you should share what make $row['IllNo'] should equal to indicate if it should be checked or not.
I reformatted it a bit and this may give you a good start.
<form NAME ="form1" METHOD ="POST" ACTION ="dari.php">
<table>
<?php
$columns = count($fieldarray);
//run the query
$result = mysql_query("SELECT * FROM request_item ORDER BY request_item.IllNo DESC LIMIT 0, 6") or die(mysql_error()) ;
$row = mysql_num_rows($result);
while($row=mysql_fetch_array($result)) {
echo "<tr><td>";
echo "<Input type = 'Checkbox' Name ='ch1' value ='ch1'";
// check checked if it is. this will be checked if $row['IllNo'] has a value
// if there were a condition to make it checked, you would put the condition
// before the ?
echo $row['IllNo'] ? ' checked' : '';
echo ' />';
echo $row['IllNo'];
echo "</td></tr>";
}
?>
</table>
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Choose your books">
</FORM>

Categories