Displaying dynamic information php - php

Here is my code
<?php
session_start();
include 'header.php';
$getUserInfo = mysql_query("SELECT * FROM Campaigns WHERE cid = '$cid'");
$userinfo = mysql_fetch_assoc($getUserInfo);
$cid = $getUserInfo['cid']; //ID
$CName = $getUserInfo['CName']; //Name
$CDesc = $getUserInfo['CDesc']; //Description
$CAmt = $getUserInfo['CAmt']; //Rate
$CReqs = $getUserInfo['CReqs']; //Requirements
?>
<html>
<body>
<h3><?php echo $cid;?>, <?php echo $CName;?></h3>
<p><?php echo $CDesc;?>
</body>
</html>
And for every new item in the php database i want it to be displayed dynamically.
But for some reason none of the infomation is being displayed. Is it because i'm using getUserInfo

Replace
$cid = $getUserInfo['cid']; //ID
$CName = $getUserInfo['CName']; //Name
$CDesc = $getUserInfo['CDesc']; //Description
$CAmt = $getUserInfo['CAmt']; //Rate
$CReqs = $getUserInfo['CReqs']; //Requireme
with
$cid = $userinfo ['cid']; //ID
$CName = $userinfo ['CName']; //Name
$CDesc = $userinfo ['CDesc']; //Description
$CAmt = $userinfo ['CAmt']; //Rate
$CReqs = $userinfo ['CReqs']; //Requireme
You are reading from the wrong variable
Further suggestions:
Make sure you actually get a result from MySQL:
var_dump($getUserInfo);
var_dump($userinfo);
If not, check why not with echo mysql_error();
Make sure you have the right keys in your array (PHP variables are case sensitive)

Related

PHP/MYSQL - How do i assign data fetched from the database(stored in variables) to a session variable?

I am having a bit of an issue passing array variables that are fetched from the database and stored in variables and passing them into session variables to use all over the site.
Code:
$rid=$_SESSION['SESS_MEMBER_ID'] ;
$recquery = 'SELECT * FROM clinic_receptionist,clinic_table where clinic_receptionist.recep_clinic_id = clinic_table.ID AND recep_id ='.$rid;
$recresult = mysqli_query($conn,$recquery);
if(mysqli_num_rows($recresult)>0)
{
while($row = mysqli_fetch_assoc($recresult))
{
$cid = $row['clinic_id'];
$cname = $row['clinic_name'];
$name = $row['recep_full_name'];
$phone = $row['recep_mobile'];
$email = $row['recep_email'];
$address = $row['recep_address'];
$gender = explode(",",$row['recep_gender']);
$dob = $row['recep_dob'];
$doj = $row['recep_doj'];
}
}
?>
The above code is where the select query is happening. I wanted to know how do I assign $cid and $cname to a $_SESSION variable in different pages. Any help would be much appreciated. Thanks
CODE:
<?php session_start();
include('header.php');
include('menu.php');
include('config.php');
$appquery = 'SELECT * FROM appointment_table,clinic_table where clinic_table.ID = appointment_table.clinic_id';
$appresult = mysqli_query($conn,$appquery);
if(mysqli_num_rows($appresult)>0)
{
while($row = mysqli_fetch_array($appresult))
{
$_SESSION['cid'] = $row['ID'];
$_SESSION['cname'] = $row['clinic_name'];
$ptdate = $row['date'];
$pttime = $row['time'];
$ptname = $row['pt_name'];
$ptphone = $row['pt_ph_num'];
$ptemail = $row['pt_email_id'];
}
}
?>
The above code is the place from where I want to receive the id and clinic name.
<?php
// Start the session
session_start();
//Assigning the values to $_SESSION
$_SESSION['cid'] = $row['clinic_id'];
$_SESSION['cname'] = $row['clinic_name'];;
?>
session_start
You need to call session_start() on each page to initialize the session, of course; after that you can just
$_SESSION['cid'] = $cid;
$_SESSION['cname'] = $cname;
to store those values in $_SESSION.
By the way, someone's going to tell you to use a prepared statement for that query, so it might as well be me.
Perhaps an illustration is in order.
page1.php:
<?php
session_start(); // find session, or create new one
include('header.php');
include('menu.php');
include('config.php');
$appquery = 'SELECT * FROM appointment_table,clinic_table where clinic_table.ID = appointment_table.clinic_id';
$appresult = $conn->query($appquery);
if(($rowcount = $appresult->num_rows()) > 0)
{
// create arrays to pass data
$cids = array($rowcount);
$cnames = array($rowcount);
$i = 0;
while($row = $appresult->fetch_array())
{
// add values to arrays
$cids[$i] = $row['ID'];
$cnames[$i] = $row['clinic_name'];
$i++;
}
// add arrays to session
$_SESSION['cids'] = $cids;
$_SESSION['cnames'] = $cnames;
}
page2.php:
<?php
session_start(); // find session, or create new one
include('header.php');
include('menu.php');
include('config.php');
// retrieve arrays from session
$cnames = $_SESSION['cnames'];
$cids = $_SESSION['cids'];
// ... and print them
$count = count($cids);
for ($i = 0; $i < $count; $i++)
{
echo "cid = " . $cid[$i] . "; cname = " . $cname[$i] . "<br>";
}
page1.php performs the query and stores cid and cname values in arrays which are then stored in $_SESSION. page2.php retrieves the arrays from $_SESSION and echoes them out. In production, of course, it would be considered more efficient to run the query on the page where it is displayed, and only pass key values between pages.
I've also used object syntax instead of procedural syntax for mysqli, just to show what it looks like. As usual, I leave error handling as an exercise for the reader.

