Log in

View Full Version : Resolved Form-MySQL-Display table on site



liamallan
02-13-2010, 01:53 PM
i want to use the following form to post data into a MySQL database and then project database entries into a table on my site! but need some guidance.


<form name="add" action"addfriends.php" method="post" >
<tr class="b">
<td width="11%">&nbsp;Name:</td>
<td width="38%"><input type="text" name="name" id="name" /></td>
<td width="20%">&nbsp;Profile URL: </td>
<td width="31%"><input name="profile" type="text" id="profile" size="15" />
&nbsp;</td>
</tr>
<tr class="b">
<td>&nbsp;Gender:</td>
<td width="31%"><input name="profile" type="text" id="profile" size="15" />
<option value="1" selected="selected">Male</option>
<option value="2">Female</option>
</select></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr class="b">
<td>&nbsp;Location:</td>
<td colspan="3"><label>
<select name="location" size="1">
<option selected="selected" value="">Select Country</option>
//removed options for shorter post
</select>
</label></td>
</tr>
<tr class="b">
<td>&nbsp;</td>
<td align="right"><input name="button3" type="submit" id="button" value="Request Friends" /></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</form>
my database is already created with the following fields: Name, Gender, Location and Profile url.
however, i dont know how to create an action to post data from form to database and show database on site!
can anyone help plz?!

fileserverdirect
02-13-2010, 04:36 PM
Well assuming if your using MySql, you could:


<?php
$host="localhost";
$username="username";
$password="password";
$db_name="test";
$tbl_name="test_mysql";

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
foreach($_POST as $var => $val){
$var=$val;
}
$sql="INSERT INTO $tbl_name(Name, Gender, Location, Profile url)VALUES('$name', '$gender', '$location', '$profile')";
$result=mysql_query($sql);
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='index.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
mysql_close();
?>

and you have a typo in your HTML:


<td>&nbsp;Gender:</td>
<td width="31%"><select name="gender" id="gender" />
<option value="1" selected="selected">Male</option>
<option value="2">Female</option>
</select></td>

******BOTH UNTESTED. THERE MAY BE ERRORS********

liamallan
02-13-2010, 06:23 PM
thanks for your reply, when i run 'addprocess.php' (thats what i called action), i get the following message:


Notice: Undefined variable: Name in /www/freewebhostx.com/l/i/a/liamallan/htdocs/addprocess.php on line 13

Notice: Undefined variable: Gender in /www/freewebhostx.com/l/i/a/liamallan/htdocs/addprocess.php on line 13

Notice: Undefined variable: Location in /www/freewebhostx.com/l/i/a/liamallan/htdocs/addprocess.php on line 13

Notice: Undefined variable: Profile in /www/freewebhostx.com/l/i/a/liamallan/htdocs/addprocess.php on line 13
ERROR
any ideas?

Schmoopy
02-13-2010, 10:44 PM
You need to set up the variables before being able to insert them into the database, like this:


<?php
$host="localhost";
$username="username";
$password="password";
$db_name="test";
$tbl_name="test_mysql";

$name = mysql_real_escape_string($_POST['name']);
$gender = mysql_real_escape_string($_POST['gender']);
$location = mysql_real_escape_string($_POST['location']);
$profile = mysql_real_escape_string($_POST['profile']);

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="INSERT INTO $tbl_name(Name, Gender, Location, Profile url)VALUES('$name', '$gender', '$location', '$profile')";
$result=mysql_query($sql);
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='index.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
mysql_close();
?>

This form has no validation, so you may want to consider adding that in, by using:


if(isset($_POST['FIELD_NAME']) && !empty($_POST['FIELD_NAME'])) {
$FIELD_NAME = $_POST['FIELD_NAME'];
} else{
echo "Please fill in all the required fields."
}

To check whether or not the user has entered the information they need to.

liamallan
02-13-2010, 10:58 PM
thanx for replying, i used the above action and tells me:

Notice: Undefined index: name in /www/freewebhostx.com/l/i/a/liamallan/htdocs/addprocess.php on line 8

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'vhostswww'@'localhost' (using password: NO) in /www/freewebhostx.com/l/i/a/liamallan/htdocs/addprocess.php on line 8
says that for lines 8, 9, 10 and 11.
im new to php, so im pretty confused lol

CustomPowerDesigns
02-14-2010, 03:21 AM
What hosting company are you using? There are some hosting companies that do not use localhost for the host, and that could be where you are going wrong. And can you post your php code as well so we can make sure that it looks right.

liamallan
02-14-2010, 11:42 AM
i am with freewebhostx.com

<?php
$host="localhost";
$username="username";
$password="password";
$db_name="liamallan_addfriends";
$tbl_name="addfriends";

$name = mysql_real_escape_string($_POST['name']);
$gender = mysql_real_escape_string($_POST['gender']);
$location = mysql_real_escape_string($_POST['location']);
$profile = mysql_real_escape_string($_POST['profile']);

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="INSERT INTO $tbl_name(Name, Gender, Location, Profile url)VALUES('$name', '$gender', '$location', '$profile')";
$result=mysql_query($sql);
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='index.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
mysql_close();
?>