Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-10-2008, 09:56 PM   PM User | #1
LJackson
Senior Coder

 
Join Date: Jun 2008
Location: Cornwall
Posts: 1,973
Thanks: 289
Thanked 12 Times in 12 Posts
LJackson is on a distinguished road
error attaching form to database! help needed :(

hi all,

firstly i have been watching some tutorial videoswhich were a great help, so i wanted to try my new found knowledge but failed badly

what im trying to do is create a "sign up" form with 7 fields First Name, Last name, Age, Username, Password etc etc,

but i want it so when the user submits the form it adds the info to the database i setup, called login_test with 1 table called user_details which has the same 7 fields in it with the username being unique.

but i have absolutly no idea how to make the users input, add to the database,

so far i have:

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php

$my_connection = mysql_connect('localhost','root', '');

if (!$my_connection) {
die('Could Not Connect: ' . mysql_error());
}

echo 'Connected Successfully to MySQL Server' . '<br><br>';

mysql_select_db('login_test');

if (!my_database){
die('could not find database: ' . mysql_error());
}

//form processing code - using 'super globals' - Killerphp.com

$first_name = $_REQUEST['name_first'];
$last_name = $_REQUEST['name_last'];
$age = $_REQUEST['age'];
$address = $_REQUEST['address'];
$postcode = $_REQUEST['postcode'];
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];

mysql_query("INSERT INTo user_details (First Name ,Last Name,Age, Address , Postcode, Username , Password) VALUES ('$first_name' , '$last_name' , '$age', '$Address', '$postcode' , '$username' , '$password')");

$result = mysql_query("SELECT * FROM user_details");

echo $result;


mysql_close($my_connection);

?>
</body>
</html>

but i keep getting: Connected Successfully to MySQL Server

Resource id #3


instead of it listing all the data

any ideas???
cheers
Luke
LJackson is offline   Reply With Quote
Old 08-10-2008, 10:02 PM   PM User | #2
zackwiny
New Coder

 
Join Date: Aug 2008
Posts: 17
Thanks: 4
Thanked 1 Time in 1 Post
zackwiny is an unknown quantity at this point
Quote:
Originally Posted by LJackson View Post
hi all,

firstly i have been watching some tutorial videoswhich were a great help, so i wanted to try my new found knowledge but failed badly

what im trying to do is create a "sign up" form with 7 fields First Name, Last name, Age, Username, Password etc etc,

but i want it so when the user submits the form it adds the info to the database i setup, called login_test with 1 table called user_details which has the same 7 fields in it with the username being unique.

but i have absolutly no idea how to make the users input, add to the database,

so far i have:

Code:
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php

$my_connection = mysql_connect('localhost','root', '');

if (!$my_connection) {
die('Could Not Connect: ' . mysql_error());
}

echo 'Connected Successfully to MySQL Server' . '<br><br>';

mysql_select_db('login_test');

if (!my_database){
die('could not find database: ' . mysql_error());
}

//form processing code - using 'super globals' - Killerphp.com

$first_name = $_REQUEST['name_first'];
$last_name = $_REQUEST['name_last'];
$age = $_REQUEST['age'];
$address = $_REQUEST['address'];
$postcode = $_REQUEST['postcode'];
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];

mysql_query("INSERT INTo user_details (First Name ,Last Name,Age, Address , Postcode, Username , Password) VALUES ('$first_name' , '$last_name' , '$age', '$Address', '$postcode' , '$username' , '$password')");

$result = mysql_query("SELECT * FROM user_details");

echo $result;


mysql_close($my_connection);

?>
</body>
</html>
but i keep getting: Connected Successfully to MySQL Server

Resource id #3


instead of it listing all the data

any ideas???
cheers
Luke
Try instead of echo $result; use print_r($result);

I didn't test it but it should work.

Last edited by zackwiny; 08-10-2008 at 10:04 PM..
zackwiny is offline   Reply With Quote
Old 08-10-2008, 10:08 PM   PM User | #3
LJackson
Senior Coder

 
Join Date: Jun 2008
Location: Cornwall
Posts: 1,973
Thanks: 289
Thanked 12 Times in 12 Posts
LJackson is on a distinguished road
unfortunatly i get the same error and when i check my sql database its empty so im doing something wrong somewhere

