954,174 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help in php database connection

hello guys , please tell me how to connect my php form to a database and save data and also getting data from my database
i hope you give me the code

Member 784361
Newbie Poster
6 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

I am not 100% sure what you mean, but I'll try to help you. First, I hope that you got MySQL installed. Let's assume that you got a database called "mysite" with a table called "users" that contains two rows called "username" and "password". Both username and password is root.

index.php (The form)

<html>
<body>
	<form method="post" action="submit.php">
		Username:<input type="text" name="username"></input>
		Password:<input type="password" name="password"></input>
	</form>
</body>
</html>

submit.php (The MySQL part)

<?php
	mysql_connect("localhost","root","root"); //mysql_connect(host,username,password)
	mysql_select_db("mysite"); //Select the database mysite.
	$username=mysql_real_escape_string($_POST[username]); //mysql_real_escape_string protects you from sql injections that could "hack" your database.
	$password=mysql_real_escape_string($_POST[password]); //if you name a input field password, it is avalible in the $_POST[password] variable
	mysql_query("INSERT INTO users (username,password) VALUES('$username','$password')"); //The query that inserts everything to the database.
?>
OK!


I didn't test this code, please reply if you receive any errors(or even better, solve them!).

Hope it helps, and good luck!

Member 785088
Newbie Poster
2 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

thank you for giving me the code but i am new at php and all what i did on my hosting is that i created mysql database with the name but i did not define the name of any table so what do i need to do more
sorry for disturbing but as you know i am new at php and i dont know many things at it

Member 784361
Newbie Poster
6 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

http://www.webdevelopersnotes.com/tutorials/sql/mysql_training_course_creating_tables.php3

Before getting into the subject please go thru some tutorials for php and mysql

rajarajan07
Nearly a Posting Virtuoso
1,447 posts since May 2008
Reputation Points: 167
Solved Threads: 239
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: