i finally got my head round inserting data in mysql through an html form, but now i want to display all of the table entries on a page on my site.
can anyone let me know how to do it?
any help will be appreciated, thanx
i finally got my head round inserting data in mysql through an html form, but now i want to display all of the table entries on a page on my site.
can anyone let me know how to do it?
any help will be appreciated, thanx
Last edited by liamallan; 02-16-2010 at 03:12 AM.
Something like:
Code:$query = mysql_query("SELECT * FROM `table`") or die(mysql_error()); while($row = mysql_fetch_assoc($query)){ echo $row['field']; }
Jeremy | jfein.net
yes, something like that mate. but i want to add entries in a table.
the fields on my db are: name and profile
$name = 'thomas';
$profile = 'profile';
$query = mysql_query("INSERT INTO table(name, profile) VALUES('$name', '$profile')") or die(mysql_error());
im new to php, so you might need to explain a little.
my situation is: created form on shiftysplayground.co.cc/addfriends.php to post data (name, profile) into database.
the plan is to have a table of users below the form on shiftysplayground.co.cc/addfriends.php, taken from database.
will i need to add the php code to the page addfriends.php or the file supplying the action?
assuming the form on addfriends.php has an action of process and a method of POST, you would combine the code\ideas from the above posts:
and your add friends page that dispalys a list:PHP Code:<?php
//process.php
$host="yourhost";
$username="yourusername";
$password="yourpass";
$db_name="yourDB";
$tbl_name="yourTABLE";
mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select db");
$name = $_POST['name'];
$profile = $_POST['profile'];
$query = mysql_query("INSERT INTO $tbl_name(name, profile) VALUES('$name', '$profile')") or die(mysql_error());
header('location: addfriends.php');
?>
****Untested******PHP Code:<?php
//process.php
$host="yourhost";
$username="yourusername";
$password="yourpass";
$db_name="yourDB";
$tbl_name="yourTABLE";
mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select db");
$query = mysql_query("SELECT * FROM `table`") or die(mysql_error());
?>
<!-- Here's where your form goes, then place this beneath it or wherever you want the list.... -->
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td colspan="2"><strong>Entries</strong> </td>
</tr>
<tr>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Profile</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($query)){
?>
<tr>
<td><? echo $rows['name']; ?></td>
<td><? echo $rows['profile']; ?></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
-Ben -- THE DYNAMIC DRIVERS
My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
I told my client to press F5, the client pressed F, then 5, *facepalm*
liamallan (02-16-2010)
thank you mate, it seems to be inputting the data into the table very well.
i have another question though,
new entries seem to be joining at the bottom of the table, how could i make it so they join at the top?
also, the data entered into the profile field is a url, is there a way i could make the url fields a clickable link that reads 'add friend'?
thanks again for your help
To make it display descending change:
To:Code:$query = mysql_query("SELECT * FROM `table`") or die(mysql_error());
For profile, change:Code:$query = mysql_query("SELECT * FROM `table` DESC") or die(mysql_error());To:Code:<td><? echo $rows['profile']; ?></td>
Code:<td><a href="<? echo $rows['profile']; ?>">Add Friend</a></td>
Jeremy | jfein.net
does that mean it doesnt recognise 'desc'?You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DESC' at line 1
Bookmarks