just not sure where lol

Luke
LJackson is offline   Reply With Quote
Old 08-10-2008, 10:17 PM   PM User | #4
zackwiny
New Coder

 
Join Date: Aug 2008
Posts: 17
Thanks: 4
Thanked 1 Time in 1 Post
zackwiny is an unknown quantity at this point
Well the problem is that i think $result is an array and can't be echoed,
or it might be that $result is like a placeholder for the query but isn't echoing the result of the query,
can't really help you much more,

Sry
zackwiny is offline   Reply With Quote
Old 08-10-2008, 10:19 PM   PM User | #5
LJackson
Senior Coder

 
Join Date: Jun 2008
Location: Cornwall
Posts: 1,973
Thanks: 289
Thanked 12 Times in 12 Posts
LJackson is on a distinguished road
ok cheers mate
LJackson is offline   Reply With Quote
Old 08-11-2008, 05:37 AM   PM User | #6
unrelenting
Regular Coder

 
Join Date: Jan 2007
Posts: 142
Thanks: 9
Thanked 1 Time in 1 Post
unrelenting is an unknown quantity at this point
You should clean up the insert code some. Fix the comma placement.

I'd also name my fields in my database to remove the spaces, like First_Name.

Make sure your age field is set as an integer with at least 2 in the length.

You can't just echo the result like that. Try this for a test. It will only work AFTER you get stuff into the database.

Code:
$result = mysql_query("SELECT * FROM user_details");

while ($row = mysql_fetch_array($result)) 
{
	echo '' . $row['First Name'] . '<br />';
	echo '' . $row['Last Name'] . '<br />';
	echo '' . $row['Age'] . '<br />';
	echo '' . $row['Address'] . '<br />';
	echo '' . $row['Postcode'] . '<br />';
	echo '' . $row['Username'] . '<br />';
	echo '' . $row['Password'] . '';
}
I would make sure you fix those table names to remove the spaces.
unrelenting is offline   Reply With Quote
Users who have thanked unrelenting for this post:
LJackson (08-11-2008)
Old 08-11-2008, 06:22 AM   PM User | #7
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
@LJackson, you need to fetch the data from the mysql query result(as given in the above post), see the manual for further examples and similar function, http://php.net/mysql_query
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 08-11-2008, 10:04 AM   PM User | #8
LJackson
Senior Coder

 
Join Date: Jun 2008
Location: Cornwall
Posts: 1,973
Thanks: 289
Thanked 12 Times in 12 Posts
LJackson is on a distinguished road
thanks guys, now have records saving to database and echoing on screen, however 2 fields dont save or echo, address which ive set as a text string, and password which is set as a password in the form but they dont save, i ve checked the code and everything seems ok

<PHP>
<?php

$my_connection = mysql_connect('localhost','root', '');

if (!$my_connection) {
die('Could Not Connect: ' . mysql_error());
}

echo 'Connected Successfully to MySQL Server' . '<br><br>';

mysql_select_db('login_test');

if (!my_database){
die('could not find database: ' . mysql_error());
}

//form processing code - using 'super globals' - Killerphp.com

$first_name = $_REQUEST['name_first'];
$last_name = $_REQUEST['name_last'];
$age = $_REQUEST['age'];
$address = $_REQUEST['address'];
$postcode = $_REQUEST['postcode'];
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];

mysql_query("INSERT INTO user_details (first_name, last_name, age, address , postcode, username , password) VALUES ('$first_name' , '$last_name' , '$age', '$address', '$postcode' , '$username' , '$password')");

$result = mysql_query("SELECT * FROM user_details");

while ($row = mysql_fetch_array($result))
{
echo '' . $row['first_name'] . '<br />';
echo '' . $row['last_name'] . '<br />';
echo '' . $row['age'] . '<br />';
echo '' . $row['address'] . '<br />';
echo '' . $row['postcode'] . '<br />';
echo '' . $row['username'] . '<br />';
echo '' . $row['password'] . '';
}
mysql_close($my_connection);

?>
</PHP>

p.s what did you mean by saying clean up you commas?

cheers
LJackson is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:42 AM.


Advertisement
Log in to turn off these ads.