file_get_contents with query string to get data for particular id

I have two files, register.php, query.php. I want functionality like from register.php page I can display the data for particular id, which is processed in query.php.
register.php
echo file_get_contents('query.php?id=1');
echo file_get_contents('query.php?id=2');
.
.
echo file_get_contents('query.php?id=14');
echo file_get_contents('query.php?id=15');
query.php
if (isset($_GET['id'])) {
$id = $_GET['id'];
switch ($id) {
case 1:
$query1 = mysql_query("select * from tblname where id=$id");
while ($row1 = mysql_fetch_array($query1)) {
$file = $row1['type'];
$file_type_name = "qsg";
$header = $row1['title'];
$paragraph_description = $row1['description'];
include '../../../print.php';
}
// break;
case 2:
$query1 = mysql_query("select * from tblname where id=$id");
while ($row1 = mysql_fetch_array($query1)) {
$file_type = $row1['type'];
$file_name = "email_intro";
$header = $row1['title'];
$abstract = $row1['description'];
include '../../../email.php';
}
// break;
// default:
// echo "other";
}
}
I have to show data from the database for all id. I am not getting how should I get data for particular id which may vary, and show it on register.php page.
Can anyone help me please?

Passing JSON to Array in PHP

I'm working on a filter in which results are filtered right away, I'm wondering if that may be the cause of the problem so I thought I would ask and see if anyone could give me a pointer on how to proceed.
<script>
var services = [
<?php
//Variables for connecting to your database.
//These variable values come from your hosting account.
$hostname = "###";
$username = "###";
$dbname = "###";
//These variable values need to be changed by you before deploying
$password = "###";
$usertable = "###";
$url = "permalink";
$title = "Address";
$amount = "rent";
$id = "id";
$status = "Beds";
$nonprofit = "Address";
//Connecting to your database
mysql_connect($hostname, $username, $password) OR DIE ("He's dead Jim");
mysql_select_db($dbname);
//Fetching from your database table.
$query = "SELECT * FROM $usertable";
$result = mysql_query($query);
if ($result) {
while($row = mysql_fetch_array($result)) {
$url = $row["$url"];
$title = $row["$title"];
$amount = $row["$amount"];
$id = $row["$id"];
$status = $row["$status"];
$nonprofit = $row["$nonprofit"];
echo '{"permalink": "';
echo "{$url}";
echo '",';
echo '"title": "';
echo "{$title}";
echo '",';
echo '"amount":';
echo "{$amount}";
echo ',';
echo '"id": "';
echo "{$id}";
echo '",';
echo '"status": "';
echo "{$status}";
echo '",';
echo '"address": "';
echo "{$address}";
echo '",';
echo '},';
}
}
?>
]
//]]>
</script>
<script id="template" type="text/html">
<a title="{{title}}" href="{{permalink}}">
<div class="fs_box hide-for-small-down">
<div class="fs_left">
<span class="fs_head">{{title}}</span>
<span class="fs_id"><img src="images/{{id}}.jpg" width="75%" height="75%" onError="this.onerror=null;this.src='images/logo.png';"></span>
<span class="fs_status">{{status}}</span>
<span class="fs_disc">{{address}}</span>
</div>
<div class="fs_price">${{amount}}+</div>
<div class="clear"></div>
</div>
</a>
</script>
I'm expecting it to produce a bunch of results that then are filtered criteria which are elsewhere in the page.
When I try it currently just as a php code it outputs fine. However, when I try it in the php file that this should go in it produces nothing. Or does it dislike being in a script?
Thanks for any help!
You can use json_decode and json_encode to turn an array to json and json back to an array.
Also someone will probably mention that you should not be using the mysql_* functions in PHP as they are depreciated.
Something like this:
<?php
//Variables for connecting to your database.
//These variable values come from your hosting account.
$hostname = "###";
$username = "###";
$dbname = "###";
//These variable values need to be changed by you before deploying
$password = "###";
$usertable = "###";
$url = "permalink";
$title = "Address";
$amount = "rent";
$id = "id";
$status = "Beds";
$nonprofit = "Address";
//Connecting to your database
mysql_connect($hostname, $username, $password) OR DIE ("He's dead Jim");
mysql_select_db($dbname);
//Fetching from your database table.
$query = "SELECT * FROM $usertable";
$result = mysql_query($query);
if ($result) {
$results = array()
while($row = mysql_fetch_array($result)) {
$results[] = $row;
}
$json = json_encode($results);
}
?>
]
<script>
var services = <?php echo $json; ?>;
</script>
This would give you a json object to use to render in your script.
What extension is the file you are saving?
If it's not .php or an extension set to render php, then you'll just have the code show up as test.
You might want to pull out the "die" statement after the db connect. This looks like you are running php in a .js file so you probably want the entire file to write out rather than stop because you couldn't connect to the database (or at least give 0 results, maybe a warning)

