LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-19-2003, 10:47 AM   #1
vous
Member
 
Registered: Mar 2003
Location: Macondo
Distribution: Mandrake 9.1, 10.1, SuSE 8.1 pro, 10.1, Red Hat 8.0/9.0
Posts: 380

Rep: Reputation: 30
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....???
 
Old 03-19-2003, 12:47 PM   #2
no2nt
Member
 
Registered: Aug 2001
Location: South Carolina, USA
Distribution: Redhat 8.0/Custom
Posts: 96

Rep: Reputation: 16
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...
 
Old 03-19-2003, 06:07 PM   #3
m0rl0ck
Member
 
Registered: Nov 2002
Distribution: A totally 133t distro :)
Posts: 358

Rep: Reputation: 31
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

Last edited by m0rl0ck; 03-19-2003 at 06:09 PM.
 
Old 03-20-2003, 06:03 AM   #4
vous
Member
 
Registered: Mar 2003
Location: Macondo
Distribution: Mandrake 9.1, 10.1, SuSE 8.1 pro, 10.1, Red Hat 8.0/9.0
Posts: 380

Original Poster
Rep: Reputation: 30
Cool

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!!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing variables to an include? Red Squirrel Linux - Software 4 09-23-2005 09:18 PM
PHP passing variables from url to ... latino Programming 3 08-31-2005 06:35 PM
Passing SSI variables between pages dubya Programming 2 07-05-2005 02:06 PM
PHP, passing a lot of variables to another page Robert0380 Programming 4 09-07-2003 04:44 AM
passing variables to sed jjfate Programming 8 07-31-2003 04:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration