Jump to content
xisto Community
Sign in to follow this  
iGuest

connect to database Test Your Php Pages W/o Upload/internet

Recommended Posts

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

Share this post


Link to post
Share on other sites

Retrieving:

SELECT _fields_ FROM _table_ WHERE _field_ = _value_

Example:

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 (see edit history)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.