...

View Full Version : error attaching form to database! help needed :(



LJackson
08-10-2008, 09:56 PM
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

zackwiny
08-10-2008, 10:02 PM
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

Try instead of echo $result; use print_r($result);

I didn't test it but it should work.

LJackson
08-10-2008, 10:08 PM
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

zackwiny
08-10-2008, 10:17 PM
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

LJackson
08-10-2008, 10:19 PM
ok cheers mate

unrelenting
08-11-2008, 05:37 AM
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.



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

abduraooft
08-11-2008, 06:22 AM
@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

LJackson
08-11-2008, 10:04 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum