PHP retrieving data from database.

Codes here !

Moderators: macek, egami, gesf

Tryah85
New php-forum User
New php-forum User
Posts: 10
Joined: Wed May 30, 2012 12:20 am

PHP retrieving data from database.

Postby Tryah85 » Fri Sep 28, 2012 11:34 pm

I am looking at an example database tutorial I found about how a simple C.R.U.D system works with PHP/MYSQL database. I have gotten it to work! However I am getting a little lost in the php manual on php.net about the super global variable $_SERVER['PHP_SELF']. It states "is an array containing information such as headers, paths, and script locations."

So does that mean that whatever data that has just been inputted will be in $_SERVER['PHP_SELF']... in array form?

How do you retrieve data from previous queries that happened before submitted data from $_SERVER['PHP_SELF']? Maybe an SQL stament using a mysql_result($result) command?

Maybe I am making this more confusing than it should be.

Thanks in advance

Todd :)

rwhite35
New php-forum User
New php-forum User
Posts: 13
Joined: Thu Sep 27, 2012 10:55 am

Re: PHP retrieving data from database.

Postby rwhite35 » Tue Oct 02, 2012 5:52 pm

$_SERVER['PHP_SELF'] simply refers to the current page with all its header, links, etc.. If you had a form and wanted to reload the page, you would process the field data before you generate any HTML. You could use this approach when you want to maintain the users input, maybe for a required fields check or some other pre submission routine. $_SERVER['PHP_SELF'] could be used like this:

At the top of your page, check for the super global $_POST array:

Code: Select all

<?php
if (!empty($_POST)){
  foreach($_POST as $key=>$value){
      ${$key} = $value;
     //instantiates $users = $_POST['users']
    }
}
?>
<html>
<head></head>
<body>


Then later in your HTML form:

Code: Select all

 <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="users" size="24" value="<?php echo $users;?>">
</form>

PaanMoody
New php-forum User
New php-forum User
Posts: 1
Joined: Wed Oct 03, 2012 5:12 pm

Re: PHP retrieving data from database.

Postby PaanMoody » Wed Oct 03, 2012 5:27 pm

hi, can you help me with my php coding. i think i stuck where my php cannot retrieve back the data from phpmyadmin.this is my coding. an error occur which is

Notice: Undefined index: channel in C:\Program Files (x86)\EasyPHP-12.0\www\Timetable\timetable.php on line 34
Notice: Undefined index: time in C:\Program Files (x86)\EasyPHP-12.0\www\Timetable\timetable.php on line 34

Notice: Undefined index: program in C:\Program Files (x86)\EasyPHP-12.0\www\Timetable\timetable.php on line 34

<?php

$con = mysql_connect("localhost","root","12345");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("test", $con);

$result = mysql_query("SELECT * FROM test") or die(mysql_error());




/* declare a 2d array accessible by $arr["channel"]["time"].
all programs are initialised to nothing. */

$arr = array(
"Time" => array("7.30", "8.00", "8.30", "9.00", "9.30", "10.00", "10.30"),
"Ten" => array("7.30" => "", "8.00" => "", "8.30" => "", "9.00" => "", "9.30" => ""),
"Nine" => array("7.30" => "", "8.00" => "", "8.30" => "", "9.00" => "", "9.30" => ""),
"Prime" => array("7.30" => "", "8.00" => "", "8.30" => "", "9.00" => "", "9.30" => ""),
"SBS" => array("7.30" => "", "8.00" => "", "8.30" => "", "9.00" => "", "9.30" => ""),
"ABC" => array("7.30" => "", "8.00" => "", "8.30" => "", "9.00" => "", "9.30" => ""),
);

/* the programs would be loaded into the array from here. */



/* this step would be updating the database.
for now we will just set the element chosen in the html page. */
$arr[$_POST['channel']][$_POST['time']] = $_POST['program'];

echo "<table border=\"1\">\n";

/* loop through the channels */
foreach($arr as $channel => $value)
{
echo "<tr>";
echo "<td>" . $channel ."</td>";

/* and the times */
foreach($value as $time)
{
echo "<td>" . $time . "</td>";
}
echo "</tr>\n";
}

?>


Return to “mySQL & php coding”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest