Check values from shuffled column - php

I have created an exercise on which one column has words and the second column has the pronounce.
The second column is shuffled.
The code which shows the results is:
echo "<table><tr><td>ideogram</td><td>Pinyin<td>";
while($row = pg_fetch_array($ideo_results)){
$pinyin_query = "select id, pinnin from temp_match where ideogram = '".$row['ideogram']."'";
$pinyin_results = pg_query($cn, $pinyin_query);
while($line = pg_fetch_array($pinyin_results)){
echo "<tr><td><input type=button name=".$row['id']." value='".$row['ideogram']."'></td><td><input type=button name=".$line['id']." value='".$line['pinnin']."'></td><td>";
}
}
echo "</table>";
How can I get the values from 2 selected buttons in order to check if the user choose the correct or the wrong ?
The ID is the name for all the buttons BUT the name is unique

Related

How to display entries corresponding to a specific $_SESSION variable

My page will display a list of available restaurants using a mysqli_fetch_array within a while loop. From these I can select the restaurant I want. After that, I'm trying to display the menu items corresponding to a given restaurant based on its rest_id.
In index.php, I've put the rest_id values retrieved from the restaurant table into a $_SESSION['restid'] variable. I.e.
$_SESSION['restid'] = $row['rest_id'];
However, when I use the $_SESSION variable in viewMenu.php to display the menu entries corresponding to a given rest_id, it will only display the entries that corresponds to the final rest_id in the restaurant table.
I do think that's because in the while loop to display the records for the restaurant, the $_SESSION variable will be set to the last rest_id value retrieved by the loop. I can think of an array, but have no clue how to implement it.
So, my question is: how do I get the specific $_SESSION value, which corresponds to the specific rest_id I selected so that I get only the entries based on the restaurant I actually selected?
index.php
<?php
//Connect to mysql db
$con = mysqli_connect('127.0.0.1','root','toor');
//select database
mysqli_select_db($con,'eatrebs');
//Select query
$sql = "SELECT * FROM restaurant";
//Execute query
$records = mysqli_query($con,$sql);
?>
<?php
session_start();
while ($row = mysqli_fetch_array($records))
{
echo '<tr><form action="update.php" method="post">';
echo"<input type=hidden name=id value='".$row['rest_id']."'></td>";
echo "<td><input type = text name = \"rname\" value = '".$row['rest_name']."'</td>";
echo "<td><input type = text name = \"email\" value = '".$row['email']."'</td>";
echo "<td><input type = text name = \"address\" value = '".$row['address']."'</td>";
echo'<td><input type=submit value="Update" onclick="return confirm(\'Are you sure?\');">';
echo'<td><Button btn btn-primary btn-lg pull-left name=del onclick="return confirm(\'Are you sure?\');">Delete</Button>';
echo "</form>";
echo '<form action="Menu/viewMenu.php" method="post">';
echo"<input type=hidden name=iid value='".$row['rest_id']."'></td>";
$_SESSION['restid'] = $row['rest_id'];
echo"<td><input type=submit value=\"View Menu\" name=viewMenu>";
echo'</form></tr>';
}
//viewMenu.php
<?php
session_start();
$con = mysqli_connect("localhost","root","toor");
mysqli_select_db($con, 'eatrebs');
$sql = "SELECT items.* FROM items INNER JOIN restaurant ON restaurant.rest_id = items.rest_id Where items.rest_id = '{$_SESSION['restid']}' ";
$menu = mysqli_query($con,$sql);
?>

query form data to be passed on

I have a SQL table structured that way:
Id_value | id_group_value | name_group_value | name_value
I build a php/sql query to create a dropdown form getting its values which work like that:
<label>item</label>
<?php
$result = $conn->query("select id_ value,id_ group _ value, name _ group_ value, name_value FROM table WHERE id_ group _ value = 0");
echo "<select name='name_value' ><option value='' disabled selected>choose value</option>";
while ($row = $result->fetch_assoc()) {
unset($idv, $namev);
$idv = $row['id_value'];
$namev = $row['name_value'];
$id_gv = $row['id_group_value'];
$id_v = $row[' name_group_value '];
echo '<option value="'.$ idv.'">'.$namev.'</option>';
}
echo "</select>";
I can easily save values from that select form item using a '$_POST[form_item]' which I do this way and means get the $idv variable:
$sql = "INSERT INTO table (form_select_value)
VALUES ('$_POST[name_value]')";
What should I do to save another extracted value which I retrieved from the original SQL query, let’s say $namev variable, together?
Using hidden fields you can set all the values like:
<input type="hidden" name="nvalue" value="<?php echo $namev; ?>>"
And insert value like this,
$sql = "INSERT INTO table (form_select_value1,form_select_value2) VALUES ($_POST['name_value'],$_POST['nvalue'])";

inserting data from multiple text boxes to single column

