LinuxQuestions.org
Help answer threads with 0 replies.
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 02-20-2003, 07:59 AM   #1
Greenman
Member
 
Registered: Jan 2003
Posts: 34

Rep: Reputation: 15
Post Php MySQL Form?


Hi,

I have got the following code to try and insert data into MySQL database but I am not having much luck.
Has anyone any ideas as to what’s I am doing wrong.
I am not getting errors when I try click enter info.


Ta,
g


<html>

<body>



<?php
$db = mysql_connect("localhost", "user", "passwd")
or die ("cant connect to the DB");
print ("connected to database successfully");

mysql_select_db("test",$db);

?><html><br><br><?php


if ($submit) {

$sql = "INSERT INTO employees (first,last,address,position)
VALUES ('$first','$last','$address','$position')";

$result = mysql_query($sql)or die(mysql_error());

echo "Thank you! Information entered.\n";

} else{



// display form



?>



<form method="post" action="<?php echo $PHP_SELF?>">

First name:<input type="Text" name="first" value="<?php echo $first ?>"><br>


Last name:<input type="Text" name="last"><br>

Address:<input type="Text" name="address"><br>

Position:<input type="Text" name="position"><br>

<input type="Submit" name="submit" value="Enter information">

</form>


<?php



} // end if


?>



</body>



</html>

Last edited by Greenman; 02-20-2003 at 08:02 AM.
 
Old 02-20-2003, 04:42 PM   #2
rhuser
Member
 
Registered: Jan 2003
Posts: 55

Rep: Reputation: 15
hi there

i had problems before with this, and the way i solved this was to download the rpm for the link between php and mysql. i think it is called php-mysql.
A module for PHP applications that use MySQL databases.

i was using Red hat, so i obtained php-mysql from rhn.redhat.com.
php-mysql-4.2.2-8.0.7.i386.rpm

------
The php-mysql package contains a dynamic shared object that will add
MySQL database support to PHP. MySQL is an object-relational database
management system. PHP is an HTML-embeddable scripting language. If
you need MySQL support for PHP applications, you will need to install
this package and the php or mod_php package.
 
Old 02-21-2003, 01:34 AM   #3
Greenman
Member
 
Registered: Jan 2003
Posts: 34

Original Poster
Rep: Reputation: 15
Hi rhuser,

I have seen that problems before also and have database connectivity and can call data from the MySQL DB happily
Also with some simple script I can also add to the data base
But when I try and enter data in the MySQL via a form no Cigar!!

This is an example of some code that works for me



$insert = "INSERT INTO employees (first,last)
VALUES ('Morgan','Andersson')";
mysql_query($insert) or die ("Could not add data to the table");
$result = mysql_query($sql);

Last edited by Greenman; 02-21-2003 at 01:36 AM.
 
Old 02-26-2003, 05:58 AM   #4
Greenman
Member
 
Registered: Jan 2003
Posts: 34

Original Poster
Rep: Reputation: 15
Anyone have any ideas what is wrong with this code folks?

I am trying to enter info into a MySQL DB via a Form

Last edited by Greenman; 02-26-2003 at 06:06 AM.
 
Old 02-27-2003, 11:53 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Re: Php MySQL Form?

Your problem may be caused by having the option "register_globals = Off" in your php.ini file (somewhere in /etc/ or /etc/php /etc/php4 or so). If you set it to "On" (maybe you need to restart apache after editing php.ini), I think your script will work.

If you don't want to set "register_globals = On" (e.g. for reasons of better security), or if you can't because it's on some provider's server, add some lines to your script in order to fill the variables. Like this:

Code:
/* . . . */

$submit = $HTTP_POST_VARS['submit'];
$first = $HTTP_POST_VARS['first'];
$last = $HTTP_POST_VARS['last'];
$address = $HTTP_POST_VARS['address'];
$position = $HTTP_POST_VARS['position'];

if ($submit) {
  $sql = "INSERT INTO employees (first,last,address,position) 
	VALUES ('$first','$last','$address','$position')";

  $result = mysql_query($sql)or die(mysql_error());

/* . . . */
If you use PHP 4.1.0 or later it probably better to use $_POST instead of $HTTP_POST_VARS.

Good luck.
 
Old 02-27-2003, 01:35 PM   #6
Greenman
Member
 
Registered: Jan 2003
Posts: 34

Original Poster
Rep: Reputation: 15
Hi,

Thanks for the advice!!!!

This was the solution to my problems!!

if ($_POST["submit"]) {

Nice

G
 
  


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
mysql-server4.1 and mod php-mysql conflig kernelvn *BSD 0 06-18-2005 11:52 AM
Problem getting PHP to recognize MySQL, Using PHP 4.0 and MySQL 4.0.20 d2army Programming 4 06-27-2004 08:54 PM
php4 mysql, installation, php-pages with mysql info stay empty dnla Linux - Software 2 03-14-2004 02:54 PM
Apache Mysql Php: mysql with php doesn't work breakerfall Linux - Networking 6 12-27-2003 08:59 PM
install a php forum with mysql. dkc_ace Linux - General 28 02-04-2003 04:55 PM

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

All times are GMT -5. The time now is 10:42 AM.

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