LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Passing variables in PHP???? (https://www.linuxquestions.org/questions/programming-9/passing-variables-in-php-50654/)

vous 03-19-2003 10:47 AM

Passing variables in PHP????
 
Hello All,

I am trying to input records in a database using a php script inputing data into a MySQL database running SuSE 8.1 and apache 1.3.x

The problem I have is that the html form doesn't pass values to my php page which is the one that inputs the data (obviously) into MySQL. I have checked the php code standalone and it works so I'm assuming it is a problem with passing the variables.

Here is the html form:

<HTML>
<HEAD>
<TITLE> Form Handling with PHP </TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<center>
<FORM METHOD=POST ACTION="segunda.php">
<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"><br><INPUT
TYPE="text" NAME="email"><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></BODY></HTML>


Here is the php code:

<?
$DBhost = "localhost";
$DBuser = "root";
$DBpass = "";
$DBName = "mydb";
$table = "information";
mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");

@mysql_select_db("$DBName") or die("Unable to select
database $DBName");

$sqlquery = "INSERT INTO $table
VALUES('$id','$name','$email','$opinion')";

$results = mysql_query($sqlquery);

mysql_close();

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>";
?>


Any thoughts? What am I doing wrong here....???

no2nt 03-19-2003 12:47 PM

You may not be doing anything wrong. Do a phpinfo() and make sure the register_globals setting is On.

If register_globals is Off, here's an example of what you'll need to do this to access the variables:

$sqlquery = "INSERT INTO $_POST[table] VALUES('$_POST[id]','$_POST[name]','$_POST[email]','$_POST[opinion]')";

Turning register_globals on is inherently dangerous for reasons I'll not go into discussion here, even though I will agree that it is annoying to have to write $_POST[var] for every variable...

m0rl0ck 03-19-2003 06:07 PM

Quote:

INSERT INTO $table
VALUES('$id','$name','$email','$opinion')";

Shouldnt that be something like:

"insert into $TABLE (id,name,email, opinion) VALUES('$id','$name','$email','$opinion')";



EDIT: corrected spelling

vous 03-20-2003 06:03 AM

Hi all...:-)

Finally got it working, thanks a lot for your tips that sent me on the right direction!

Anyways, I'll be posting the correct code below in just a sec, but before I wanted to add that this is a problem that you will encounter from php 4.2 and up and if you are new-comer to the MySQL/PHP/Linux family (like me) I can imagine how frustrating it is to go through a thousand php websites telling you how to do something, you try it 20 slighlty different ways but IT STILL DOESN"T WORK. Anyways, I hope this helps out a lot of newbies...

Here is the site that finally did it for me if you want to check out the WHY (you DEFINITLEY must!) this works in this manner in the new versions...

http://www.sitepoint.com/article/758/2

And here is the winning piece of code:

<?
$DBhost = "localhost";
$DBuser = "root";
$DBpass = "";
$DBName = "mydb";
$table = "information";
$id = $_REQUEST['id'];
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$opinion = $_REQUEST['opinion'];
$PHP_SELF = $_SERVER['PHP_SELF'];

mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");

@mysql_select_db("$DBName") or die("Unable to select
database $DBName");

/* $sqlquery = "INSERT INTO $_POST[$table] VALUES('$_POST[$id]','$_POST[$name]','$_POST[$email]','$_POST[$opin
ion]')"; */


$sqlquery = "INSERT INTO $table
VALUES('$id','$name','$email','$opinion')";

$results = mysql_query($sqlquery);

mysql_close();

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>";
?>


As you can see, the only change was the declaration of the variables in another manner...and that did it.

Cheers,

vous.

PS: Thanks to morlock and no2nt for your tips!!


All times are GMT -5. The time now is 01:57 AM.