I have been trying to work this out for a while.
I have created a form with a dropdown box that gets results from a database. from this i then $_POST that from to another page. From that second page i wish to get the ID number and then get the records and display them on screen.
I will then put them in a table to organise the results better.
can anyone help me in achieving this.
Here is the code for the form (which works and sends the $PlantID)
$sql = "SELECT DISTINCT * FROM PLANTS";
$result = mysqli_query($mysqli,$sql)or die(mysqli_error());
//********************* Botannical name drop down box
echo "<form name='selection' id='selection' action='profile.php' method='post'>";
echo "<select name='flower'>";
while($row = mysqli_fetch_array($result)) {
$plantid = $row['FlowerID'];
$plantname = $row['Botannical_Name'];
$plantcommon = $row['Common_Name'];
/* $plantheight = $row['Height'];
$plantav = $row['AV'];
$plantcolours = $row['Colours'];
$plantflowering = $row['Flower_Time'];
$plantspecial = $row['Special_Conditions'];
$plantfrost = $row['Frost_Hardy'];
$plantaspect = $row['Aspect'];
$plantspeed = $row['Growth_Speed'];*/
echo "<option value=".$plantid.">".$plantname." -> AKA -> ".$plantcommon."</option>";
}
echo "</select>";
echo "<br />";
//********************* End of form
echo "<input type='submit' name='submit' value='Submit'/>";
echo "</form>";
I have created this page to get the ID and display that ID on screen. AS you can tell i have probably doubled up on ways to try work this out.
$sql = "SELECT * FROM PLANTS";
$result = mysqli_query($mysqli,$sql)or die(mysqli_error());
if(isset($_POST['submit'])){
$selected_val = $_POST['Botannical_Name']; // Storing Selected Value In Variable
echo "You have selected :" .$selected_val; // Displaying Selected Value
}
echo "<br />";
echo "well:".$_POST["Botannical_Name"]."<br/>";
echo "now:".$plantquery."<br />";
echo $_POST;
echo "<table>";
foreach ($_POST as $key => $value) {
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
echo "</table>";
Any help would be greatly appreciated.
you should use following to get the selected value,
$selected_val = $_POST['flower'];
if(isset($_POST['submit'])){
$selected_val = $_POST['flower']; // Storing Selected Value In Variable
echo "You have selected :" .$selected_val; // Displaying Selected Value
$sql = "SELECT * FROM PLANTS WHERE FlowerID='.$selected_val.'";
$result = mysqli_query($mysqli,$sql)or die(mysqli_error());
while ($row=mysqli_fetch_assoc($result))
{
echo $row['Botannical_Name'];
}
}
echo "<br />";
print_r($_POST);
if(!empty(_POST)) {
echo "<table>";
foreach ($_POST as $key => $value) {
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
Related
I have below code which creates drop-down from php. I want to achieve 2 things here.
1. I want to set one of the option to be set default. It may be hard coded or selected from query.
2. When hit button, it should retain selected option. I could retrieve selected option using session data with this: echo $print_version1[array_keys($print_version1)[0]];
Drop-down code:
$result = $conn->query("SELECT DISTINCT nx_version FROM workflow1 ORDER BY id DESC");
echo "<form action='http://localhost/w_5aug/process.php' method='get'>";
echo "<html>";
echo "<body>";
echo "<p></p>";
echo "<center>";
echo "<strong> Select Base Verison To Compare With : </strong>";
echo "<select name='nx_version' id='nx_version'>";
while ($row = $result->fetch_assoc()) {
$nx_version = $row['nx_version'];
echo '<option>'.$nx_version.'</option>';
}
echo "</select>";
echo " <button type='submit'><b>Add Base Verison</b></button>";
echo "</center>";
echo "</body>";
echo "</html>";
echo "<p></p>";
$array_select = $_SESSION['data'];
print_r($array_select);
echo "<form>";
i assume that the option that should be selected is$print_version1[array_keys($print_version1)[0]
while ($row = $result->fetch_assoc()) {
$nx_version = $row['nx_version'];
if($_SESSION["id"]) {
if($nx_version == "the hardcode value you want to be selected"){
echo '<option selected="selected">'.$nx_version.'</option>';
}else{
echo '<option>'.$nx_version.'</option>';
}
}else{
if($print_version1[array_keys($print_version1)[0]] == $nx_version){
echo '<option selected="selected">'.$nx_version.'</option>';
}else{
echo '<option>'.$nx_version.'</option>';
}
}
}
I have below code which creates drop-down from php. I want to achieve 2 things here.
1. I want to set one of the option to be set default. It may be hard coded or selected from query.
2. When hit button, it should retain selected option. I could retrieve selected option using session data with this: echo $print_version1[array_keys($print_version1)[0]];
Drop-down code:
$result = $conn->query("SELECT DISTINCT nx_version FROM workflow1 ORDER BY id DESC");
echo "<form action='http://localhost/w_5aug/process.php' method='get'>";
echo "<html>";
echo "<body>";
echo "<p></p>";
echo "<center>";
echo "<strong> Select Base Verison To Compare With : </strong>";
echo "<select name='nx_version' id='nx_version'>";
while ($row = $result->fetch_assoc()) {
$nx_version = $row['nx_version'];
echo '<option>'.$nx_version.'</option>';
}
echo "</select>";
echo " <button type='submit'><b>Add Base Verison</b></button>";
echo "</center>";
echo "</body>";
echo "</html>";
echo "<p></p>";
$array_select = $_SESSION['data'];
print_r($array_select);
echo "<form>";
i assume that the option that should be selected is$print_version1[array_keys($print_version1)[0]
while ($row = $result->fetch_assoc()) {
$nx_version = $row['nx_version'];
if($_SESSION["id"]) {
if($nx_version == "the hardcode value you want to be selected"){
echo '<option selected="selected">'.$nx_version.'</option>';
}else{
echo '<option>'.$nx_version.'</option>';
}
}else{
if($print_version1[array_keys($print_version1)[0]] == $nx_version){
echo '<option selected="selected">'.$nx_version.'</option>';
}else{
echo '<option>'.$nx_version.'</option>';
}
}
}
I am currently working on a PHP and SQLite application. It does have HTML for the webpages being created. I am currently using PDO to connect the database to my online server. When I get to the webpage that allows me to type in what I am searching for then it will display what I have found in the echo statements below. I want to be able to have just the item name, that acts as a hyperlink; when it is clicked on it will go to another webpage (I believe) that will display the item's name, amount, and a short description. Is there a way to use PHP for this action or should I go with the HTML tagging?
if($_POST && isset($_POST['search'])) {
echo "<br>\n";
$query = $mysql->prepare('SELECT * FROM Items WHERE Name = :partname');
$subst = array ('partname' => $_POST['search']);
$query->execute($subst);
echo "<TABLE>";
echo "<tr>";
echo "<td>Name</td>";
echo "<td>Amount</td>";
echo "<td>Detail</td>";
echo "</tr>";
while ($row = $query->fetch()) {
//print_r($row);
echo "<tr>";
echo "<td>$row[Name]</td>";
echo "<td>$row[Amount]</td>";
echo "<td>$row[Detail]</td>";
echo "</tr>";
}
echo "</TABLE>";
} else echo "Item searched for was not found.";
from what i understand
if($_POST && isset($_POST['search'])) {
echo "<br>\n";
$query = $mysql->prepare('SELECT * FROM Items WHERE Name = :partname');
$subst = array ('partname' => $_POST['search']);
$query->execute($subst);
echo "<TABLE>";
echo "<tr>";
echo "<td>Name</td>";
echo "</tr>";
while ($row = $query->fetch()) {
//print_r($row);
$name = $row['Name'];
echo "<tr>";
echo "<td><a href='details.php?name={$name}'>{$name}</a></td>";
echo "</tr>";
}
echo "</TABLE>";
} else echo "Item searched for was not found.";
To use Associative Arrays within double quotes, you need to use curly braces:
echo "<td>{$row['Name']}</td><td>{$row['Amount']}</td><td>{$row['Detail']}</td>";
I am new to StackOverflow, and relatively new to PHP. I have a mixture of a mySQL table and form input, where a user will enter numbers into a field (options) next to each corresponding record (companyid), then submit and these should be inserted into the mySQL table. If no "options" are entered, the loop should skip and move on.
When I submit, nothing is actually inserted into the database. I need a little help because I have looked at several examples and I thought I had everything correct.
Thank you so much!!
Below is the table with form inputs:
<?php
$result = mysqli_query($connection,"SELECT * FROM company_main");
echo "<form action='userinvestdynamic.php' method='post'>";
while($row = mysqli_fetch_array($result))
{
echo "<table border=1>";
echo "<tr>";
echo "<td>Company ID</td>";
echo "<td>" .$row['company_id'] . "</td>";
echo "<input type=hidden name=companyid[] value=" . $row['company_id'] . " />";
echo "<td><input type=number name=options[]></td>";
echo "</tr>";
echo "</table>";
}
echo "<input name='userinvestoptionsdynamic' type='submit' value='Invest!'>";
echo "</form>";
?>
And here is the code once the form is submitted.
<?php
require ('db_login.php');
session_start();
if (isset($_POST['userinvestoptionsdynamic'])) {
$userid = $_SESSION['login_user'];
$companyid = $_POST['companyid'];
$options = $_POST['options'];
// loop through array
$number = count($_POST['companyid']);
for ($i=0; $i<$number; $i++)
{
//store single company and options in local variables
$companyid = $companyid[$i];
$options = $options[$i];
//run insert for any items that don't have blank options
if($options[$i] <>''){
$query = "INSERT INTO user_company_invested(user_id, company_id, user_company_options_invested) VALUES($userid,$companyid,$options)";
mysqli_query($connection, $query);
}
}
header('Location: userpage.php');
} else {
echo 'error';
}
?>
Explanations are inside the comment /* */
<?php
$result = mysqli_query($connection,"SELECT * FROM company_main");
$counter=0; /* DECLARE THIS BEFORE YOUR WHILE LOOP */
echo "<form action='userinvestdynamic.php' method='post'>";
while($row = mysqli_fetch_array($result))
{
echo "<table border=1>";
echo "<tr>";
echo "<td>Company ID</td>";
echo "<td>" .$row['company_id'] . "</td>";
echo "<input type=hidden name=companyid[$counter] value=" . $row['company_id'] . " />"; /* DECLARE THE COUNTER ON COMPANY ID ARRAY */
echo "<td><input type=number name=options[$counter]></td>"; /* DECLARE THE COUNTER ON OPTIONS ARRAY */
echo "</tr>";
echo "</table>";
$counter=$counter+1; /* ADD 1 EVERY LOOP ON COUNTER */
}
echo "<input type='hidden' name='hiddencounter' value='$counter'>"; /* COUNT ALL THE LOOP AND SUBMIT IT TO THE NEXT FORM */
echo "<input name='userinvestoptionsdynamic' type='submit' value='Invest!'>";
echo "</form>";
?>
userinvestdynamic.php:
<?php
require ('db_login.php');
session_start();
if (isset($_POST['userinvestoptionsdynamic'])) {
$userid = $_SESSION['login_user']; /* I ASSUME YOU HAVE LOGIN_USER SESSION DECLARED IN THE PREVIOUS FILE? */
$companyid = $_POST['companyid'];
$options = $_POST['options'];
$counter = $_POST['hiddencounter']; /* THIS WILL SET AS YOUR COUNTER */
for ($i=0; $i<=$counter; $i++)
{
if(empty($options[$i])){ /* IF NO INPUT ON OPTIONS */
/* DO NOTHING */
}
else {
/* store single company and options in local variables */
$query = "INSERT INTO user_company_invested(user_id, company_id, user_company_options_invested)
VALUES($userid,$companyid[$i],$options[$i])";
mysqli_query($connection, $query);
} /* END OF ELSE IF NOT EMPTY COMPANY ID */
} /* END OF FOR LOOP */
header('Location: userpage.php');
} /* END OF ISSET USERINVESTOPTIONSDYNAMIC */
else {
echo 'Error!';
}
?>
Problem is while loop always showing last inserted row result. I've tried this below code to follow/unfollow button option. For example I'm a user id=1. I have already followed user id=4. Now, I want to follow user id=5. When i click follow button(id=5) it turns into Unfollow properly. But, I have already followed user id=4. That turns into Follow. This is my problem.
Then I tried echo $following;. it Prints 5555. That means last inserted data. But I want 45. I'm sure I've made a mistake in my while loop. But I don't know what I should change?
<?php
try
{
$stmt = $conn->prepare("SELECT * FROM users ORDER BY Autoid");
$stmt->errorInfo();
$stmt->execute();
$sth = $conn->prepare("SELECT * FROM followers ORDER BY Autoid");
$sth->errorInfo();
$sth->execute();
while($follow_row = $sth->fetch(PDO::FETCH_ASSOC))
{
$following = $follow_row['Following'];
$follower = $follow_row['Follower'];
}
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo "<tr>";
echo "<td>". $row['Autoid'] ."</td>";
echo "<td>". $row['Name'] ."</td>";
// echo $row['Following'];
if($_SESSION['sesuname'] == $row['Username'])
{
echo "<td class='itsyou' >Its You ". $_SESSION['sesuname'] ."</td>";
}
else
{
if(($follower == $_SESSION['sesid']) AND ($following != $row['Autoid']))
{
//echo "<td>true</td>";
echo $following;
echo "<td>";
echo "<form id='jsform' method='post' action='subscribe.php'>";
echo "<input type='hidden' name='id' value=" . $row['Autoid'] . " />";
echo "<button class='follow' >Follow</button>";
echo "</form>";
echo "</td>";
}
else
{
//echo "<td>false</td>";
echo "<td>";
echo "<form id='ufform' method='post' action='unsubscribe.php'>";
echo "<input type='hidden' name='uid' value=" . $row['Autoid'] . " />";
echo "<button class='follow' >UnFollow</button>";
echo "</form>";
echo "</td>";
}
}
echo "</tr>";
}
} catch (PDOException $e) {
'Database Error : ' .$e->getMessage();
}
?>
This code:
while($follow_row = $sth->fetch(PDO::FETCH_ASSOC))
{
$following = $follow_row['Following'];
$follower = $follow_row['Follower'];
}
Simply OVERWRITES $following and $follower every time you fetch a row, leaving you with the LAST row fetched in the variables. Perhaps you want something more like
$following[] = $follow_row['Following'];
$follower[] = $follow_row['Follower'];
^^--- append new row value to an array.