PHP - Session Vars Loaded From Mssql Query Drop, Those Loaded From Mysql Query Stick
I'm restarting this under a new subject b/c I learned some things after I initially posted and the subject heading is no longer accurate.
What would cause this behavior - when I populate session vars from a MYSQL query, they stick, if I populate them from an MSSQL query, they drop. It doesn't matter if I get to the next page using a header redirect or a form submit. I have two session vars I'm loading from a MYSQL query and they remain, the two loaded from MSSQL disappear. I have confirmed that all four session vars are loading ok initially and I can echo them out to the page, but when the application moves to next page via redirect or form submit, the two vars loaded from MSSQL are empty. Any ideas? Similar TutorialsGood evening, I am currently doing a web application which requires pulling out of data from the database. I am still a novice in the programming industry, and is still seeking help from my colleagues and of course forum sites like phpdn. I have an existing code which my friend provided me. I have already done some modifications with the code. I have a problem though with the code, it executes database query upon loading of the page. I do understand how the code works, however, I am not able to modify the code to disallow the execution of query upon loading of page. Here is the code: <?PHP include("dbconnection.php"); $query = "SELECT * FROM records"; if(isset($_POST["btnSearch"])) { $query .= " WHERE last_name LIKE '%".$_POST["search"]."%' OR first_name LIKE '%".$_POST["search"]."%'OR territory LIKE '%".$_POST["search"]."%'OR job_title LIKE '%".$_POST["search"]."%'OR title LIKE '%".$_POST["search"]."%'OR employer LIKE '%".$_POST["search"]."%' " ; } $result = mysql_query($query, $connection) or die(mysql_error()); ?> I do know that this php code, the way it is written, is supposed to do that - to select data from my database (This code was provided by a friend). But my requirement for the project is actually to give it a search engine and display the information based from the search query. I have a search engine already together with the code, and it works pretty well. What I must do is to disallow the pulling of data from the first load, but just pull data if the search engine is used. Here's the whole code: <link href="add_client.css" rel="stylesheet" type="text/css"> <?PHP include("dbconnection.php"); $query = "SELECT * FROM records"; if(isset($_POST["btnSearch"])) { $query .= " WHERE last_name LIKE '%".$_POST["search"]."%' OR first_name LIKE '%".$_POST["search"]."%'OR territory LIKE '%".$_POST["search"]."%'OR job_title LIKE '%".$_POST["search"]."%'OR title LIKE '%".$_POST["search"]."%'OR employer LIKE '%".$_POST["search"]."%' " ; } $result = mysql_query($query, $connection) or die(mysql_error()); ?> <table width="760" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td><table width="760" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="199" align="center" valign="top"><a href="login.html"><img src="asia.gif" alt="" width="152" height="58" border="0" /></a> <script type="text/javascript" src="menu.js"></script></td> <td width="176" align="right" valign="bottom"><a href="main.php"><img src="Home.jpg" width="104" height="20" border="0"/></a></td> <td width="130" align="right" valign="bottom"><img src="View.jpg" width="104" height="20" border="0"/></td> <td width="146" align="right" valign="bottom"><a href="add_client.php"><img src="Add.jpg" width="104" height="20" border="0"/></a></td> <td width="109" align="right" valign="bottom"> </td> </tr> </table></td> </tr> <tr> <td><table width="760" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="200" height="3" bgcolor="#1B1C78"><img src="images/topspacerblue.gif" alt="" width="1" height="3" /></td> <td width="560" bgcolor="#0076CC"><img src="images/topspacerlblue.gif" alt="" width="1" height="3" /></td> </tr> </table></td> </tr> <tr> <td height="553" align="center" valign="top" bgcolor="#F3FAFE"><br /> <form name="form" action="view_client.php" method="post"> <table width="351" border="0"> <tr> <td width="137" align="left" valign="middle">SEARCH RECORD:</td> <td width="144" align="center" valign="middle"><input type="text" name="search" /></td> <td width="56" align="left" valign="middle"><input type="submit" name="btnSearch" value="Search" /></td> </tr> </table> <br /> <table width="680" border="0" cellpadding="3" cellspacing="1" bordercolor="38619E" > <tr> <th width="100" align="center" bgcolor="#E0E8F3">Territory</th> <th width="110" align="center" bgcolor="#E0E8F3">Employer</th> <th width="110" align="center" bgcolor="#E0E8F3">Job Title</th> <th width="50" align="center" bgcolor="#E0E8F3">Title</th> <th width="110" align="center" bgcolor="#E0E8F3">First Name</th> <th width="110" align="center" bgcolor="#E0E8F3">Last Name</th> <th width="70" align="center" valign="middle" bgcolor="#E0E8F3"> </th> </tr> <?php if($result) { for($i=0; $i<mysql_num_rows($result); $i++) { $id = trim(mysql_result($result, $i, "id")); $territory = trim(mysql_result($result, $i, "territory")); $employer = trim(mysql_result($result, $i, "employer")); $job_title = trim(mysql_result($result, $i, "job_title")); $title = trim(mysql_result($result, $i, "title")); $first_name = trim(mysql_result($result, $i, "first_name")); $last_name = trim(mysql_result($result, $i, "last_name")); echo "<tr>"; echo "<td>".$territory."</td>"; echo "<td>".$employer."</td>"; echo "<td>".$job_title."</td>"; echo "<td>".$title."</td>"; echo "<td>".$first_name."</td>"; echo "<td>".$last_name."</td>"; echo "<td><a href='admin_edit.php?id=".$id."'>edit</a> | <a href='admin_delete.php?id=".$id."'>del</a> </td>"; echo "</tr>"; } } ?> </table> <br /> </form> <p> </p></td> </tr> <tr> <td height="38"><table width="760" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="200" height="35" align="center" bgcolor="#1B1C78" class=white><img src="images/topspacerblue.gif" alt="" width="1" height="3" /> <a href="disclaimer.html"><font color="#FFFFFF">Legal Disclaimer</font></a> </td> <td width="560" align="center" bgcolor="#0076CC" class=white><img src="images/topspacerlblue.gif" alt="" width="1" height="3" /> Copyright © 2006 - 2010 Limited. All rights reserved. </td> </tr> </table></td> </tr> </table> Immediate response is well appreciated. Thank you very much! Hi all,
I am trying to do a query on a database that takes a variable from a html drop down box. I've tried so many different forums and can't find the answers.
HTML PAGE:
<html> Hi Maybe a strange request and perhaps someone here knows a better way of doing what I want. I have a sign up form on my website that automatically pulls the session var 'name' and places this in the text field for "Reference". That way I know which agent signed up a customer. What I need to do now though is allow this variable to be injected through the url for click through banners. This what I've done (within the header HTML Tags): Code: [Select] if ($_GET['Reference'] == '') { $_SESSION['Name'] = $my->name; } else { $_SESSION['Name'] = $_GET['Reference']; } However, even though this is loaded on EVERY page view the variable is empty if the user clicks another page. Just curious how other people feel about this. I am working on an application where a lot of info is pulled from MySQL and needed on multiple pages.
Would it make more sense to...
1. Pull all data ONCE and store it in SESSION variables to use on other pages
2. Pull the data from the database on each new page that needs it
I assume the preferred method is #1, but maybe there is some downside to using SESSION variables "too much"?
Side question that's kind of related: As far as URLs, is it preferable to have data stored in them (i.e. domain.com/somepage.php?somedata=something&otherdata=thisdata) or use SESSION variables to store that data so the URLs can stay general/clean (i.e. domain.com/somepage.php)?
Both are probably loaded questions but any possible insight would be appreciated.
Thanks!
Greg
Edited by galvin, 04 November 2014 - 10:30 AM. I've got a HTML drop down box as similar to this: <select name="dropdown"> <option>Option 1</option> <option>Option 2</option> <option>Option 3</option> </select> If I run a mysql query and get a result of "Option 3", is there anyway using PHP to give Option 3 the selected value? Hello,
I am trying to run a report as per the below mssql query:
$query = "SELECT tblWJCItem.AddedDescription, tblWJC.WJCPrefix, tblWJC.WJCNo, tblWJCItem.MaterialName, tblStockFamily.StockFamily, tblWJCItem.WeightToSend, tblWJC.DateCreated, tblWJC.WJCStatusID FROM tblWJC INNER JOIN tblCustomer ON tblWJC.CustomerID = tblCustomer.CustomerID INNER JOIN tblWJCStockStatus ON tblWJC.WJCStockStatusID = tblWJCStockStatus.WJCStockStatusID INNER JOIN tblStockStatus ON tblWJC.WJCID = tblStockStatus.WJCID LEFT OUTER JOIN tblWJCProductLine ON tblWJC.WJCID = tblWJCProductLine.WJCID LEFT OUTER JOIN tblWJCStockItem ON tblWJCProductLine.WJCProductLineID = tblWJCStockItem.tblWJCStockItem INNER JOIN tblStockFamily ON tblWJCItem.ProductFamilyID = tblStockFamily.StockFamilyID WHERE tblCustomer.CustomerName = 'NAME' AND tblWJCStockStatus.WJCStockStatus <> 'Stock Usage Confirmed' ORDER BY tblWJC.WJCID"; strings from a field(type: VARCHAR(4000)) are being truncated at 255 characters. Apparently this is how varchars work(how is that useful?). I've found these two solutions on the internet but neither of them seemed to work. first, some people thought it might be a php.ini thing, so I put this at the top of my php file: Code: [Select] ini_set ( 'mssql.textlimit' , '65536' ); ini_set ( 'mssql.textsize' , '65536' ); that didn't work, so other people suggested using the text field type instead but these two queries return nothing, so I must be doing something wrong or this just isn't the answer: Code: [Select] $bquery="SELECT CONVERT(TEXT,description) FROM table WHERE userID='$row[userID]'"; $bquery="SELECT CAST(description AS TEXT) FROM table WHERE userID='$row[userID]'"; I have a stored procedure on MSSQL that returns a list of results. This works fine. The results that are returned may or may not be allowed to be seen by the end user. So, if 10 results are returned, maybe 7 of them are allowed to be viewed by the end user. There is a PHP SESSION variable that contains a bunch of codes that is compared to a variable returned from the search result. When these match, the result can be seen by the user. So, I am using a php if statement that echos the result based on the result of the if statement. That works fine. What does not work is the found count for the array. For example, if 10 are found, it returns 10, but the if statement removes 3 making 7 of the results viewable. So, is there a way to iterate through an array result set in PHP and remove results based on an if statement creating a new array? Hope this is not too confusing! Here is my code: // Start MySQL Query for Records $query = "SELECT codes_update_no_join_1b" . "SET orig_code_1 = new_code_1, orig_code_2 = new_code_2" . "WHERE concat(orig_code_1, orig_code_2) = concat(old_code_1, old_code_2)"; $results = mysql_query($query) or die(mysql_error()); // End MySQL Query for Records This query runs perfectly fine when run direct as SQL in phpMyAdmin, but throws this error when running in my script??? Why is this??? Code: [Select] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= new_code_1, orig_code_2 = new_code_2WHERE concat(orig_code_1, orig_c' at line 1 If you also have any feedback on my code, please do tell me. I wish to improve my coding base. Basically when you fill out the register form, it will check for data, then execute the insert query. But for some reason, the query will NOT insert into the database. In the following code below, I left out the field ID. Doesn't work with it anyways, and I'm not sure it makes a difference. Code: Code: [Select] mysql_query("INSERT INTO servers (username, password, name, type, description, ip, votes, beta) VALUES ($username, $password, $name, $server_type, $description, $ip, 0, 1)"); Full code: Code: [Select] <?php include_once("includes/config.php"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title><? $title; ?></title> <meta http-equiv="Content-Language" content="English" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link rel="stylesheet" type="text/css" href="style.css" media="screen" /> </head> <body> <div id="wrap"> <div id="header"> <h1><? $title; ?></h1> <h2><? $description; ?></h2> </div> <? include_once("includes/navigation.php"); ?> <div id="content"> <div id="right"> <h2>Create</h2> <div id="artlicles"> <?php if(!$_SESSION['user']) { $username = mysql_real_escape_string($_POST['username']); $password = mysql_real_escape_string($_POST['password']); $name = mysql_real_escape_string($_POST['name']); $server_type = mysql_real_escape_string($_POST['type']); $description = mysql_real_escape_string($_POST['description']); if(!$username || !$password || !$server_type || !$description || !$name) { echo "Note: Descriptions allow HTML. Any abuse of this will result in an IP and account ban. No warnings!<br/>All forms are required to be filled out.<br><form action='create.php' method='POST'><table><tr><td>Username</td><td><input type='text' name='username'></td></tr><tr><td>Password</td><td><input type='password' name='password'></td></tr>"; echo "<tr><td>Sever Name</td><td><input type='text' name='name' maxlength='35'></td></tr><tr><td>Type of Server</td><td><select name='type'> <option value='Any'>Any</option> <option value='PvP'>PvP</option> <option value='Creative'>Creative</option> <option value='Survival'>Survival</option> <option value='Roleplay'>RolePlay</option> </select></td></tr> <tr><td>Description</td><td><textarea maxlength='1500' rows='18' cols='40' name='description'></textarea></td></tr>"; echo "<tr><td>Submit</td><td><input type='submit'></td></tr></table></form>"; } elseif(strlen($password) < 8) { echo "Password needs to be higher than 8 characters!"; } elseif(strlen($username) > 13) { echo "Username can't be greater than 13 characters!"; } else { $check1 = mysql_query("SELECT username,name FROM servers WHERE username = '$username' OR name = '$name' LIMIT 1"); if(mysql_num_rows($check1) < 0) { echo "Sorry, there is already an account with this username and/or server name!"; } else { $ip = $_SERVER['REMOTE_ADDR']; mysql_query("INSERT INTO servers (username, password, name, type, description, ip, votes, beta) VALUES ($username, $password, $name, $server_type, $description, $ip, 0, 1)"); echo "Server has been succesfully created!"; } } } else { echo "You are currently logged in!"; } ?> </div> </div> <div style="clear: both;"> </div> </div> <div id="footer"> <a href="http://www.templatesold.com/" target="_blank">Website Templates</a> by <a href="http://www.free-css-templates.com/" target="_blank">Free CSS Templates</a> - Site Copyright MCTop </div> </div> </body> </html> hi i have the script below which copies data from one table to another but will only insert new data update current data or delete old data from tempproducts to products then it will delete the tempproducts from the db however i keep getting this error: Warning: mssql_query() [function.mssql-query]: Query failed in E:\UpdateProducts.php on line 33 updateproducts.php Code: [Select] <?php include('../../otherscripts/functions.php'); $log = new Logging(); // create DB connection $host = "localhost"; $user = "user"; $pass = "pass"; $mydb = "db"; $db = mssql_connect($host,$user,$pass); //Select Database mssql_select_db($mydb); // delete all old data $sql0 = "SELECT * FROM tempproduct"; $sql1 = "INSERT INTO products SELECT * FROM tempproduct WHERE manf_part_no NOT IN (SELECT manf_part_no FROM products) AND supp_id NOT IN (SELECT supp_id FROM products)"; $sql2 = "DELETE FROM products WHERE manf_part_no NOT IN (SELECT manf_part_no FROM tempproduct) AND supp_id NOT IN (SELECT supp_id FROM tempproduct)"; $sql3 = "UPDATE p1 SET p1.avail_qty = t1.avail_qty, p1.cost_price = t1.cost_price, p1.rrp = t1.rrp, p1.date_added = t1.date_added, p1.description = t1.description FROM Products p1 INNER JOIN tempproduct t1 ON (p1.manf_part_no = t1.manf_part_no AND p1.supp_id = t1.supp_id)"; $sql4 = "TRUNCATE TABLE tempproduct"; //If tempproduct is empty done Execute Commands if it is full then execute commands $query = mssql_query($sql0) or die($log->lwrite('Failed to select for count from db')); $rowcount = mssql_num_rows($query); if($rowcount == 0){ $log->lwrite('Teh tempproduct am emptyish'); } else{ mssql_query($sql1) or die($log->lwrite('Failed to insert to db'.$sql1)); mssql_query($sql2) or die($log->lwrite('Failed to Delete from db')); mssql_query($sql3) or die($log->lwrite('Failed to Update db')); mssql_query($sql4) or die ($log->lwrite('Failed to TRUNCATE db')); } ?> if i run $sql1 command in the sql manager it runs fine and no errors occur? Is it possible to set the require_once, so that later in the script PHP does not require_once the file? Code: [Select] $file = $root.'filename.php'; set_require_once($file);//this would be nice require_once($file);//so this does not load Hello i am trying to make the admin login in a webpage and after I completed the page i uploaded it and now the page is not loading
Here is the script
<?php error_reporting(E_ALL); ini_set('display_errors', '1'); exit(); ?> <?php session_start(); if (!isset($_SESSION["manager"])){ header("location:admin_login.php"); exit(); } exit(); ?> <?php if(isset($_POST["username"])&&isset($_POST["password"])){ $manager=preg_replace('#[^A-Za-z0-9]#i','',$_POST["username"]); $password=preg_replace('#[^A-Za-z0-9]#i','',$_POST["password"]); include"../storescripts/connect_to_mysql.php"; $sql=mysql_querey("SELECT id FROM admin WHERE username='$manager' AND password='$password' LIMIT 1"); if($existCount == 1){ while($row = mysql_fetch_array($sql)){ $id = $rpw["id"]; } $_SESSION["id"] = $id; $_SESSION["manager"] = $manager; $_SESSION["password"] = $password; header("location:index.php"); exit(); } else { echo 'That Information is incorrect, try again <a href="index.php">Click Here</a>'; exit(); } } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Admin Login</title> <link rel="stylesheet" href="k../style/style.css" type="text/css" media=="screen" /> <script type="text/javascript" src="//use.typekit.net/jxp6vds.js"></script> <script type="text/javascript">try{Typekit.load();}catch(e){}</script> </head> <body> <div align="centre" id="mainWrapper"> <?php include_once("../template_header.php") ?> <div id="pageConntent"> <h2>Please Log In To Manage The store</h2> <form id="form1" name="form1" meathod="post" action="admin_login.php"> Username:<br/> <input name="username" type="text" id="username" size="40"/> <br/><br/> Password<br/> <input name="password" type="password" id="password" size="40"/> <br/> <br/> <br/> <input type="submit" name="button" id="button" value="Log In" /> </form> </div> <?php include_once("../footer.php"); ?> </div> </body> </html>Thank you in advance Somebody please help me. The below given code is not working.
Ok, just another noob problem here while working with PHP + MySQL: I have a php page containing a form which I use to pull the data from mysql database. When the user chooses a value/values from the dropdown lists and clicks submit, the page reloads and displaying the quarry results according to user's selection. When user first loads the php page with form, it shows no data unless he clicks submit. Question: How to show a default quarry value on first page load before user even get to use the form? In other words, I want the user to see the default quarry data pulled from DB on page load before he uses the form to refine the results... I am not sure if its possible, but truly hope so I would appreciate any advice or pointer in the right direction. Please let me know if you need the exact code I am using for my form page. Hi there guys I have a problem that I can't seem to get fixed.
Below Is the code I am using and the Link to the Tutorial:
http://fearlessflyer...x-and-timthumb/
PLEASE HELP! I need help I am a new php user, and am trying to understand how to read a .txt file into an array then to retrieve the index of the array at will. Code: [Select] <?php //echo $myfile = fopen("ArrayTestFile.txt", "r") . "File exists: " or die("File does not exist or you lack permission to open it! "); //echo "The contents of the file is " . file_get_contents("ArrayTestFile.txt") . "<br />"; $fh = fopen("ArrayTestFile.txt", 'r+') or die("Failure"); $array = array(); //creation of array $num = fgets($fh);// recursive call variable $num fgets(.txt line) for ($i = 0; $i <= 5; $i++) //for loop loads first 5 of .txt { $array["i"] = $num; //loads each line of .txt into the array at the loops index. echo "Line " . $i . " of the array is " . $array["i"]; // displays the contents of the array in a print message } ?> Thank you. MOD EDIT: code tags added. when the user clicks the submit button from a html form, it needs to go back two pages with that loaded page refreshed. for the javascript i only have history.go(-2); with php, I have tried cookies and sessions which i discovered that neither will work because the loaded page is not refreshed. I could do a html body onload but that would break the w3c validator. what are my options?
I am able to make the map shift to the direction as intended but I got a few other problems: 1. The player location isn't properly loaded, eventhough it saved to the database successfully (I use locationX and locationY as XY coordinate individually). The X and Y corrdinates shows up on the URL itself like this ".../main.php?x=#&y=#". But if I remove them and then hit enter again the it will reset the position back to the original location of the map. 2. The player tile does not appears in center of the map tiles. Here is the page I uploaded my PHP script: http://student18.gamfe.com.hk/facebook/main.php Here is the map portion of the code: Code: [Select] <?php $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error with MySQL connection'); mysql_query("SET NAMES 'utf-8'"); mysql_select_db($dbname) or die(mysql_error()); $query = "SELECT * FROM game_user WHERE game_id = '00001'"; $result = mysql_query($query) or die(mysql_error()); if ((!empty($_GET["x"])) && (!empty($_GET["y"]))) { $updateString = "UPDATE game_user SET locationX=".$_GET["x"].", locationY=" .$_GET["y"]. " WHERE game_id = '00001'"; mysql_query($updateString); } else { $x = 2; $y = 2; } ?> ... // Phrasing map tiles and player tile code <?php $file = file_get_contents('./images/maps/33/map.txt', true); $entries = explode("\n", $file); for($i=$x-2;$i<=$x+2;$i++){ $data = explode(",", $entries[$i]); for($j=$y-2;$j<=$y+2;$j++){ echo "<td align=\"center\" background=\"images/maps/33/33_"; echo $data[$j]; echo ".gif\" width=\"65\" height=\"65\"> <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"65\" height=\"65\"> <tbody><tr> <td> <center>"; while($rows = mysql_fetch_array($result)) { if(($rows['locationX'] == $_GET["x"]) && ($rows['locationY'] == $_GET["y"])) { echo "<img src=\"../images/player_tile.gif\" title=\"You are here\">"; } } echo "</center> </td> </tr></tbody> </table> </td>"; } echo "</tr><tr>"; } ?> |