Jump to content

Adding content to a database from a form


unplugged_web

Recommended Posts

I have a form that is suposted to send the databse of the files to a database. I have access to a database from a config.php file:

<?#------ Database Details ----#$host = "xx.xx.xx.xxx";$dbname = "xxxxxxxxx_xxxxxxxxx";$dbuser = "xxxxxxxxx_xxxxxxx";$dbpass = "dbpass";#-- Paths Used in Codeing ---#$root_path = "/home/software/public_html/xxxxxxxxi/";$http_path = "http://www.xxxxxxxxxx.com/xxxxxxxx/";?>

I have also found a database.class.php file in the same folder:

<?phpclass database{	//=====================================================================================	function database()//Constructor to connect to the database	{		include("config.php");		$DB_HOST	=	$host; // hostname		$DB_NAME	=	$dbname; // database name		$DB_USER	=	$dbuser; // database username		$DB_PASS	=	$dbpass; // database password		$dbh = mysql_pconnect ($DB_HOST, $DB_USER, $DB_PASS);		mysql_select_db($DB_NAME);		return $dbh;	  }//function database()	//=====================================================================================	//=====================================================================================	function get_rsltset($mysql) //Retrieves a resultset based on the query	{		//	$result = mysql_query($sqlqry);			if (! ($result = mysql_query ("$mysql")))//$mysql is for the query			{				  $men = mysql_errno();				  $mem = mysql_error();				  echo ("<h4>$mysql  $men $mem</h4>");				  exit;			}		else			{			$xx = 0;				  while ( $row = mysql_fetch_array ($result) ) 				  {					$rsltset[$xx] = $row;				$xx++;				  }				  mysql_free_result($result);				  return $rsltset;  			} 	}//function get_rsltset()	//=====================================================================================	//=====================================================================================	function get_a_line($sqlqry)//Retrieves a single record based on the query	  {	 		if (! ($result = mysql_query ("$sqlqry")))	 		{				$men = mysql_errno();				$mem = mysql_error();				echo ("<h4>$sqlqry  $men $mem</h4>");				exit;	 		}		$line = mysql_fetch_array ($result);	 		mysql_free_result ($result);	 		return $line;	  }//function get_a_line()	//=====================================================================================	//====================================================================================	function insert($mysql)	  {			  if (! (mysql_query ("$mysql")))//$mysql is for the query			{				  $men = mysql_errno();				  $mem = mysql_error();				  echo ("<h4>$mysql  $men $mem</h4>");			  exit;			}	}//function insert()	//===================================================================================	//===================================================================================	function insert_data_id($mysql) 	{		if (!mysql_query ("$mysql"))		{			$men = mysql_errno();			$mem = mysql_error();			echo ("<h4>$mysql  $men $mem</h4>");			exit;		} 		$r=mysql_insert_id();		  return $r;	}//end insert data id	//====================================================================================	  //====================================================================================	function get_single_column ($mysql)	  {	 		$x = 0;	 		$result = mysql_query($mysql);	 		while ( $row = mysql_fetch_array ($result) ) 	 		{	   			$q[$x] = $row[0];	   			$x++;	 		}	 		mysql_free_result ($result);	 		return $q;	}//access using $q[1]["fieldname"] or $q[1][3] etc	//======================================================================================	//======================================================================================		function check($table,$column,$v1)	{	 		if (! $result=mysql_query ("select * from $table where $column ='$v1'"))		{		   		$men = mysql_errno();		 		$mem = mysql_error();		 		echo ("<h4>$mysql  $men $mem</h4>");		 		exit();		}	 		$row=mysql_fetch_array ($result);	 		mysql_free_result ($result);	 		if ($row[0])				$var =  1;	 		else			$var =  0;	 		return $var;	}//function check()	//=====================================================================================	//=====================================================================================	function check_edit($table,$column1,$v1,$column2,$v2)		{		if (! $result=mysql_query ("select * from $table where $column2 !=$v2 and $column1='$v1' "))		{				$men = mysql_errno();		 		$mem = mysql_error();		 		echo ("<h4>$mysql  $men $mem</h4>");		 		exit();		}			$row=mysql_fetch_array ($result);	 		mysql_free_result ($result);	 		if ($row[0])			$var =  1;	 		else			$var =  0;	 		return $var;	}//function check_edit()	//=====================================================================================}//end of class database?>

but I don't know how to add the contents of the form into the database. The hosting company only supports PHP 4.4.4.The form code is:

<form name="swisscottage" method="post" action="">	<tr>	  <td width="35%" valign="top" class="text">Title:</td>	  <td width="65%" valign="top" class="text"><select name="Title" id="Title">		<option value="mr" selected>Mr</option>		<option value="ms">Ms</option>		<option value="miss">Miss</option>		<option value="mrs">Mrs</option>		<option value="dr">Dr</option>	  </select></td>	</tr>	<tr>	  <td valign="top" class="text"><label> First name: </label></td>	  <td valign="top" class="text"><input name="firstname:" type="text" size="37"></td>	  </tr>	<tr>	  <td valign="top" class="text">Last name: </td>	  <td valign="top" class="text"><input name="lastname:" type="text" size="37"></td>	  </tr>	<tr>	  <td valign="top" class="text">		<label> Address: </label></td>	  <td valign="top" class="text"><input name="street:" type="text" id="Address:" size="37"></td>	  </tr>	<tr>	  <td valign="top" class="text"><label>Town:</label></td>	  <td valign="top" class="text"><input name="town:" type="text" size="37"></td>	  </tr>	<tr>	  <td valign="top" class="text"><label>City:</label></td>	  <td valign="top" class="text"><input name="city:" type="text" size="37"></td>	  </tr>	<tr>	  <td valign="top" class="text"><label>Postcode:</label></td>	  <td valign="top" class="text"><input name="postcode:" type="text" size="37"></td>	  </tr>	<tr>	  <td valign="top" class="text"><label>Telephone no:</label></td>	  <td valign="top" class="text"><input name="phone:" type="text" size="37"></td>	  </tr>	<tr>	  <td valign="top" class="text"><label>Mobile no: </label></td>	  <td valign="top" class="text"><input name="mobile:" type="text" size="37"></td>	  </tr>	<tr>	  <td valign="top" class="text"><label>Email address:</label></td>	  <td valign="top" class="text"><input name="email:" type="text" size="37"></td>	  </tr>	<tr>	  <td valign="top" class="text"><label>Date of birth:</label></td>	  <td valign="top" class="text"><input name="day:" type="text" value="dd" size="2">	  <input name="month:" type="text" value="mm" size="2">	  <input name="year:" type="text" value="yyyy" size="4"><br><img src="images/spacer.jpg" alt="spacer" name="spacer" id="spacer"></td>	  </tr>	<tr>	  <td colspan="2" valign="top" class="text">How did you here about us:</td>	  </tr>	<tr>	  <td colspan="2" valign="top" class="text"><input name="Heard:" type="text" size="51"><br><img src="images/spacer.jpg" alt="spacer" name="spacer" id="spacer"></td>	  </tr>	<tr>	  <td colspan="2" valign="top" class="text">Please feel free to pass on any other comments you may have: </td>	  </tr>	<tr>	  <td colspan="2" valign="top" class="text">				  <textarea name="Comments:" cols="48" rows="5"></textarea>		  <br>		  <img src="images/spacer.jpg" alt="spacer" name="spacer" id="spacer"></td>	  </tr>	<tr>	  <td colspan="2" valign="top" class="text_form">From time to time we would like to send you other promotions,please check the boxes to indicate how you would like to receive theseoffers.</td>	  </tr>	<tr>	  <td colspan="2" valign="top" class="text_form"><input name="sms" type="checkbox" value="">I would like to receive Free SMS promotions to my mobile phone.		</td>	  </tr>	<tr>	  <td colspan="2" valign="top" class="text_form"><input name="post" type="checkbox" value="">I would like to receive Free offers and promotions via email or post.		</td>	  </tr>	<tr>	  <td colspan="2" valign="top" class="text_form">We guarantee not to pass your details onto any third party, unless youauthorise us to do so by checking the box below. </td>	  </tr>	<tr>	  <td colspan="2" valign="top" class="text_form"><input name="thirdparty" type="checkbox" value="">I do not mind receiving other offers from charities and other reputablecompanies.		</td>	  </tr>	<tr>	  <td colspan="2" valign="top" class="text_form"><input name="add" type="submit" id="add" value="Submit"></td>	  </tr>	  </form>

Thanks

Link to comment
Share on other sites

Well, I wouldn't worry about the database class, it will be easier to just use the database directly. That particular class doesn't really provide anything useful that would be a motivation for using it. Check the PHP tutorial on the w3schools site, or the PHP tips thread in the PHP forum, for a description on how to process a form using PHP. Once you can just basically process the form and read the information that was submitted from it, then you can use the MySQL functions to connect to the database and send it the insert statement. You can read the database tutorial for PHP here:http://www.w3schools.com/php/php_mysql_intro.asp

Link to comment
Share on other sites

Well, I wouldn't worry about the database class, it will be easier to just use the database directly. That particular class doesn't really provide anything useful that would be a motivation for using it. Check the PHP tutorial on the w3schools site, or the PHP tips thread in the PHP forum, for a description on how to process a form using PHP. Once you can just basically process the form and read the information that was submitted from it, then you can use the MySQL functions to connect to the database and send it the insert statement. You can read the database tutorial for PHP here:http://www.w3schools.com/php/php_mysql_intro.asp
Thanks for that I'll have a look at that tutorial
Link to comment
Share on other sites

I tried using:

<?php$con = mysql_connect("common/config.php");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("xxxxx_xxxxxxx", $con);$sql="INSERT INTO Stanmore (Title, firstname, lastname, address, town, city, postcode, phone, mobile, email, day, month, heard, comments, sms, post, thirdparty)VALUES('$_POST[Title]','$_POST[firstname]','$_POST[lastname]','$_POST[lastname]','$_POST[town]','$_POST[city]','$_POST[postcode]','$_POST[phone]','$_POST[mobile]','$_POST[email]','$_POST[day]','$_POST[month]','$_POST[heard]','$_POST[comments]','$_POST[sms]','$_POST[post]','$_POST[thirdparty]')";if (!mysql_query($sql,$con))  {  die('Error: ' . mysql_error());  }echo "1 record added";mysql_close($con)?>

but got an error saying:

Warning: mysql_connect(): Unknown MySQL Server Host 'common/config.php' (11004) in \\NAS35ENT\domains\m\whatever.com\user\htdocs\stores_add.php on line 2Could not connect: Unknown MySQL Server Host 'common/config.php' (11004)
I wasn't sure what it meant. Is what I've done totally wrong or am I on the right lines?
Link to comment
Share on other sites

I changed

$con = mysql_connect("common/config.php");

to

$con = mysql_connect("host","dbname","dbpass");

but got an error saying:

Warning: mysql_connect(): Access denied for user 'xxxxx_xxxxxx'@'server213-171-219-2.livedns.org.uk' (using password: YES) in \\NAS35ENT\domains\m\whatever.com\user\htdocs\stores_add.php on line 2Could not connect: Access denied for user 'xxxxx_xxxxxx'@'server213-171-219-2.livedns.org.uk' (using password: YES)
Link to comment
Share on other sites

I removed the

$con = mysql_connect("host","dbname","dbpass");

and replaced it with

include("common/config.php");mysql_connect($host, $dbuser, $dbpass);mysql_select_db($dbname);

but I got an error saying could not connectI added

$con = mysql_connect("host","dbuser","dbpass");

instead and then it said that the table didn't exist, so I now seem to be having a problem creating a table. I've tried:

<?php$con = mysql_connect("host","dbuser","dbpass");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("dbname", $con);$sql = "CREATE TABLE store (Title varchar(50),firstname varchar(50),lastname varchar(50),address varchar(50),town varchar(50),city varchar(50),postcode varchar(50),phone varchar(50),mobile varchar(50),email varchar(50),day varchar(50),month varchar(50),heard varchar(100),comments varchar(200),sms,post,thirdparty,)";mysql_query($sql,$con);mysql_close($con);?>

but just get a blank window. If I try to run the other code again it still says that the table doesn't exist

Link to comment
Share on other sites

This code:$con = mysql_connect("host","dbuser","dbpass");will only work if the server you are trying to connect to is called "host", the user you are using is "dbuser", and the password for the user is "dbpass". In all other situations that will not work. If you want to use the values in the config file, that would be the code I gave you, you would first include the file (which defines the variables), and then use the new variables to connect. If you can't connect and you know the information is correct, post the error message.Also, the create table statement you posted has some problems. The last three columns listed are missing the data type, and there is an extra comma at the end.

Link to comment
Share on other sites

This code:$con = mysql_connect("host","dbuser","dbpass");will only work if the server you are trying to connect to is called "host", the user you are using is "dbuser", and the password for the user is "dbpass". In all other situations that will not work. If you want to use the values in the config file, that would be the code I gave you, you would first include the file (which defines the variables), and then use the new variables to connect. If you can't connect and you know the information is correct, post the error message.Also, the create table statement you posted has some problems. The last three columns listed are missing the data type, and there is an extra comma at the end.
Sorry when I said host dbuser and dbpass I meant that I'd put the hostname, the database name and database password in those fields. When I try to create a table I just get a blank window and when I try to add the data into the table I get and error saying that the table doesn't existThe last three data types are tick boxes that I wanted to be true or false, but didn't know how to set that.
Link to comment
Share on other sites

There is an error on the blank page that you aren't seeing. It's just a syntax error probably complaining about either the lack of a data type or the extra comma, but you can see it like this:if (!mysql_query($sql,$con)) echo mysql_error();For something that you want to be true/false you can either use boolean or tinyint. Check the MySQL documentation for more information:http://dev.mysql.com/doc/refman/4.1/en/data-types.html

Link to comment
Share on other sites

There is an error on the blank page that you aren't seeing. It's just a syntax error probably complaining about either the lack of a data type or the extra comma, but you can see it like this:if (!mysql_query($sql,$con)) echo mysql_error();For something that you want to be true/false you can either use boolean or tinyint. Check the MySQL documentation for more information:http://dev.mysql.com/doc/refman/4.1/en/data-types.html
Thanks for that, it now says that 1 record was added so I guess that it's almost done.Thanks for your help
Link to comment
Share on other sites

Sorry I just linked the page (that gave the 1 record stored) result to the html form and then when I went to the input data and submit the form I got an error saying:

No input file specified. HTTP/1.1 503 Server too busy Connection: close Date: Tue, 14 Aug 2007 00:42:56 GMT Server: Microsoft-IIS/6.0 Content-Type: text/html Server Error, unable to connect to fastcgi server.
but it worked a minute ago.
Link to comment
Share on other sites

This looks like an error with fastcgi. What is the setup you are using there, it looks like you have IIS6 so I'm assuming you're using either Windows XP or 2003, but what about the PHP setup, did you install it yourself or did you use a package that installs it for you?

Link to comment
Share on other sites

PHP + IIS = ######. Use Apache on windows. You dont really need to use ASP(if thats the only reason you're using IIS). Also, you may want to check and see if MySQL is even installed. On a windows server, out of the box, i highly, highly doubt it.

Link to comment
Share on other sites

I've been using PHP successfully on IIS for a while, I've never had a need to set up an Apache server. The fastcgi bug can be fixed by configuring IIS to use the isapi version instead of the fastcgi version, according to the PHP buglist the bug seems to be a buffer overflow in the fastcgi software, which is not part of either PHP or IIS.

Link to comment
Share on other sites

I've been using PHP successfully on IIS for a while, I've never had a need to set up an Apache server. The fastcgi bug can be fixed by configuring IIS to use the isapi version instead of the fastcgi version, according to the PHP buglist the bug seems to be a buffer overflow in the fastcgi software, which is not part of either PHP or IIS.
I don't know what the set up is. The database is on another server and I have only been able to access it by using the details in the config file. Also I'm using a Mac, is it possible to configure IIS on it?I used phpinfo() to find out that the php version is 4.3.2, the _SERVER["SERVER_SOFTWARE"] is Microsoft-IIS/6.0, it also says that the _SERVER["OS"] and the _ENV["OS"] are Windows_NT. But this is for the host of the site and I don't think the database.
Link to comment
Share on other sites

Also I'm using a Mac, is it possible to configure IIS on it?
NoA link to your php_info page would be nice :)
Link to comment
Share on other sites

