Input data from a form to database
#1
Posted 16 March 2010 - 07:36 AM
i was just following this tutorialof how to input data from a form inside a database but seems to be lost somewhere
After i make the form
and insert.php they havent mentioned to create a VALUES table in the database?
sorry am i right?
#2
Posted 16 March 2010 - 07:46 AM
read step 3 again and replace the values as you need
#3
Posted 16 March 2010 - 08:00 AM
Geeks, on 16 March 2010 - 07:46 AM, said:
read step 3 again and replace the values as you need
so i have to make a results.php to display the data but how do i have any relation between the two
data.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Database </title> </head> <body> <form action="insert.php" method="post"> Value1: <input type="text" name="field1-name"><br/> Value2: <input type="text" name="field2-name"><br/> Value3: <input type="text" name="field3-name"><br/> Value4: <input type="text" name="field4-name"><br/> Value5: <input type="text" name="field5-name"><br/> <input type="submit"> </form> </body> </html>
insert.php
<?php
$username="sash_007";
$password="";
$database="your_databse";
$field1-name=$_POST['Value1'];
$field2-name=$_POST['Value2'];
$field3-name=$_POST['Value3'];
$field4-name=$_POST['Value4'];
$field5-name=$_POST['Value5'];
mysql_connect (localhost,$username,$password);
@mysql_select_db($database) or die ("Unable to select database");
$query = "INSERT INTO tablename VALUES
('','$field-name','$field2-name','field3-name','field4-name','field5-name');
mysql_query($query);
mysql_close();
?>
result.php
<?php
$username="sash_007";
$password="";
$database="your_databse";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("Unable to select database");
$query="SELECT * VALUES";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
echo "<b><center>Database Outout</center></b><br/><br/>"
$i=0;
while ($i < $num) {
$field1-name=mysql1_result ($result,$i,"field1-name");
$field2-name=mysql1_result ($result,$i,"field2-name");
$field3-name=mysql1_result ($result,$i,"field3-name");
$field4-name=mysql1_result ($result,$i,"field4-name");
$field5-name=mysql1_result ($result,$i,"field5-name");
echo "<b>$field1-name<br/>
$field2-name</b><br/>$field3-name<br/>$field4-name<br/>$field5-name<hr><br/>";
$i++;
}
?>
the what is the name of this page
<html>
<body>
<?php
$username="username";
$password="password";
$database="your_database";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM tablename";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
?>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Value1</font></th>
<th><font face="Arial, Helvetica, sans-serif">Value2</font></th>
<th><font face="Arial, Helvetica, sans-serif">Value3</font></th>
<th><font face="Arial, Helvetica, sans-serif">Value4</font></th>
<th><font face="Arial, Helvetica, sans-serif">Value5</font></th>
</tr>
<?php
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"field1");
$f2=mysql_result($result,$i,"field2");
$f3=mysql_result($result,$i,"field3");
$f4=mysql_result($result,$i,"field4");
$f5=mysql_result($result,$i,"field5");
?>
<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font></td>
</tr>
<?php
$i++;
}
?>
</body>
</html>
am i going right?
#4
Posted 16 March 2010 - 08:32 AM
lets do one at a time
have you created a database ?
#6
Posted 16 March 2010 - 09:17 AM
what tables does your DB have and how are those tables structured eg:
[table1]
[ID][item_name][item_desrciption]
[table2]
[field1][field2][field3][field4]
#7
Posted 16 March 2010 - 09:30 AM
Geeks, on 16 March 2010 - 09:17 AM, said:
what tables does your DB have and how are those tables structured eg:
[table1]
[ID][item_name][item_desrciption]
[table2]
[field1][field2][field3][field4]
thanks
so in this case according to the example i have to make 2 tables ?
#8
Posted 16 March 2010 - 09:41 AM
how do you work on your database ?
phpmyadmin ?
#10
Posted 16 March 2010 - 10:03 AM
create a table in test_data_base named test_table_1 give it 4 fields
name these fields
ID - Set to number, and primary key, and to auto increment
Name - Set to Text
Surname - Set To Text
Email - Set to Text
so you will have
[test_data_base]
[test_table_1]
[ID][NAME][SURNAME][EMAIL]
now in phpmyadmin open test_table_1 click on insert and add some data for 1 user
leave ID blank, it will set automatically
Name = John
Surname = Doe
Email = jdoe@mymail.com
#18
Posted 16 March 2010 - 12:11 PM
you have a DB and a TABLE with some DATA <div><br></div><div>EDIT : my code got all messed up gonna repost</div>
#19
Posted 16 March 2010 - 12:16 PM
call it print_data.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<?php
// we are just going to show how the php talks to the DB here
$username="sash_007"; //set username
$password=""; //set password
$database = "test_data_base"; //set database
$server = "localhost"; //set server
$sqlcon = mysql_connect($server,$username,$password); //connect to DB
@mysql_select_db($database) or die( "Unable to select database"); //connect to table
$query = "SELECT * from test_table_1"; //set query
$data = mysql_query($query) or die('Query failed: ' . mysql_error()); //fetch data
unset($query); //save resources by killing unused variables
if (!$data ) //some simple error checking
{
//communicate with user if there is an error
echo "sorry there was an error if you think we should know about this please let us know at <a href='mailto:email@server.com'>email@server.com</a>";
exit; //stop processing if there is an error
}
else
{
while($row = mysql_fetch_row($data)) //loop through results which are now matched with parameters
{
//echo each value
echo "ID = $row[0]<br />"; // indexing always starts at 0
echo "NAME = $row[1]<br />";
echo "SURNAME = $row[2]<br />";
echo "EMAIL = $row[3]<br />";
}
}
unset($data); //save resources by killing unused variables
?>
</body>
</html>
#20
Posted 16 March 2010 - 12:21 PM
Geeks, on 16 March 2010 - 12:16 PM, said:
call it print_data.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<?php
// we are just going to show how the php talks to the DB here
$username="sash_007"; //set username
$password=""; //set password
$database = "test_data_base"; //set database
$server = "localhost"; //set server
$sqlcon = mysql_connect($server,$username,$password); //connect to DB
@mysql_select_db($database) or die( "Unable to select database"); //connect to table
$query = "SELECT * from test_table_1"; //set query
$data = mysql_query($query) or die('Query failed: ' . mysql_error()); //fetch data
unset($query); //save resources by killing unused variables
if (!$data ) //some simple error checking
{
//communicate with user if there is an error
echo "sorry there was an error if you think we should know about this please let us know at <a href='mailto:email@server.com'>email@server.com</a>";
exit; //stop processing if there is an error
}
else
{
while($row = mysql_fetch_row($data)) //loop through results which are now matched with parameters
{
//echo each value
echo "ID = $row[0]<br />"; // indexing always starts at 0
echo "NAME = $row[1]<br />";
echo "SURNAME = $row[2]<br />";
echo "EMAIL = $row[3]<br />";
}
}
unset($data); //save resources by killing unused variables
?>
</body>
</html>
it shows
Unable to select database
#21
Posted 16 March 2010 - 12:26 PM
change <div>
$database ="test_data_base";//set database<div>to </div><div>
$database =" test_data_base";//set database</div><div> note the space
</div></div>
#22
Posted 16 March 2010 - 12:38 PM
Geeks, on 16 March 2010 - 12:26 PM, said:
change <div>
$database ="test_data_base";//set database<div>to </div><div>
$database =" test_data_base";//set database</div><div> note the space
</div></div>
ya it shows
ID = 1
NAME = Jon
SURNAME = Doe
EMAIL = jdoe@mymail.com
#23
Posted 16 March 2010 - 12:46 PM
<div>This site is not loving me</div><div><br></div><div>will repost</div>
#24
Posted 16 March 2010 - 12:49 PM
call it new_user.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<!-- THIS FORM WILL SEND THE DATA TO update_page.php then update_page.php will handle it -->
<form method="post" action="update_page.php">
NAME : <input type="text" name="input_name" /><!--USER WILL ENTER NAME HERE -->
<br />
SURNAME : <input type="text" name="input_surname" /><!--USER WILL ENTER SURNAME HERE -->
<br />
EMAIL : <input type="text" name="input_email" /><!--USER WILL ENTER EMAIL HERE -->
<br /><br />
<input type="submit" value="add user"><!-- THE SUBMIT BUTTON SEND THE INFORMATIO -->
</form>
</body>
</html>
#25
Posted 16 March 2010 - 12:58 PM
Geeks, on 16 March 2010 - 12:49 PM, said:
call it new_user.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head></head> <body> <!-- THIS FORM WILL SEND THE DATA TO update_page.php then update_page.php will handle it --> <form method="post" action="update_page.php"> NAME : <input type="text" name="input_name" /><!--USER WILL ENTER NAME HERE --> <br /> SURNAME : <input type="text" name="input_surname" /><!--USER WILL ENTER SURNAME HERE --> <br /> EMAIL : <input type="text" name="input_email" /><!--USER WILL ENTER EMAIL HERE --> <br /><br /> <input type="submit" value="add user"><!-- THE SUBMIT BUTTON SEND THE INFORMATIO --> </form> </body> </html>
ya done
#26
Posted 16 March 2010 - 01:01 PM
#27
Posted 16 March 2010 - 01:03 PM
This post has been edited by Geeks: 16 March 2010 - 01:04 PM
#28
Posted 16 March 2010 - 01:04 PM
[/size][/color][/size][/font]
[font="arial, verdana, tahoma, sans-serif"][size="3"][color="#1C2837"][size=2]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<?php
$username="sash_007"; //set username
$password=""; //set password
$database = "test_data_base"; //set database
$server = "localhost"; //set server
$sqlcon = mysql_connect($server,$username,$password); //connect to DB
@mysql_select_db($database) or die( "Unable to select database"); //connect to table
// the data that was sent is stored in a variable called $_POST
$new_name = $_POST["input_name"];
$new_surname = $_POST["input_surname"];
$new_email = $_POST["input_email"];
$update_query = "INSERT INTO `test_data_base`.`test_table_1` ( `ID`, `name` , `surname` , `email` )
VALUES (NULL , '$new_name', '$new_surname', '$new_email');";
$update = mysql_query($update_query) or die('Query failed: ' . mysql_error());
echo "<h1 align='center'>USER ADDED</h1>";
?>
<!-- THIS FORM WILL SEND THE DATA TO update_page.php then update_page.php will handle it -->
<form method="post" action="update_page.php">
NAME : <input type="text" name="input_name" /><!--USER WILL ENTER NAME HERE -->
<br />
SURNAME : <input type="text" name="input_surname" /><!--USER WILL ENTER SURNAME HERE -->
<br />
EMAIL : <input type="text" name="input_email" /><!--USER WILL ENTER EMAIL HERE -->
<br /><br />
<input type="submit" value="add user"><!-- THE SUBMIT BUTTON SEND THE INFORMATIO -->
</form>
</body>
</html>
[/size][/color][/size][/font]
[font="arial, verdana, tahoma, sans-serif"][size="3"][color="#1C2837"][size=2]
#29
Posted 16 March 2010 - 01:07 PM
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: YES) in C:\wamp\www\practice\update_page.php on line 10
Unable to select database
shld i drop root from privileges
#30
Posted 16 March 2010 - 01:10 PM
always just double check that.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<?php
$username="sash_007"; //set username
$password=""; //set password
$database = " test_data_base"; //set database
$server = "localhost"; //set server
$sqlcon = mysql_connect($server,$username,$password); //connect to DB
@mysql_select_db($database) or die( "Unable to select database"); //connect to table
// the data that was sent is stored in a variable called $_POST
$new_name = $_POST["input_name"];
$new_surname = $_POST["input_surname"];
$new_email = $_POST["input_email"];
$update_query = "INSERT INTO `test_data_base`.`test_table_1` ( `ID`, `name` , `surname` , `email` )
VALUES (NULL , '$new_name', '$new_surname', '$new_email');";
$update = mysql_query($update_query) or die('Query failed: ' . mysql_error());
echo "<h1 align='center'>USER ADDED</h1>";
?>
<!-- THIS FORM WILL SEND THE DATA TO update_page.php then update_page.php will handle it -->
<form method="post" action="update_page.php">
NAME : <input type="text" name="input_name" /><!--USER WILL ENTER NAME HERE -->
<br />
SURNAME : <input type="text" name="input_surname" /><!--USER WILL ENTER SURNAME HERE -->
<br />
EMAIL : <input type="text" name="input_email" /><!--USER WILL ENTER EMAIL HERE -->
<br /><br />
<input type="submit" value="add user"><!-- THE SUBMIT BUTTON SEND THE INFORMATIO -->
</form>
</body>
</html>
#31
Posted 16 March 2010 - 01:23 PM
Geeks, on 16 March 2010 - 01:10 PM, said:
always just double check that.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<?php
$username="sash_007"; //set username
$password=""; //set password
$database = " test_data_base"; //set database
$server = "localhost"; //set server
$sqlcon = mysql_connect($server,$username,$password); //connect to DB
@mysql_select_db($database) or die( "Unable to select database"); //connect to table
// the data that was sent is stored in a variable called $_POST
$new_name = $_POST["input_name"];
$new_surname = $_POST["input_surname"];
$new_email = $_POST["input_email"];
$update_query = "INSERT INTO `test_data_base`.`test_table_1` ( `ID`, `name` , `surname` , `email` )
VALUES (NULL , '$new_name', '$new_surname', '$new_email');";
$update = mysql_query($update_query) or die('Query failed: ' . mysql_error());
echo "<h1 align='center'>USER ADDED</h1>";
?>
<!-- THIS FORM WILL SEND THE DATA TO update_page.php then update_page.php will handle it -->
<form method="post" action="update_page.php">
NAME : <input type="text" name="input_name" /><!--USER WILL ENTER NAME HERE -->
<br />
SURNAME : <input type="text" name="input_surname" /><!--USER WILL ENTER SURNAME HERE -->
<br />
EMAIL : <input type="text" name="input_email" /><!--USER WILL ENTER EMAIL HERE -->
<br /><br />
<input type="submit" value="add user"><!-- THE SUBMIT BUTTON SEND THE INFORMATIO -->
</form>
</body>
</html>
changed and i got this
Query failed: Table 'test_data_base. test_table_1' doesn't exist
i tried adding space
A.jpg (45.57K)
Number of downloads: 3
#32
Posted 16 March 2010 - 01:29 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<?php
$username="sash_007"; //sash_007 //set username
$password=""; //set password
$database = " test_data_base"; //set database
$server = "localhost"; //set server
$sqlcon = mysql_connect($server,$username,$password); //connect to DB
@mysql_select_db($database) or die( "Unable to select database"); //connect to table
// the data that was sent is stored in a variable called $_POST
$new_name = $_POST["input_name"];
$new_surname = $_POST["input_surname"];
$new_email = $_POST["input_email"];
$update_query = "INSERT INTO test_table_1 ( `ID`, `name` , `surname` , `email` )
VALUES (NULL , '$new_name', '$new_surname', '$new_email');";
$update = mysql_query($update_query) or die('Query failed: ' . mysql_error());
echo "<h1 align='center'>USER ADDED</h1>";
?>
<!-- THIS FORM WILL SEND THE DATA TO update_page.php then update_page.php will handle it -->
<form method="post" action="update_page.php">
NAME : <input type="text" name="input_name" /><!--USER WILL ENTER NAME HERE -->
<br />
SURNAME : <input type="text" name="input_surname" /><!--USER WILL ENTER SURNAME HERE -->
<br />
EMAIL : <input type="text" name="input_email" /><!--USER WILL ENTER EMAIL HERE -->
<br /><br />
<input type="submit" value="add user"><!-- THE SUBMIT BUTTON SEND THE INFORMATIO -->
</form>
</body>
</html>
- ← Changing .asp pages to .html pages. Any Implications?
- Server Side (PHP, Databases, ASP.NET, etc)
- PHP Contact Form - Error 404 Object Not Found →
Help
















