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

Recommended Answers


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

Jump to Post

All 3 Replies

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)

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

submit.php (The MySQL part)

	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.

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

Hope it helps, and good luck!

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.