I want to pass a variable which contains a query, to another php page.
I have variable as follows.
$search_type = $_POST['search_type'];
$search_brand = $_POST['search_brand'];
$result = "SELECT * FROM vehicle_info WHERE type='".$search_type."' AND brand='".$search_brand;
Then I create a link to a different page, I want to pass the $result variable to that page.
<?php echo "<a href='$secondpage.php?data=$result'>Click Here</a>"; ?>
But when I click this link, the whole data doesn't carry out with the URL. When I click the link, my URL was like following
.../secondpage.php?data=SELECT%20*%20FROM%20vehicle_info%20WHERE%20type=
The URL doesn't contain the variables ($search_type and $search_brand)
So how can I make this possible? Please help me !
You can use $_SESSION to store the query or you can encode the string using json-encode function and pass it in link
$search_type = $_POST['search_type'];
$search_brand = $_POST['search_brand'];
$result = mysql_query("SELECT * FROM vehicle_info WHERE type='".$search_type."' AND brand='".$search_brand) or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
extract($row);
echo "<a href='secondpage.php?searchtype=$type&searchbrand=$brand'>Click Here</a>";
}
All of that is assuming your secondpage.php takes in the values searchtype and searchbrand to display the results. The problem was, you did not fetch the results, what you were displaying was the query itself.
Related
Hello i selected all records from users and returned them using a php whileloop and array, using implode i could get all the records outside the loop, what i wish to do actually is to access individual record and assign them to a variable individually to be used later in the same page.
this is what i came up with already, it works, but i don't know how to assign the recorded to individual variables, since all the records are displayed using implode
`<?php
$data = array();
$query = "SELECT * FROM users ";
$queryResult = mysqli_query($conn,$query);
while($_row = mysqli_fetch_array($queryResult)){
$data[] = $_row['first_name'];
$data[] = $_row['email'];
$data[] = $_row['amount'];
?>
<?php }?>
<?php
echo "<br />";
$request = implode("<br />", $data);
echo $request;?>`
please i need assistance on how to achieve or a better way to access whileloop records outside the loop this thanks in advance
This is what i intended doing with that result of the loop in the next query
`<?php
$profit = 10;
$query = "UPDATE storage SET ";
$query .="store_profit = '{$profit}' ";
$query .= "WHERE email = '{$email_from_loop_above}' ";?>`
for clarity purposes.. this script will be executed by a cronjob every 1 minute, initially this is what is did..
`
<?php
$query = "SELECT * FROM users WHERE email = $_SESSION['email'] ";
$queryResult = mysqli_query($conn,$query);
while($_row = mysqli_fetch_array($queryResult)){
$first_name = $_row['first_name'];
$email_from_loop_above = $_row['email'];
$amount = $_row['amount'];
?>
<?php }?>`
Then for the update query this is what i did
`<?php
//The profit below was gotten from a percent from the amount i return from the loop above.
$profit = 10;
$query = "UPDATE storage SET ";
$query .="store_profit = '{$profit}' ";
$query .= "WHERE email = '{$_SESSION['email']}' ";
?>`
Now this code above works perfectly, but when the user logout out the store_profit would not update anymore simply because this you know would require an active user SESSION to work, so what i am trying to do now is is make it work accordingly even when the user is logged out of the account, if i use the following method
`<?php
$query = "SELECT * FROM users ";
$queryResult = mysqli_query($conn,$query);
while($_row = mysqli_fetch_array($queryResult)){
$first_name = $_row['first_name'];
$email_from_loop = $_row['email'];
$amount = $_row['amount'];
?>
<?php }?>`
`
<?php
$profit = 10;
$query = "UPDATE storage SET ";
$query .="store_profit = '{$profit}' ";
$query .= "WHERE email = '{$email_from_loop}' ";
?>`
it would only return the last user data only, simply because i am outside the loop, now when i try to update inside the loop, it works but it adds 10 to store_profit to the first user and adds 20 to the second user also adds 30 to third user... simply because it inside the loop
now what is want to do is make this work accordingly so i decided to use array, however i am not to familiar with array that why i became confused.
An explanation of how i can achieve this purpose would be very much appreciated thanks.
however i was thinking of not destroying all the user session maybe i create session for that purpose and not distroy it.. but i don't if that method would be safe or good.
As a result of your loop you have a $data array which you then turn into a string by gluing all its elements using implode function.
You can of course still access every individual element of $data array outside of the loop by addressing to its direct index.
For instance:
echo $data[0]['email'];
will output the email record of first (arrays are 0 based in PHP) element of your array.
I am trying to store the result from a MySQL statement for later use in PHP. I have this code which gets me the result:
// Get the categories from the db.
$categories = array();
$catSql = "SELECT id, name FROM categories";
if ($catStmt = mysqli_prepare($db, $catSql))
{
$catStmt->execute();
$result = $catStmt->get_result();
// Fetch the result variables.
while ($row = $result->fetch_assoc())
{
// Store the results for later use.
}
}
So I know i will have the results in $row["id"] and $row["name"] and I want to save all of the rows so whenever i need them i can loop through them and for example echo them. I have searched for structures and arrays for keeping them in PHP but I cannot seem to find any information about that or maybe I am not searching in the right direction. Can anyone point me where i should read about this to find out how to do this efficiently and if possible post a small example?
Use sessions:
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
// Get the categories from the db.
$categories = array();
$catSql = "SELECT id, name FROM categories";
if ($catStmt = mysqli_prepare($db, $catSql))
{
$catStmt->execute();
$result = $catStmt->get_result();
// Fetch the result variables.
while ($row = $result->fetch_assoc())
{
// Store the results for later use.
$_SESSION['category_' . $row['id']] = $row['name'];
}
}
Then access it later from a different page
$_SESSION['session_variable_name']
You can also create an array of the information and store the entire array in a single session variable.
Just make sure you add the session_start function at the beginning of each page. The if statement prevents you from trying to start it multiple times.
$categories = array();
$catSql = "SELECT id, name FROM categories";
if ($catStmt = mysqli_prepare($db, $catSql))
{
$catStmt->execute();
$result = $catStmt->get_result();
while ($row = $result->fetch_assoc())
{
$categories[$row['id']]=$row['name'];
}
}
And If you want the name anywhere use below :
$categories[$id]
I was facing like id overwriting problem when I tried to pass:
$_session['ads_id'] = $row['ads_id'];
to the next page using:
echo $_session['ads_id'];
I then define an array like:
$session['ads_id] = array();
and
$session['ads_id'][] = $row['ads_id'];
When I echo it like:
$ads_id = implode($_SESSION["ads"]);
so it was showing all the ads_id like 112,113,114 and not specific linked with user_id.
Then I defined a foreach loop like:
foreach($_SESSION['ads'] as $ads_id) {
}
Now I don't know how to pass it to the next page? If someone clicks ads_id 113 from the table and it shows only 113 and not other ids.
$sql = "SELECT * FROM ads where user_id ='{$_SESSION[ "user_id" ]}'";
$result = $conn->query($sql);
$ads_id = array();
if ($result->num_rows > 0) {
$_SESSION['ads'] = array();
while($row = $result->fetch_assoc()) {
$_SESSION['ads'][]= $row['ads_id'];
foreach($_SESSION['ads'] as $ads_id) {
}
echo $ads_id //// Here I don't want to echo it, I want to pass to the next page
I want to pass the $ads_id to update.php from here.
I tried like this on update.php which is not working.
SESSION_START();
echo $ads_id ;
echo $_SESSION['ads_id'];
Your $_SESSION variable will be accessible in all your other scripts as long as all other scripts starts with session_start().
<?php
session_start();
//...rest of the codes
I am trying to use variables(dates) queried in a php page then placed in $_SESSION, in another page to perform another query.
I will only use one date from that session array. which is that one i clicked on its link tag.
Is there an action that should be performed onclick of that link?
Here is my php, first page it creates the $_session variables and create the links.
The second page it is supposed to print the value i clicked. but it does not.
oldentries.php:
<?php
$query = "SELECT DISTINCT Tdate FROM titletable";
$result = mysqli_query($conn, $query);
if(mysqli_num_rows($result) > 0 )
{
while($row = mysqli_fetch_assoc($result))
{
$_SESSION[$row["Tdate"]]= $row["Tdate"];
echo ''.$_SESSION[$row["Tdate"]].'<br>';
}
}
else
{
echo "No Results in the database!";
}
?>
content.php
<?php
$datetable = $_SESSION[$row["Tdate"]];
echo "$datetable";
$query = "SELECT table_name FROM information_schema.tables where table_schema='council_db'";
?>
Your $_SESSION array is keyed by the result of the SQL query. In your content.php the $row variable is not available. In cases like this it's probably better to pass variables between pages with a query parameter rather than the session.
...
while($row = mysqli_fetch_assoc($result))
{
echo ''.$row["Tdate"].'<br>';
}
...
Then in content.php you can access it with $_GET
$datetable = $_GET["Tdate"];
As you can see from the code below im very new to PHP, so please excuse the newbie question but ive been struggling with this all afternoon and just cant figure out what exactly is the problem.
Basically Ive got 3 dropdown menus for team1, team2 and venue.
When user selects teams and venue an isset function is triggered and I extract the result from the game out of my database.
As you can see in the code below I use 3 echo statements to test if correct values from dropdown menus has been captured. When I open the page the echo statements confirm the correct results are captured from the drop-downs, I go on and query the database with the variables I echoed above.
The Problem
In the while loop, I want to echo the results from the query BUT nothing gets displayed. I tried doing a var_dump to see the contents of the variables but NOTHING gets displayed. What am I doing wrong?
foreach($_REQUEST["team1"] as $teamone){
$team1 = $teamone;
}
foreach($_REQUEST["team2"] as $teamtwo){
$team2 = $teamtwo;
}
foreach($_REQUEST['venue'] as $venue){
$venue = $venue;
}
//These echo statments are a test to see if the correct dropdown values has been captured
echo $team1.'<br>';
echo $team2.'<br>';
echo $venue;
//Use results from dropdown menu to query database
$result = mysql_query('SELECT *
FROM `results`
WHERE `hometeam` = "$team1" && `awayteam` = "$team2"') or
die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['awayteamscore'];
echo $row['hometeamscore'];
}
If anyone can point me in the right direction it would be greatly appreciated.
I suspect you're receiving sql syntax error for two reasons at least
First when you want to use the variable value within string you must use double embracing quotes
$sql="select * from mytable where col1='$my_var'";
And second: SQL expects string values ebraced with simgle quotes as I pointed above. In your case you use "" quotes.
And one more recommendation. When debugging php app it might be useful to enable extended output by inserting
error_reporing(E_ALL);
somewhere in the beginning of php script
you mixed between double and single quotes . while variables shouldnt be rounded by double quotes.
$result = mysql_query(" SELECT *
FROM `results`
WHERE `hometeam` = '$team1' && `awayteam` = '$team2' ") or
die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['awayteamscore'];
echo $row['hometeamscore'];
}
You put your SQL query in single quotes and variables do not work in there. Try this:
<?php
$var = "stuff";
echo '$var';
echo "$var";
?>
What you can do instead is to concatenate strings like this:
$result = mysql_query('SELECT *
FROM `results`
WHERE `hometeam` = "'.$team1.'" && `awayteam` = "'.$team2.'"')
My Friend Your code is just fine you just need to check that your submiting the dropboxes with the form so first check your html and when you var_dump($_REQUEST) it should be an array containing all the values that you trying to gather
Your foreach loops are overriding the values. It would be best to put it in an array like so
$team1 = array();
foreach($_REQUEST["team1"] as $teamone){
$team1[] = $teamone;
}
if you need to make the values of $team1 into a string then this would be more appropriate
$team1 = "";
foreach($_REQUEST["team1"] as $teamone){
$team1 .= $teamone;
}
Fixing the above first might help. If not then maybe the problem is in your db queries.
//Using double quotes
"SELECT * FROM `results` WHERE `hometeam` = '$team1' && `awayteam` = '$team2'"
//Using single quotes
'SELECT * FROM `results` WHERE `hometeam` = "'.$team1.'" && `awayteam` = "'.$team2.'"'
Then display the result
while($row = mysql_fetch_array($result)){
echo $row['awayteamscore'];
echo $row['hometeamscore'];
}