Hello everyone, great forum! - I am a newbie and trying to learn PHP and MYSQL and have run into a prob! - Wonder if you guys and girls would help me out?
Ok I have a tourism accomodation database consisting of several tables. I would like to display the information in a table with the option to edit. The edit link will take the user/admin to a form where they can do their edits and update the tables.
Right - so far I have no problem achieving this with just a single table, its when I try to edit data from multiple tables that the prob comes in..............
Take a look at http://www.swazinet.com/accomoadmin8.php For the bottom table (one that works) the code is:
<?
// connect to the db server
$dbcnx = @mysql_connect ("localhost", "uname", "pword");
if (!$dbcnx) {
echo ("<P>Unable to connect to the " .
"database server at this time. </P>" );
exit ();
}
// Select the accomodation db
if (! @mysql_select_db("swazine_tour") ) {
echo ("<P>Unable to connect to the tourism" .
"database server at this time. </P>" );
exit ();
}
if(isset($_GET['del']))
{
$query = "DELETE FROM accomodation WHERE id = '{$_GET['del']}'";
mysql_query($query) or die('Error, cannot delete accomodation);
}
$query = "SELECT id, name, info FROM accomodation ORDER BY id";
$result = mysql_query($query) or die('Error, cannot get accomodation from database');
?>
<table width="600" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#CCCCCC">
<tr>
<td width="500"><strong>Name</strong></td>
<td width="500"><strong>Info</strong></td>
<td width="100"><strong>Action</strong></td>
</tr>
<?
while(list($id, $title, $info) = mysql_fetch_array($result, MYSQL_NUM))
{
?>
<tr>
<td width="500">
<?=$title;?>
</td>
<td width="500">
<?=$info;?>
</td>
<td width="100"><a href="../editaccomo.php?id=<?=$id;?>">edit</a> <a href="javascript:delArticle('<?=$id;?>', '<?=$title;?>')">delete</a></td>
</tr>
<?
}
?>
<tr>
<td width="500"> </td>
<td width="500"> </td>
<td width="100"> </td>
</tr>
</table>
<?
?>
I got this code from a tutorial I worked through and it works great! However I was taught via another tutorial to display data from multiple tables (like the top table in the above webpage) in a different way - the code I used is:
<?php
// connect to the db server
$dbcnx = @mysql_connect ("localhost", "uname", "pword");
if (!$dbcnx) {
echo ("<P>Unable to connect to the " .
"database server at this time. </P>" );
exit ();
}
// Select the accomodation db
if (! @mysql_select_db("swazine_tour") ) {
echo ("<P>Unable to connect to the tourism" .
"database server at this time. </P>" );
exit ();
}
?>
<p>Here is the accomodation listing</P>
<TABLE BORDER=1>
<TR><TH>ID</TH><TH>Name</TH><TH>Classification</TH><TH>Star Rating</TH><TH>Catered/Self-Catering</TH><TH>Info</TH><TH>Directions From</TH><TH>Author/Admin</TH><TH>Edit</TH></TR>
<?php
$thome = mysql_query(
"SELECT name, info, cid, sid, caid, aid, did, category, rating, catering, author, place " .
"FROM accomodation, classification, rating, catering, authors, directions WHERE cid=classification.id AND sid=rating.id AND caid=catering.id AND aid=authors.id AND did=directions.id");
If (!$thome) {
echo("</TABLE>");
echo("<p> Error retreiving!<BR>".
"Error: " . mysql_error());
exit();
}
while ($tresult = mysql_fetch_array($thome)) {
echo("<TR>\n");
$id = $tresult["id"];
$info = $tresult["info"];
$name = $tresult["name"];
$category = $tresult["category"];
$rating = $tresult["rating"];
$catering = $tresult["catering"];
$author = $tresult["author"];
$place = $tresult["place"];
// Display the information
echo( "<TD>$id</TD>\n" );
echo( "<TD>$name</TD>\n" );
echo( "<TD>$category</TD>\n" );
echo( "<TD>$rating</TD>\n" );
echo( "<TD>$catering</TD>\n" );
echo( "<TD>$info</TD>\n" );
echo( "<TD>$place</TD>\n" );
echo( "<TD>$author</TD>\n" );
echo( "<TD>test</TD>\n" );
echo("</TR>\n");
}
?>
I guess ideally I would like to use the first code but everytime I try to add tables to:
$query = "SELECT id, name, info FROM accomodation ORDER BY id";
$result = mysql_query($query) or die('Error, cannot get accomodation from database');
Using the same format as the econd code ie
$thome = mysql_query(
"SELECT name, info, cid, sid, caid, aid, did, category, rating, catering, author, place " .
"FROM accomodation, classification, rating, catering, authors, directions WHERE cid=classification.id AND sid=rating.id AND caid=catering.id AND aid=authors.id AND did=directions.id");
I get the error message 'cannot get accomodation from database'
I have also tried it vice versa - ie tried to put
"<a href="../editaccomo.php?id=<?=$id;?>"
into the second code table format with no success!
Can someone help me out?
Thanks