Hmm, your host needs to upgrade that server. PHP 4.3.2 is many, many years old. I'm not sure specifically how old it is, but I know that I left my own host about 2 years ago because they refused to upgrade from that same version, so 2 years ago that version was out of date. The fastcgi version is listed as 2.2.2, from the fastcgi site it looks like they released version 2.4.2 in 2003, so that version of fastcgi is also extremely out of date.There's not a lot you can do other then moving hosts. You can try to contact your host and tell them that you would like your account upgraded to the lastest stable versions of PHP and fastcgi (and probably MySQL too), but chances are they won't agree to do that. You could also send them information about the error you're getting, because that error is due to their server setup. If they upgraded the error would probably go away.

Link to comment
Share on other sites

Hmm, your host needs to upgrade that server. PHP 4.3.2 is many, many years old. I'm not sure specifically how old it is, but I know that I left my own host about 2 years ago because they refused to upgrade from that same version, so 2 years ago that version was out of date. The fastcgi version is listed as 2.2.2, from the fastcgi site it looks like they released version 2.4.2 in 2003, so that version of fastcgi is also extremely out of date.There's not a lot you can do other then moving hosts. You can try to contact your host and tell them that you would like your account upgraded to the lastest stable versions of PHP and fastcgi (and probably MySQL too), but chances are they won't agree to do that. You could also send them information about the error you're getting, because that error is due to their server setup. If they upgraded the error would probably go away.
ThanksI've found a new database and am going to try it with that, but I'm unable to move the site. I do agree though that I alt to transfer the site
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...