PHP - Comparing 2 Variable Values
i've google but no luck on finding what i need....is there a way in javascript to compare 2 variables ie.
var him = "jack"; var her = "jill"; if (him == her){ alert('same name'); }thank you Similar TutorialsHi. I have a MySQL database with 4 different fields. id, customer, case and status. No problems of getting them to show up on my page but.. I would like to change the row bgcolor depending of field value on "customer". For example row color would be white until customer changes, then color yellow until customer changes again and back to white etc.. Hope you get the point. Is this possible? Thanks. Hi im not sure if this can be done or not but im trying to do a site without using mysql and i want to be able to compare 3 values and depending on the values have them aranged lowest to highest... for example: Apple = 8 Pear = 3 Bannana = 5 so the results would be displayed like... Pear with a total of 3 bannana with a total of 5 Apple with a total of 8 Is this possible using just PHP or will i need to use Mysql as well... Thank you Chris Hi there i have 2 queries one for each table that calculates values in one or more fields. The two tables are different in structure so i have had to calculate them differently: | Ship table | Class |Planet table | Class 1 Class 2 Class 3 Class 4 Depending on what type of ship is added (Class 1-4 ship) its corresponding Class is determined. The planet table is different though as it can have any number of Class 1 - 4 ships. So with help ive put the Ship query results into an array and echoed the results. The Planet query is not in an array as such as i could acheive the same results using the SUM() function. Both work fine and the outputted results look like this: Ship query 1 Class 1 4 Class 2 3 Class 3 1 Class 4 Planet query 1 Class 1 2 Class 2 4 Class 3 13 Class 4 What i really need to do now is compare each queries Class and if the planets class 1-4 < ships class 1-4 then echo the difference. For example the results would be: Class 1 = 0 echo no Class 1 needed Class 2 = - 2 echo no Class 2 needed Class 3 = +1 echo 1 Class 3 needed Class 4 = +12 echo 12 class 4 needed Im sorry if it seems longwinded or if ive not explained my self very well but im finding it difficult. Heres the code: Code: [Select] <?php $colname_resultp = "-1"; if (isset($_SESSION['MM_Username'])) { $colname_resultp = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']); } mysql_select_db($database_swb, $swb); $query_resultp = sprintf("SELECT SUM(Class1) As Class1_totla, SUM(Class2) As Class2_totla, SUM(Class3) As Class3_totla, SUM(Class4) As Class4_totla FROM planet WHERE PlayerName = %s", GetSQLValueString($colname_resultp, "text")); $resultp = mysql_query($query_resultp, $swb) or die(mysql_error()); $row_resultp = mysql_fetch_assoc($resultp); $totalRows_resultp = mysql_num_rows($resultp); $colname_Recordset1 = "-1"; if (isset($_SESSION['MM_Username'])) { $colname_Recordset1 = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']); } mysql_select_db($database_swb, $swb); $query = sprintf("SELECT Class FROM ships WHERE PlayerName = %s ORDER BY Class ASC", GetSQLValueString($colname_Recordset1, "text")); $result = mysql_query($query, $swb) or die(mysql_error()); $number_of_results = array(); while($row = mysql_fetch_array($result)){ if (isset($number_of_results[$row['Class']])) { $number_of_results[$row['Class']]++; } else { $number_of_results[$row['Class']] = 1; } } foreach ($number_of_results as $class => $number_of_class) { echo $number_of_class . " Class " . $class; echo "<br />"; } <?php do { ?> <?php echo $row_resultp['Class1_totla']; echo ' Class 1'; echo'<br>'; echo $row_resultp['Class2_totla']; echo ' Class 2'; echo'<br>'; echo $row_resultp['Class3_totla']; echo ' Class 3'; echo'<br>'; echo $row_resultp['Class4_totla']; echo ' Class 4'; ?> <?php } while ($row_resultp = mysql_fetch_assoc($resultp)); mysql_free_result($result); mysql_free_result($resultp); ?> If you could please help that would be ace. Thanks for listening. Hi guys,
I wonder if you can offer any advice?
I have two user database tables that are about 70% identical. The problem is that with one of them, the phone number for each user has been cut short by two digits (it was an error on my part early on in the database design).
What I need to do is to get the values from both tables and put them into separate arrays then compare the arrays in a loop. If, say, the first five digits of the phone number in each row of the first table match one entry in the second table then update the second table with the phone number (the full length one).
I'm about puzzled about how to approach this, I've still got a lot to learn about array functions, it seems like it should be fairly simple but I'm a bit lost as to where to begin.
Any advice would be massively appreciated!
hey guys, i have my code set up to run substrings through a db to check for matches...even when there is a match it wont insert correctly Code: [Select] $result = mysql_query("select * from friends where userid=$userid"); while($row = mysql_fetch_assoc($result)) { $first_name = $row['first_name']; $last_name = $row['last_name']; $strpos_1 = strpos($friend_1, ' '); $substr_1 = substr($friend_1, 0, $strpos_1); $substr_2 = substr($friend_1, $strpos_1); if($substr_1 == $first_name && $substr_2 == $last_name) { $sql = "insert into fav_friends values('', '$userid', '$friend_1', '$friend_2', '$friend_3', '$friend_4', '$friend_5', '$friend_6')"; $query = mysql_query($sql); } } and i don't receive any errors, any thoughts? This has kept me busy for a couple of hours. I have two arrays that I would like to compare against each other to see matches and otherwise. Simple enough, right? foreach ($array1 as $value){ if (in_array($value, $array2)) { echo $value."-FOUND<br />"; } else { echo $value."-NOT!<br />"; } } Using print_r I can CLEARLY see matches: Code: [Select] array1 = Array ( [0] => CHARLES [1] => TOM [2] => DICK [3] => HARRY) array2 = Array ( [0] => HARRY [1] => DICK [2] => TOM [3] => CHARLES) The only difference I can see is the keys - but why should that matter for what I'm trying to do? Both arrays were converted to uppercase. I have done variations of search_array, array_intersect and array_unique - but absolutely nothing is working. 3 hours later and I'm no further then I started off. WTF? Hi all, Just curious why this works: Code: [Select] while (($data = fgetcsv($handle, 1000, ",")) !== FALSE){ $import="INSERT into $prodtblname ($csvheaders1) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]')"; } And this does not: $headdata_1 = "'$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]'"; while (($data = fgetcsv($handle, 1000, ",")) !== FALSE){ $import="INSERT into $prodtblname ($csvheaders1) values($headdata_1)"; }it puts $data[#'s] in the database fields instead of the actual data that '$data[0]','$data[1]'... relates to. I wrote a script to create the values in $headdata_1 based on the number of headers in $csvheaders1 but can't seem to get it working in the sql statement. Thanks Hi Guys, I don't know how to do this but basically I want the value of $postcode3 to equal the value of $postcodep1 aswell as $postcodep2 like this... else {$postcode3 = $postcodep1 $postcodep2;} That doesn't work... Say $postcodep1 = LS14 $postcodep2 = 2AB Then $postcode3 = LS14 2AB How do I write this in the above php code?! Thanks, S Hey everyone, I'm new to this forum and I came here looking for some help with PHP. I've just started getting into PHP and you guys are probably a smile ahead. So basically, I have some inputs and want them to be sent to my email in a basic layout shown below. Code: [Select] Inputs: Title = _title Forum Name = _forumname Name = _irlname Age = _age I want the email to be sent to me like: Code: [Select] Email Title --------------- Forum Name: TextGiven Real Life Name: TextGiven Age: TextGiven Here is my current code which titles the email correctly, but I want to figure out how to make $message set to the example email I showed above. Code: [Select] <?php $to = "******@*********.net"; $subject = $_REQUEST['_title']; $headers = "From: ***** Applicant \r\n"; $headers .= "MIME-Version: 1.0\r\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\r\n" . "Content-Transfer-Encoding: 7bit\r\n"; $message = $_REQUEST['_forumname' & '_irlname']; <--- Part I want fixed if(mail($to, $subject, $message, $headers)) { echo 'MAIL SENT'; } else { echo 'MAIL FAILED'; } ?> I have this code, and it works. But I want $message = "" to make the message contain not just the forum name, but as well as the name, age, and all other inputs I want. Hi folks, I was wondering how to do this. I want the if statement to detect if the query string has any of these values. so im trying to assign them all to the same variable. However, this code wont work. Whats the trick here? <?php $primary=$_GET['intro']; $primary=$_GET['port']; $primary=$_GET['about']; $primary=$_GET['contact']; if(isset($primary)){ echo "<img src='graphics/left-a.png'>";} else {echo "<img src='graphics/leftb.png'>";}?> are all of the following same, or some same or all different: $variable="" $variable=0 $variable=NULL and which of the above will satisfy if(empty($variable)) { blah!; } I have 2 file names, which are uploaded, stored in the variable $files. I want to be able to assign a variable to each file name. What's the best way of doing it?? I want to be able to insert each file into a mysql database on the same line but in different fields. Here is a sample part of my code: Code: [Select] var_dump($files); print_r(array_slice(array($files),0)); //I want to split this into 2 variables. One for each file name??????? list($file1)=$files; //insert the values/filenames into mysql fields "itm_pic_name & itm_pic_name2" $mysqli = new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME); $this->execSQL("INSERT INTO itm_pic_detail(itm_pic_name,itm_pic_name2,itm_pic_type, itm_pic_size) VALUES (?,?,?,?)", array('ssss',$file1,$file1,$type, $size),true); I would really like some input. : I am building a website that uses session variables extensively. The site is getting a little complicated, and I am starting to lose track of which session variables are assigned, and what their values are. Is there some way where I can create a piece of code that will display all of the existing session variables and their values? The php web script that I’m trying to modify shows an html page of transactions. I’d like to change what is displayed there. The array that generates what is displayed begins on approx line 184 (in bold, near the bottom of the code below, with the line: $ads_list .= PT_LoadPage('transactions/list',array( I’d like help to substitute what appears on the html transaction page by changing what is listed in the transaction/list array (with data from the ‘u_paid_videos’ table, which has these columns: id, id_user, id_video, video_title, time, user_id_uploaded, time_date, earned_amount, currency). Here is current the php code: <?php error_reporting(-1); // set maximum errors ini_set('display_errors' , 'true'); if (!IS_LOGGED || ($pt->config->sell_videos_system == 'off' && $pt->config->usr_v_mon == 'off') ) { header('Location: ' . PT_Link('404')); exit; } $currency = '$'; if ($pt->config->payment_currency == 'EUR') { $currency = '€'; } $types = array('today','this_week','this_month','this_year'); $type = 'today'; if (!empty($_GET['type']) && in_array($_GET['type'], $types)) { $type = $_GET['type']; } if ($type == 'today') { $start = strtotime(date('M')." ".date('d').", ".date('Y')." 12:00am"); $end = strtotime(date('M')." ".date('d').", ".date('Y')." 11:59pm"); $array = array('00' => 0 ,'01' => 0 ,'02' => 0 ,'03' => 0 ,'04' => 0 ,'05' => 0 ,'06' => 0 ,'07' => 0 ,'08' => 0 ,'09' => 0 ,'10' => 0 ,'11' => 0 ,'12' => 0 ,'13' => 0 ,'14' => 0 ,'15' => 0 ,'16' => 0 ,'17' => 0 ,'18' => 0 ,'19' => 0 ,'20' => 0 ,'21' => 0 ,'22' => 0 ,'23' => 0); $ads_array = $array; $date_type = 'H'; $pt->cat_type = 'today'; $pt->chart_title = $lang->today; $pt->chart_text = date("l"); } elseif ($type == 'this_week') { $time = strtotime(date('l').", ".date('M')." ".date('d').", ".date('Y')); if (date('l') == 'Saturday') { $start = strtotime(date('M')." ".date('d').", ".date('Y')." 12:00am"); } else{ $start = strtotime('last saturday, 12:00am', $time); } if (date('l') == 'Friday') { $end = strtotime(date('M')." ".date('d').", ".date('Y')." 11:59pm"); } else{ $end = strtotime('next Friday, 11:59pm', $time); } $array = array('Saturday' => 0 , 'Sunday' => 0 , 'Monday' => 0 , 'Tuesday' => 0 , 'Wednesday' => 0 , 'Thursday' => 0 , 'Friday' => 0); $ads_array = $array; $date_type = 'l'; $pt->cat_type = 'this_week'; $pt->chart_title = $lang->this_week; $pt->chart_text = date('y/M/d',$start)." To ".date('y/M/d',$end); } elseif ($type == 'this_month') { $start = strtotime("1 ".date('M')." ".date('Y')." 12:00am"); $end = strtotime(cal_days_in_month(CAL_GREGORIAN, date('m'), date('Y'))." ".date('M')." ".date('Y')." 11:59pm"); if (cal_days_in_month(CAL_GREGORIAN, date('m'), date('Y')) == 31) { $array = array('01' => 0 ,'02' => 0 ,'03' => 0 ,'04' => 0 ,'05' => 0 ,'06' => 0 ,'07' => 0 ,'08' => 0 ,'09' => 0 ,'10' => 0 ,'11' => 0 ,'12' => 0 ,'13' => 0 ,'14' => 0 ,'15' => 0 ,'16' => 0 ,'17' => 0 ,'18' => 0 ,'19' => 0 ,'20' => 0 ,'21' => 0 ,'22' => 0 ,'23' => 0,'24' => 0 ,'25' => 0 ,'26' => 0 ,'27' => 0 ,'28' => 0 ,'29' => 0 ,'30' => 0 ,'31' => 0); }elseif (cal_days_in_month(CAL_GREGORIAN, date('m'), date('Y')) == 30) { $array = array('01' => 0 ,'02' => 0 ,'03' => 0 ,'04' => 0 ,'05' => 0 ,'06' => 0 ,'07' => 0 ,'08' => 0 ,'09' => 0 ,'10' => 0 ,'11' => 0 ,'12' => 0 ,'13' => 0 ,'14' => 0 ,'15' => 0 ,'16' => 0 ,'17' => 0 ,'18' => 0 ,'19' => 0 ,'20' => 0 ,'21' => 0 ,'22' => 0 ,'23' => 0,'24' => 0 ,'25' => 0 ,'26' => 0 ,'27' => 0 ,'28' => 0 ,'29' => 0 ,'30' => 0); }elseif (cal_days_in_month(CAL_GREGORIAN, date('m'), date('Y')) == 29) { $array = array('01' => 0 ,'02' => 0 ,'03' => 0 ,'04' => 0 ,'05' => 0 ,'06' => 0 ,'07' => 0 ,'08' => 0 ,'09' => 0 ,'10' => 0 ,'11' => 0 ,'12' => 0 ,'13' => 0 ,'14' => 0 ,'15' => 0 ,'16' => 0 ,'17' => 0 ,'18' => 0 ,'19' => 0 ,'20' => 0 ,'21' => 0 ,'22' => 0 ,'23' => 0,'24' => 0 ,'25' => 0 ,'26' => 0 ,'27' => 0 ,'28' => 0 ,'29' => 0); }elseif (cal_days_in_month(CAL_GREGORIAN, date('m'), date('Y')) == 28) { $array = array('01' => 0 ,'02' => 0 ,'03' => 0 ,'04' => 0 ,'05' => 0 ,'06' => 0 ,'07' => 0 ,'08' => 0 ,'09' => 0 ,'10' => 0 ,'11' => 0 ,'12' => 0 ,'13' => 0 ,'14' => 0 ,'15' => 0 ,'16' => 0 ,'17' => 0 ,'18' => 0 ,'19' => 0 ,'20' => 0 ,'21' => 0 ,'22' => 0 ,'23' => 0,'24' => 0 ,'25' => 0 ,'26' => 0 ,'27' => 0 ,'28' => 0); } $ads_array = $array; $pt->month_days = count($array); $date_type = 'd'; $pt->cat_type = 'this_month'; $pt->chart_title = $lang->this_month; $pt->chart_text = date("M"); } elseif ($type == 'this_year') { $start = strtotime("1 January ".date('Y')." 12:00am"); $end = strtotime("31 December ".date('Y')." 11:59pm"); $array = array('01' => 0 ,'02' => 0 ,'03' => 0 ,'04' => 0 ,'05' => 0 ,'06' => 0 ,'07' => 0 ,'08' => 0 ,'09' => 0 ,'10' => 0 ,'11' => 0 ,'12' => 0); $ads_array = $array; $date_type = 'm'; $pt->cat_type = 'this_year'; $pt->chart_title = $lang->this_year; $pt->chart_text = date("Y"); } $day_start = strtotime(date('M')." ".date('d').", ".date('Y')." 12:00am"); $day_end = strtotime(date('M')." ".date('d').", ".date('Y')." 11:59pm"); $this_day_ads_earn = $db->rawQuery("SELECT SUM(amount) AS sum FROM ".T_ADS_TRANS." c WHERE `time` >= ".$day_start." AND `time` <= ".$day_end." AND type = 'video' AND video_owner = ".$pt->user->id); //$this_day_video_earn = $db->rawQuery("SELECT * FROM ".T_VIDEOS_TRSNS." c WHERE `time` >= ".$day_start." AND `time` <= ".$day_end." AND user_id = ".$pt->user->id); $this_day_video_earn = $db->rawQuery("SELECT * FROM u_paid_videos c WHERE `time` >= ".$day_start." AND `time` <= ".$day_end." AND user_id_uploaded = ".$pt->user->id); $day_net = 0; foreach ($this_day_video_earn as $tr) { if ($tr->currency == "USD") { //$day_net = $day_net + ($tr->amount - $tr->admin_com); $day_net = $day_net + ($tr->earned_amount); } } $today_earn = $this_day_ads_earn[0]->sum + $day_net ; $month_start = strtotime("1 ".date('M')." ".date('Y')." 12:00am"); $month_end = strtotime(cal_days_in_month(CAL_GREGORIAN, date('m'), date('Y'))." ".date('M')." ".date('Y')." 11:59pm"); $this_month_ads_earn = $db->rawQuery("SELECT SUM(amount) AS sum FROM ".T_ADS_TRANS." c WHERE `time` >= ".$month_start." AND `time` <= ".$month_end." AND type = 'video' AND video_owner = ".$pt->user->id); //$this_month_video_earn = $db->rawQuery("SELECT * FROM ".T_VIDEOS_TRSNS." c WHERE `time` >= ".$month_start." AND `time` <= ".$month_end." AND user_id = ".$pt->user->id); $this_month_video_earn = $db->rawQuery("SELECT * FROM u_paid_videos c WHERE `time` >= ".$month_start." AND `time` <= ".$month_end." AND user_id_uploaded = ".$pt->user->id); $month_net = 0; foreach ($this_month_video_earn as $tr) { if ($tr->currency == "USD") { //$month_net = $month_net + ($tr->amount - $tr->admin_com); $month_net = $month_net + ($tr->earned_amount); } } $month_earn = $this_month_ads_earn[0]->sum + $month_net ; //$trans = $db->where('user_id',$user->id)->orderBy('id','DESC')->get(T_VIDEOS_TRSNS); $trans = $db->where('user_id_uploaded',$user->id)->orderBy('id_user')->get('u_paid_videos'); $ads_trans = $db->where('time',$start,'>=')->where('time',$end,'<=')->where('video_owner',$pt->user->id)->where('type','video')->get(T_ADS_TRANS); $total_ads = 0; if (!empty($ads_trans)) { foreach ($ads_trans as $key => $ad) { if ($ad->time >= $start && $ad->time <= $end) { $day = date($date_type,$ad->time); if (in_array($day, array_keys($ads_array))) { $ads_array[$day] += $ad->amount; $total_ads += $ad->amount; } } } } $ads_list = ""; $total_earn = 0; if (!empty($trans)) { foreach ($trans as $tr) { //$video = PT_GetVideoByID($tr->video_id, 0, 0, 2); $video = PT_GetVideoByID($tr->id_video, 0, 0, 2); $user_data = PT_UserData($tr->id); $currency = ""; $admin_currency = ""; $net = 0; if ($tr->currency == "USD") { $currency = "$"; //$admin_currency = "$".$tr->admin_com; $net = $tr->earned_amount; } else if($tr->currency == "EUR"){ $currency = "€"; //$admin_currency = "€".$tr->admin_com; //$net = $tr->amount - $tr->admin_com; $net = $tr->earned_amount; } elseif ($tr->currency == "EUR_PERCENT") { $currency = "€"; //$admin_currency = $tr->admin_com."%"; //$net = $tr->amount - ($tr->admin_com * $tr->amount)/100; $net = $tr->earned_amount; } elseif ($tr->currency == "USD_PERCENT") { $currency = "$"; //$admin_currency = $tr->admin_com."%"; //$net = $tr->amount - ($tr->admin_com * $tr->amount)/100; $net = $tr->earned_amount; } if ($tr->time >= $start && $tr->time <= $end) { $day = date($date_type,$tr->time); if (in_array($day, array_keys($array))) { $array[$day] += $net; } } $total_earn = $total_earn + (float)$net; if (!empty($video) && !empty($user_data)) { **$ads_list .= PT_LoadPage('transactions/list',array(** 'ID' => $tr->id, 'PAID_USER' => substr($user_data->name, 0,20), 'PAID_URL' => $user_data->url, 'USER_NAME' => $user_data->username, 'VIDEO_NAME' => substr($video->title, 0,20) , 'VIDEO_URL' => $video->url, 'VIDEO_ID_' => PT_Slug($video->title, $video->video_id), 'AMOUNT' => $tr->earned_amount, "CURRENCY" => $currency, "A_CURRENCY" => $admin_currency, "NET" => $net, "TIME" => PT_Time_Elapsed_String($tr->time) )); } } } $total_earn = $total_earn + $total_ads; $pt->array = implode(', ', $array); $pt->ads_array = implode(', ', $ads_array); $pt->page_url_ = $pt->config->site_url.'/transactions'; $pt->title = $lang->earnings . ' | ' . $pt->config->title; $pt->page = "transactions"; $pt->description = $pt->config->description; $pt->keyword = @$pt->config->keyword; $pt->currency = $currency; $pt->content = PT_LoadPage('transactions/content',array( 'CURRENCY' => $currency, 'ADS_LIST' => $ads_list, 'TOTAL_EARN' => $total_earn, 'TODAY_EARN' => $today_earn, 'MONTH_EARN' => $month_earn )); any help/guidance/suggestion is appreciated
This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=357878.0 Hi there, I am working on a PHP web form and I have a very simple situation I guess, I have a variable named: $MyVal When I do print_r($MyVal); To see whats inside, I get: SimpleXMLElement Object ( [0] => 8.23 ) Now I am assigning this variable into a session variable so that I can do calculations with it. So I assign: $_SESSION['MySesVal'] = $MyVal; But after assigning to session variable, when I do my calculations: $finalValue = $_SESSION['MySesVal'] * 4; I get 0. So is it because the actual $MyVal variable has some XML stuff as: SimpleXMLElement Object ( [0] => 8.23 ) So what is the right way to properly assign $MyVal variable to a session variable to do calculations. Please reply. All comments and feedbacks are always welcome. Thank you! I am facing problem to execute query by assigning NULL value to a variable and then executing query.In MySQL DB four fields Mobile,landline, pincode,dob are set as integer and date(for dob) respectively.The default is set as NULL and NULL option is selected as yes.All these fields are not mandatory.The problem is that when I edit the form my keeping the value as empty in DB these are saved as 0, 0 , 0 & 0000-00-00 inspite of Null. I have tried everything but still the defect persist. Please help me to come out of the problem The code, I have used: <?php //require_once 'includes/config.php'; $dbusername = $_POST['email']; $dbfirstname = $_POST['first_name']; $dblastname = $_POST['last_name']; //$dbmobile_number = $_POST['mobile']; if (isset($_POST['mobile'])) { $dbmobile_number = $_POST['mobile']; } else { $dbmobile_number = "NULL"; } $dblandline_number = $_POST['landline']; $dbdob = $_POST['dob']; if(isset($_POST['is_email'])) { $dbSubscribe_Email_Alert = '1'; } else { $dbSubscribe_Email_Alert = '0'; } if(isset($_POST['is_sms'])) { $dbSubscribe_SMS = 0; } else { $dbSubscribe_SMS = 0; } $dbAddress_firstname = $_POST['shipping_first_name']; $dbAddress_lastname = $_POST['shipping_last_name']; $dbAddress = $_POST['shipping_address']; $dbcity = $_POST['shipping_city']; $dbpincode = $_POST['shipping_pincode']; $dbstate = $_POST['shipping_state']; $dbcountry = $_POST['shipping_country']; echo "Welcome".$dbusername; //if($_POST['btnSave']) //if ($_POST['btnSave']) //{ //echo "Inside query loop"; $connect = mysql_connect("localhost","root","") or die("Couldn't connect!"); mysql_select_db("salebees") or die ("Couldn't find DB"); //$query = mysql_query("SELECT * FROM users WHERE username='$username'"); $query = mysql_query("update users set firstname = '$dbfirstname', lastname = '$dblastname', mobile_number = '$dbmobile_number', landline_number = '$dblandline_number', dob = '$dbdob', Subscribe_Email_Alert = '$dbSubscribe_Email_Alert', Subscribe_SMS = '$dbSubscribe_SMS', Address_firstname = '$dbAddress_firstname', Address_lastname = '$dbAddress_lastname', Address = '$dbAddress', city = '$dbcity', pincode = '$dbpincode', state = '$dbstate', country = '$dbcountry' where username = '$dbusername' "); header("location:my_account.php"); //} //else //{ //die(); //} ?> It's been a while since I've needed to whip anything substantial up from scratch, so my scripting is a little (lot) fast and loose (weird/inefficient) here. I'm trying to mock up a script that's essentially a quiz/survey. There are a handful of topics, each with a few screens of yes/no questions. At the end, it returns a list of recommendations based on the answers gathered. The script is posting back to itself. Using print_r ($_SESSION), it seems like all of the post values for the first screen of questions are being assigned to the session array as expected. When the second screen of questions is answered, their values are assigned as well, but the values for the first set go away completely. This continues through subsequent screens, with the values from the previous screen present and all others before missing. I'd really appreciate a look at my code to see if you tell me the cause or error(s). Thanks! <?php session_start; include('_config.php'); // database connect $dbc = mysqli_connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname); // set to section 1, page 1 if no values are in _POST array if (($_SERVER['REQUEST_METHOD'] == 'GET') || (!isset($_POST['section']))) { $section = 1; $page = 1; } else { // something was posted, so...set those values in session variable foreach($_POST as $key => $data) { $_SESSION[$key] = $data; } // debug: display contents of the session array print_r ($_SESSION); // which section and page? $section = (int) $_POST['section']; $page = (int) $_POST['next']; } // check if last topic $query = "SELECT * FROM hw_topics"; $data = mysqli_query($dbc, $query); if ($section == mysqli_num_rows($data)) { $last_section = true; } else { $last_section = false; } // get current topic name and info $query = "SELECT topic, display_name, pages_in_topic FROM hw_topics WHERE topic_id = '$section'"; $data = mysqli_query($dbc, $query); if (mysqli_num_rows($data) == 1) { $row = mysqli_fetch_array($data); $topic_display_name = $row['display_name']; $pages_in_topic = $row['pages_in_topic']; } // test if last page in topic $topic_pages = $row['pages_in_topic']; if ($page == $topic_pages) { $last_page_in_section = true; } else { $last_page_in_section = false; } // set form action (set to this script or to recommendations when last section is complete if (($last_section == true) && ($last_page_in_section == true)) { $form_action = $CFG->reccomend; } else { $form_action = $_SERVER['PHP_SELF']; } // get current page headline $query = "SELECT page_headline FROM hw_pages WHERE topic_id = '$section' AND page_number = '$page'"; $data = mysqli_query($dbc, $query); if (mysqli_num_rows($data) == 1) { // The headline row was found so display the headline $row = mysqli_fetch_array($data); $page_headline = '<h2>' . $row['page_headline'] . '</h2>'; } // Grab the question data from the database to generate the list and form fields $query = "SELECT question_id, question_number, question_text FROM hw_questions WHERE topic_id = '$section' AND page_id = '$page' ORDER BY question_number"; $data = mysqli_query($dbc, $query); $questions = array(); while ($row = mysqli_fetch_array($data)) { array_push($questions, $row); } include($CFG->includesdir.'/header.php'); ?> <div id="head"> <h1>Assessment<?php if (isset($topic_display_name)) { echo ': <em>' . $topic_display_name . '</em>'; } ?></h1> <p class="paging">Page <?php echo $page; ?> of <?php echo $pages_in_topic; ?></p> </div><!-- #head --> <div id="content"> <p class="instr">Please complete this survey. We'll generate a list of recommendations and resources for your organization.</p> <div id="questions"> <?php echo $page_headline; ?> <form method="post" action="<?php echo $form_action; ?>"> <table border="0" cellpadding="0" cellspacing="0"> <thead> <tr> <td></td> <td class="qtext"></td> <td class="qanswer">yes</td> <td class="qanswer">no</td> <td class="pad"></td> </tr> </thead> <?php if ($questions) { // display question rows foreach ($questions as $question) { echo '<tr>'; echo '<td class="qnumber">' . $question['question_number'] . '.</td>'; echo '<td class="qtext"><p>...' . $question['question_text'] . '</p></td>'; echo '<td class="qanswer"><div class="radio" id="box-yes"><input type="radio" value="yes" name="qid_' . $question['question_id'] . '" id="qid_' . $question['question_id'] . '" class="radio" /></div></td>'; echo '<td class="qanswer"><div class="radio" id="box-no"><input type="radio" value="no" name="qid_' . $question['question_id'] . '" id="qid_' . $question['question_id'] . '" class="radio"'; $field_name = 'qid_' . $question['question_id']; if (isset($_SESSION[$field_name])) { echo ' checked="checked"'; } echo ' /></div></td>'; echo '<td class="pad"></td>'; echo '</tr>'; } } else { echo '<tr>'; echo '<td colspan="3" class="qtext"><p>No questions found in the database for this page.</p></td>'; echo '<td class="pad"></td>'; echo '</tr>'; } ?> </table> <ul id="controls"> <?php if ($last_page_in_section == true) { $section++; $page = 1; } else { $page++; } echo '<input type="hidden" value="' . $section . '" name="section" />'; echo '<input type="hidden" value="' . ($page) . '" name="next" />'; if (($last_section == true) && ($last_page_in_section == true)) { echo '<li><input type="submit" value="Submit Answers and Get Recommendations" name="submit" id="submit" /></li>'; } else { echo '<li><input type="submit" value="Next Page" name="submit" id="next" /></li>'; } ?> </ul><!-- #controls --> </form> </div><!-- #questions --> <?php mysqli_close($dbc); include($CFG->includesdir.'/footer.php'); ?> This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=357747.0
First let me explain my code. This is later included in project_status.php] . In project_status.php] , I have included another file project_status_app.php which contains a HTML form.
<?php include 'inc_fn_header_and_menu.php'; function includeFile($file,$variable) { $var = $variable; include($file); } if (isset($_GET['id']) && $_GET['id']!="") { $pid = $_GET['id']; $_SESSION['pidForApproval'] = $_GET['id']; $query = 'SELECT * FROM `profile` WHERE pid ='.'\''.$pid.'\''; $result=mysqli_query($db,$queryToRetrievePP) or die("There are no records to display ... \n" . mysqli_error()); foreach ($result as $row) { $status = $row['status']; } } ...........some PHP and HTML code....... <div id="customerPurchaseApprovalForm"> <?php echo '<p>APPROVAL FOR CUSTOMER PURCHASE</p>'; $discountApprovalStatus = "Granted"; if ($discountApprovalStatus == "Granted") { includeFile("project_status_app.php",$highestannualvalue); } else { //......... } In project_status_app.php I am attempting to retrieve pidForApproval from the $_SESSION array. <?php // put your code here UPDATE `pp` SET `customer_purchase_remarks` = 'hahaha' WHERE `pp`.`id` = 207; if ($_SERVER['REQUEST_METHOD'] == 'POST') { include '../../inc/fastlogin.php'; $sql = "UPDATE pp SET customer_purchase_remarks ='{$_POST['remarkstxt']}' WHERE pp.pid='{$_SESSION['pidForApproval']}'"; $result = mysqli_query ( $fastdb, $sql ) ; if (mysqli_affected_rows($fastdb) != 1) { $_SESSION['err_cpa_rmks'] = "<p>Error while updating WHERE id='{$_SESSION['pidForApproval']}'</p>"; //echo "<p>Error while updating WHERE id='{$_POST['pidForApproval']}'</p>".mysqli_error($fastdb); } else { $_SESSION['suc_cpa_rmks'] = "<p>Records was updated successfully.</p>"; //echo "Records was updated successfully."; } header ("location: project_status.php?id="$_SESSION['pidForApproval']); exit(); } ?> When I load project_status.php, project_status_app.php is supposed to display the form. Once the user fills in the form the and the submit button has been pressed, the UPDATE statement is supposed to run and then it is supposed to navigate back to project_status.php?id=FA142. But the update is failing and the when the project_status.php is loaded back, the url looks like this http://localhost/fast/project_status.php?id= . The id is empty. It is supposed to be something like this http://localhost/fast/project_status.php?id=FA142. With the id being populated at the header ("location: project_status.php?id=".$_SESSION['pidForApproval']);
Missing some information. |