Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

connect to database



3 replies to this topic

#1 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 16 March 2010 - 07:14 PM

connect to databaseTest Your Php Pages W/o Upload/internetI really need help from u guys...I want to understand the concepts of connecting my forms to my database(phpmyadmin).For example submiting a form to update my database and also to retrieve fromt eh database...Any help will be welcomed...My php file works fine on xampp-reply by nina

#2 T X

    Member [Level 1]

  • Kontributors
  • PipPipPipPip
  • 56 posts
  • Gender:Male
  • Location:Rochester, NY
  • myCENT:1.63

Posted 27 March 2010 - 08:44 PM

Retrieving:

Quote

SELECT _fields_ FROM _table_ WHERE _field_ = _value_

Example:

Quote

SELECT * FROM users WHERE uid = 1

It would get all the fields from the user with the id of one.

Using a form to get info for a username:
if($_POST) {
	$user = $_POST['username'];
	$sql = mysql_connect('localhost', 'root', '');
	mysql_select_db('mydb');
	$query = mysql_query('SELECT * FROM users WHERE username = "'.$user.'"');
	$row = mysql_fetch_array($query, $sql);
	echo '<p>';
	echo 'Username: '.$user;
	echo '<br>';
	echo 'UID: '.$row['uid'];
	echo '<br>';
	echo 'Password: '.$row['password'];
	echo '</p>';
}
Assuming your fields are username, uid, and password.

Code might not be perfect since I just pulled that out of my head but in theory it's pretty similar or the same.

Edited by T X, 27 March 2010 - 08:45 PM.


#3 web_designer

    "french rose sparkle under moonlight"...do you believe in the magic of moonlight??!!...

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPip
  • 1,385 posts
  • Gender:Female
  • Location:US, CA
  • Interests:internet and the web
    reading books
    sport
    watching tv series
    drawings and art
  • myCENT:12.10
  • Spam Patrol

Posted 27 March 2010 - 10:43 PM

First, you should create a database in your phpmyadmin let's call it (dbname) with a username (dbuser) and password (dbpassword) , then create a table in your database containing the field you want them to be in your page let's call it (tablename). For example, you want to create a form containing username , his site url and descriptions, then you should add something like this in sql in your phpmyadmin.




	CREATE TABLE tablename (
  id int not null auto_increment,
 username varchar(100),
 site_url varchar(100),
 description text, 
 primary key (id));




Now you finished from phpmyadmin, you should now create php file that let you read, write and retrieve data from your database, but first you should create a config.php file that contains the configuration of your database it will be something like this.




	<?

  $dbname = "dbname";

  $dbuname = "dbuser";

  $dbpws = "dbpassword";

  $dbhost = "hostname";

  ?>




Now we will create the file that contains the form and how to connect it to your db lets call it add.php




	<?

  include("config.php");

	  mysql_pconnect($dbhost, $dbname, $dbpassword);

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

	  mysql_query("insert into tablename values ('','$username','$siteurl','$des')");

	  mysql_close();

  ?>







In these lines we do the following: called config.php file to get the information required to connect db, we used sql query (mysql_pconnect ) to open db, we selected the table name that we want to add the data to it which is (tablename), then we close the connection. Till now we finished from php and will continue in html. We will add these lines just under php lines



	<form method="POST" action="add.php?action=addsite">

	<p>UserName <input type="text" name="name" size="40" style="border-style: double; border-color: #000080"></p>

	 <p>SiteUrl&nbsp;&nbsp;&nbsp; <input type="text" name="siteurl" size="40" style="border-style: double; border-color: #000080" value="http://"></p>

	<p>description<textarea rows="5" name="des" cols="33" style="border-style: double; border-color: #000080"></textarea></p>

	<p align="center"><input type="submit" value="Submit" name="B1" style="border-style: double; border-color: #000080"><input type="reset" value="Reset" name="B2" style="border-style: double; border-color: #000080"></p>

  </form>







As you can see it is a normal form in html, the only difference that we used action to connect the form to the php file. That's all, I hope you get the point here.

#4 Rigaudon

    Advanced Member

  • Kontributors
  • PipPipPipPipPipPipPip
  • 101 posts
  • Gender:Male
  • Location:&lt;?php echo __FILE__; ?&gt; in other words, right behind you.
  • myCENT:50.57

Posted 09 April 2010 - 08:48 PM

There are two types of PHP forms: What I like to call Self-forms and linked forms (I'm sure there's a professional definition but that's what I call them).

Before I get into that, in your HTML, you should have a form. You need to put the attribute "action" and method on it, like:
<form action="process.php" method="post" enctype="multi-part/form data" >

Also, be sure that ALL inputs in your form have names that are different. You also must include a submit button (input type="submit").

I assume you already know about user CRUD (Create Read Update Delete) for MySQL.

On a self-editing form, the form action would be the same as the file with the form on it.

On the processing page, you need to use your superglobals (either $_POST, $_GET, or $_REQUEST), depending on which method you used for your form.

If, for example, you had a field in the form named "username" with the "post" method, then what the user entered could be retrieved with:

$_POST['username'];

It's as simple as that. The $_REQUEST superglobal simply contains all the vars from both $_GET and $_POST. From there, you can insert them into the database.




Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users