enter image description herei want to insert multiple text box value in to a single column in a table at a time
it should b like there should be name and infront of name there are text boxes to enter age of the students
so when we submit then the age entered infront of the student name it will get stored in the table with student names and ages.
(multiple students data feeding at a time)
Hi Try this change table name and column name according to yours.
Also i have query all data of table this will update all fields age if already age exist and you fill again in form. So either apply where condition in your query so that it filter only name which does not have price or add value field in form which display age if exist in database.(This is according to code you provided in comment)
<?php
$conn = mysqli_connect($mysql_hostname, $mysql_user, $mysql_password,$mysql_database) or die("Could not connect database");
$result = mysqli_query($conn,"select * from user");
if(isset($_POST['submit'])){
$sql = "UPDATE user SET age = (CASE name ";
foreach($_POST['age'] as $key=>$value){
$sql = "UPDATE user SET age = '$value' where name = '$key'";
mysqli_query($conn,$sql);
}
$success = "updated successfully";
}
if(isset($success) && !empty($success)){
echo '<h3>'.$success.'</h3>';
}
echo '<form method="post">';
echo '<table>';
while($row = mysqli_fetch_assoc($result)){?>
<tr>
<td><input type="text" value="<?php echo $row['name']; ?>" name="name" readonly="true"></td>
<td><input type="number" name="age[<?php echo $row['name']; ?>]" placeholder='enter age'></td>
</tr>
<?php } ?>
<tr><td colspan="2">
<input type="submit" name="submit" value="Save"></td></tr>
</table>
</form>
I didn't get your example but if you want to store multiple values in a single database column, you can pass these values into an array and encode it with json_encode. After reading the values from the database you can transform it back to an array with json_decode.
Use json_encode to encode the content and then store it in database field and when you retrieve the value then use json_decode
$form_data_json = json_encode( $_POST );

PHP While Loop in While Loop

I've had this problem for a little while and probably didn't ask the question properly last time. Having dabbled again I'm still very much confused and stuck.
I have a MySQL table that I list out a series of checkboxes based on values in this (around 200).
I have another MySQL table where a user will store their preferences, when the list loads I wish for the items that the user had previously selected in the second MySQL table to be checkboxes that have the check mark already assigned indicating previous choice. If you could please take a look at the following and point me in the right direction I'd be grateful.
$result = mysql_query("SELECT `car` FROM `carlist` ORDER BY variety ASC");
$result2 = mysql_query("SELECT `car` FROM `lists` WHERE `username` = 'Palendrone' ORDER BY variety ASC");
if (!$result) {
die("Query to show fields from table failed");
}
if (!$result2) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
}
$fields_num2 = mysql_num_fields($result2);
for($j=0; $j<$fields_num2; $j++)
{
$field2 = mysql_fetch_field($result2);
}
while($row = mysql_fetch_row($result))
{
while($row2 = mysql_fetch_row($result2))
{
if("$row2[0]" <> "$row[0]")
{?><input type="checkbox" value="<?php echo "$row[0]"?>" name="<?php echo "$row[0]"?>" id="<?php echo "$row[0]"?>">
<label for="<?php echo "$row[0]"?>"><?php echo "$row[0]"?></label>
<?php
} else
{?><input type="checkbox" value="<?php echo "$row[0]"?>" name="<?php echo "$row[0]"?>" checked="yes" id="<?php echo "$row[0]"?>">
<label for="<?php echo "$row[0]"?>"><?php echo "$row[0]"?></label>
<?php
}
} /* end while */
} /* end while */
I figured the first While loop to load the main 200 items in, then for each input in that table it cross checks against the users selection table in the second while loop, in that loop I have the if statement working in reverse order so as not to check it then uncheck it.
I was pointed in the way of starting from scratch last week by another user on here, although I've started going through some tutorials I kinda need to get this last part of my project nailed...
I would suggest changing your query strategy - use only one instead of two. You can check the value of second member of select list - lists.car from result of query below for null value. If it is null then checkbox is not selected otherwise it is (it is present in second table).
$result = mysql_query("SELECT `carlist`.`car`, `lists`.`car` FROM `carlist` left join `lists` on (`carlist`.`car` = `lists`.`car` and `lists`.`username` = 'Palendrone') ORDER BY `carlist`.variety ASC");

How to insert into a table after doing a select in a form

I have a PHP form that is collecting information and writing it to a database and it is working correctly. I have one field that is a select to choose entries from an existing table. I would like that result to be inserted into a different table but can't get the insert into to work. I would like to insert the FacilityName field chosen in the select to table2.
$result = mysql_query("SELECT * FROM facility");
print "<select name=\"Id\" > \n";
while ($row = mysql_fetch_array($result)){
$Id = $row['Id'];
$FacilityName = $row['FacilityName'];
print "<option value=$Id>$FacilityName\n";

Categories