Forum Moderators: coopster
I've just signed up so i'm new :)
Before I go on, let me say that i've searched the forum for what i want and couldn't find exactly what I want. So if its there, please dont lash out at me.
I'm new to PHP/MySQL.
What I'm trying to do is make a very simple guestbook. But thats the final product. Right now I'm doing the very basic things. Like reading from and writing to a DB.
I've successfully done that using some help from the web.
This page [webmasterworld.com] tells me how to pass variables between pages.
Let me explain what i'm trying to do now. I've made a page called enterdata.html which simply is a form. The following is the code.
<FORM METHOD=POST ACTION="1.php?act=write" enctype='multipart/form-data'>
<input type="hidden" name="id" value="NULL">
<TABLE>
<TR height="20"><TD colspan="2"><FONT SIZE="+0" face="verdana">
Below is a Sample Form for our PHP tutorial</TD></TR>
<TR height="50">
<td></td></TR>
<TR><TD align="left"><FONT SIZE="+0" face="verdana">
<b>Your Name <br>Your E-Mail Address</b></td>
<td><INPUT TYPE="text" NAME="name" size="20"><br>
<INPUT
TYPE="text" NAME="email" size="20"><p></TD>
</TR>
<tr><td colspan="2"><center>
<SELECT NAME="opinion">
<option value="is great">I like your site</option>
<option value="is OK">Your Site is OK</option>
<option value="is horrible">Your Site is
horrible</option>
</SELECT><p><INPUT TYPE="submit" value="Tell Us"></td></tr>
</TABLE></FORM>
Now, this data is being passed on to 1.php
the code for the PHP page is as follows
<?PHP
// DATABASE INFORMATION
$DBhost = "****";
$DBuser = "****";
$DBpass = "****";
$DBName = "****";
$table = "information";// Attempt to connect to the DB using the information provided or die!
mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
mysql_select_db("$DBName") or die("Unable to select database $DBName");$function = $_GET['act'];
switch($function)
{
case 'read':
read();
break;
case 'write':
write();
break;
}/* FUNCTION: READ
Reads information FROM the database and displays it as text.
*/
Function read()
{
// Making use of the variable declared at the start of this file.
Global $table;// Select all the rows of the table.
$sqlquery = "SELECT * FROM $table";// The result of the above query is stored in $result
$result = mysql_query($sqlquery);// Get the number of rows from the result of the SQL query that was run
// on the DB.
$number = mysql_num_rows($result);$i = 0;
// If the number of rows in the result was nil, then display the following message.
if ($number < 1)
{
print "<DIV ALIGN=\"CENTER\">There Were No Results for Your Search</DIV>";
}
// Otherwise..
else
{
// Display each row of the obtained table. This will display the name and email address.
while ($number > $i)
{
$thename = mysql_result($result,$i,"name");
$theemail = mysql_result($result,$i,"email");
print "<p><b>Name:</b> $thename<br><b>E-Mail: </b>$theemail</p>";
$i++;
}
}
} // END read()Function write()
{
Global $table;$sqlquery = "INSERT INTO $table VALUES('" . $_GET["id"] . "','" . $_GET["name"] . "','" . $_GET["email"] . $_GET["opinion"] . "','"."')";
$results = mysql_query($sqlquery);
print "<HTML><TITLE> PHP and MySQL </TITLE><BODY BGCOLOR=\"#FFFFFF\"><center><table border=\"0\" width=\"500\"><tr><td>";
print "<p><font face=\"verdana\" size=\"+0\"> <center>You Just Entered This Information Into the Database<p><blockquote>";
print "Name : $name<p>E-Mail : $email<p>Opinion: $opinion</blockquote></td></tr></table></center></BODY></HTML>";
}//Close the connection to the MySQL DB
mysql_close();?>
The objective is to use the same page for both reading and writing from/to the DB. if
?act=read
is added at the end of the URL, the file does the reading successfully.
I'm having trouble writing to it. As far as i can see the writing code is correct because if i take it out of the function and put it into another file, it writes properly.
All help is appreciated. I hope i've explained sufficiently.
Not so bad. You're using some good concepts, there, but maybe too many. How about a more simple approach?
You used:
$function = $_GET['act'];switch($function)
{
case 'read':
read();
break;
case 'write':
write();
break;
}
Then wrote the page you wanted by enclosing the code within the two functions.
I would suggest eliminating the functions, and replacing them with a more direct switch:
switch ($act) {case "write":
... write stuff ...
break;case "read":
... read stuff ...
break;}
Since the form you are using uses "method=post", you are having some trouble grabbing the form element values from the URI. (You used $_GET["x"] instead of $_POST["x"])
You could actually do away with the form page, including its contents in the PHP file in the section:
if (!$act) {
... form goes here ...
}
else {
switch ($act) {
... read/write go here ...
}
}
Good start!
The reason i used functions instead of adding the code there is because:
1- its neater
2- I was thinking of adding more functions which would make it much easier later on
But yes, the concept is the same. The problem was the "POST.." as you said, but there was another problem, i had to add
Global $id, $name, $email, $opinion;
I want to put the form in the same page. I'll look into it before i start asking questions. Thanks for the help.
I have a question though, isn't GET to "get information" and POST to "post information"?
I'm used to java so i have so make silly mistakes like not declaring $table to be global within the function (in java you would just use $table in the function and an equivalent of Global $table outside)
First:
$_GET["x"] refers to an element in the array of elements passed using the "GET" method.
Likewise, $_POST["x"] refers to an element in the array of elements passed using the "POST" method.
These two methods differ in both how the elements are passed and in how the server handles them.
You will see the elements of a "POST" array in the URL upon form submission. You will not see the "GET" elements, as they are sent as part of the session data. (I am willing to be corrected on these specifics :)
Second:
While PHP does use global and local variables, it's a little different that in Java because it's document-specific. With Java, there are variables within the app that may be modified by variables in the document, where in PHP the document is everything. (There are deeper levels that require the use of globals, but in this instance there is no need.)
When the value is passed to the PHP doc, is becomes useable in the same way as a typical global variable, in that it doesn't matter whether you use it within a function or outside of a function ... it's the same variable.
In fact, depending on whether your PHP installation has "register_globals" enabled, you may not need to reference the variable by its array.
If "register_globals" is "On" (see php.ini or use an Apache .htaccess file to turn it on for that directory), you can simply refer to the variable:
somepage.php?var1=sometext&var2=morestuff
<?
echo "VAR1: ".$var1."<br />\n";
echo "VAR2: ".$var2."<br />\n";
?>
If "register_globals" is "Off", you need to know what method is being used to pass the variables (in the above example, it's POST):
<?
echo "VAR1: ".$_POST["var1"]."<br />\n";
echo "VAR2: ".$_POST["var2"]."<br />\n";
?>
or
<?
echo "VAR1: ".$HTTP_POST_VARS["var1"]."<br />\n";
echo "VAR2: ".$HTTP_POST_VARS["var2"]."<br />\n";
?>
That makes it a bit clear.
If i had 3 pages A(form page) B(a confirmation/thankyou page) and C(display)
and say someone fills a form in A upon submitting hes shown B how do you forward him to C with a delay?
I tried to use header(Location..) but the problem with that is the fact that i've already used a header. Is there any other way?
If you want to process data and then redirect, header() is your ticket. But in your case, you want to show page B. That means you have already sent output and you cannot send additional header information. One you put a period in a sentence. What follows needs to be a new sentence ;-)
To show the page and then redirect, you need a client-side refresh using meta tags or Javascript. Keep in mind that if the user has JS turned off, it won't work.
Tom
[edit]was thinking "meta tags and JS" and just wrote JS. doh![/edit]
[edited by: ergophobe at 6:17 pm (utc) on Sep. 28, 2004]
I need to correct/clarify a bit about global variables in PHP. Dhruv, you were correct to declare the global variables inside your function, if your PHP installation is not set for register_globals=On.
While variables can pass into and out of a function when they are defined outside of the function, if they are defined inside the function, they do need to be declared as globals if you want to use them outside the function, afterwards. (whew!)
I have been experimenting with your suggested approach using functions to write common blocks of code, and have found the above to be true on a system with register_globals=Off (the default php.ini setting).
So:
=====
$var1="something";
function useVars() {
echo $var1;
}
useVars();
=====
echoes "something", and:
=====
function useVars() {
$var1="somethingelse";
}
useVars();
echo $var1;
=====
does NOT echo, as $var1 needs to be declared as a global within the function:
=====
function useVars() {
global $var1;
$var1="somethingelse";
}
useVars();
echo $var1;
=====
echoes "somethingelse".
Just like in Java! :) Sorry for any confusion.