storing username from session to database while form submission using php

I have a simple question,
I have a login and workspace area.
After the user logs in It shows the username of the logged in user at workplace as what I wanted. Now my problem is when user finish filling form available in his workspace the form is then stored in database also i need the username that is coming from session also get stored to the database.
here is code that is storing username and maintaining session after user reach at workspace after login:
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/MainProject/connect/auth.php');
session_start();
?>
The final version of the updated insert file :
//This code is included to check session and store username
<?php
require_once('..\connect\auth.php');
// session_start();
$usern = $_SESSION['SESS_FIRST_NAME'];
?>
<?php
mysql_connect('localhost','root','');
mysql_select_db('main_project') or die (mysql_error());
if(isset($_POST['WID'])){
for ($ix=0; $ix<count($_POST['WID']); $ix++)
{
$WID = mysql_real_escape_string(#$_POST['WID'][$ix]);
$website = mysql_real_escape_string(#$_POST['website'][$ix]);
//var_dump("<pre>", $_POST['cat']); die(); // Debugger for checking cat counter.
// $cat = implode(",", mysql_real_escape_string($_POST['cat'][$ix]));
if(is_array(#$_POST['cat'][$ix]))
$cat = mysql_real_escape_string(implode(',', #$_POST['cat'][$ix]));
else
$cat = mysql_real_escape_string(#$_POST['cat'][$ix]);
$email = mysql_real_escape_string(#$_POST['email'][$ix]);
$cform = mysql_real_escape_string(#$_POST['cform'][$ix]);
$contactp = mysql_real_escape_string(#$_POST['contactp'][$ix]);
$contacts = mysql_real_escape_string(#$_POST['contacts'][$ix]);
$fax = mysql_real_escape_string(#$_POST['fax'][$ix]);
$Ctype = mysql_real_escape_string(#$_POST['Ctype'][$ix]);
$usern = mysql_real_escape_string(#$_POST['usern'][$ix]);
$sql_res = mysql_query("INSERT INTO website_01data (WID,website,cat,email,cform,contactp,contacts,fax,Ctype,TimeStamp,usern)
VALUES ('".$WID."', '".$website."', '".$cat."', '".$email."','".$cform."', '".$contactp."', '".$contacts."', '".$fax."', '".$Ctype."', Now(), '".$usern."' )");
$sql_res = mysql_error();
}//end for..
echo "<p><span style=\"color: red;\">Thank You; your records are sent to database. DO NOT REFRESH THE PAGE or data will be sent again.</span></p>";
}
?>
In the logging in process, you must store your username in a session
$_SESSION['username'] = $username;
in the process of saving the form, you can call session_start(); and get the session using
$tobeinserted = $_SESSION['username'];
I believe
Remove comment in session start.
Use this.
//This code is included to check session and store username
<?php
require_once('..\connect\auth.php');
session_start();
$usern = $_SESSION['SESS_FIRST_NAME'];
?>
<?php
mysql_connect('localhost','root','');
mysql_select_db('main_project') or die (mysql_error());
if(isset($_POST['WID'])){
for ($ix=0; $ix<count($_POST['WID']); $ix++)
{
$WID = mysql_real_escape_string(#$_POST['WID'][$ix]);
$website = mysql_real_escape_string(#$_POST['website'][$ix]);
//var_dump("<pre>", $_POST['cat']); die(); // Debugger for checking cat counter.
// $cat = implode(",", mysql_real_escape_string($_POST['cat'][$ix]));
if(is_array(#$_POST['cat'][$ix]))
$cat = mysql_real_escape_string(implode(',', #$_POST['cat'][$ix]));
else
$cat = mysql_real_escape_string(#$_POST['cat'][$ix]);
$email = mysql_real_escape_string(#$_POST['email'][$ix]);
$cform = mysql_real_escape_string(#$_POST['cform'][$ix]);
$contactp = mysql_real_escape_string(#$_POST['contactp'][$ix]);
$contacts = mysql_real_escape_string(#$_POST['contacts'][$ix]);
$fax = mysql_real_escape_string(#$_POST['fax'][$ix]);
$Ctype = mysql_real_escape_string(#$_POST['Ctype'][$ix]);
//$usern = mysql_real_escape_string(#$_POST['usern'][$ix]);
$sql_res = mysql_query("INSERT INTO website_01data (WID,website,cat,email,cform,contactp,contacts,fax,Ctype,TimeStamp,usern)
VALUES ('".$WID."', '".$website."', '".$cat."', '".$email."','".$cform."', '".$contactp."', '".$contacts."', '".$fax."', '".$Ctype."', Now(), '".$usern."' )");
$sql_res = mysql_error();
}//end for..
echo "<p><span style=\"color: red;\">Thank You; your records are sent to database. DO NOT REFRESH THE PAGE or data will be sent again.</span></p>";
}
?>

web Development, PHP display variable from SQL as something else

Ok So i have an PHP page and i have a database.
In my database i have a table with a Field one of the fields is called accounttype it is enum('n', 'm', 's')
I am trying to display on my PHP page if the user is N it should say Normal User if the user is E Expert user or S Super user...
How do i go about doing this?
Top of PHP Page
<?php
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM members WHERE id='$id' LIMIT 1");
while($row = mysql_fetch_array($sql)){
$phone = $row["phone"];
$country = $row["country"];
$state = $row["state"];
$city = $row["city"];
$accounttype = $row["accounttype"];
$bio = $row["bio"];
}
?>
Where i am trying to display on the page This is the code. Right now it just puts a blank space.
<span class="admin">Edward</span>
<span class="date">March 19, 2048</span>
<span class="tag"><?php echo "$accounttype"; ?></span>
<span class="comment">166 comments</span>
picture
http://i.stack.imgur.com/KXu9A.png
first make a connection, than dont make a while, make a if like this
if($row = mysql_fetch_array($sql)){
$phone = $row["phone"];
$country = $row["country"];
$state = $row["state"];
$city = $row["city"];
$accounttype = $row["accounttype"];
$bio = $row["bio"];
}
and than
$speaking_type = null;
switch($accounttype) {
case 'n':
$speaking_type = 'Normal User';
break;
case 'm':
$speaking_type = 'Expert User';
break;
case 's':
$speaking_type = 'Super User';
break;
default:
$speagink_type = 'none';
//throw new Exception('unsupported account type '.$accounttype);
}
echo $speaking_type;
I think the problem is your scope. Your variables are defined within the while-loop, and so they are unknown further in the document. Try instantiating them on top (before the while-loop) like this:
$phone = null;
$country = null;
$state = null;
$city = null;
$accounttype = null;
$bio = null;
Than the variables will be known outside the while and the values will be remembered when you print them.
I thought u didn't connect to the database first .use following code to the connect with your credentials.That's why you are seeing a blank space
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
mysql_close($con);

Categories