Here is script:
<?php
$result = mysql_query("SELECT category.ID, category.Cat, subcategory.ID, subcategory.CID, " .
"subcategory.SubCat, authors.ID, authors.Name FROM category, subcategory, authors");
if (!$result) {
echo("<P>Error retrieving from database!<BR>".
"Error: " . mysql_error());
exit();
}
$result = mysql_fetch_array($result);
$listcatid = $result[category.ID];
$listcat = $result["Cat"];
$listsubid = $result[subcategory.ID];
$listsubcid = $result["CID"];
$listsubcat = $result["SubCat"];
$listauthorid = $result[authors.ID];
$listname = $result["Name"];
?>
<table width="100%"><tr>
<td><b>Category /b><br><? echo("$listcatid. $listcat <br>"); ?></td>
<td><b>SubCategory /b><br><? echo("$listsubcid - $listsubid. $listsubcat <br>"); ?></td>
<td><b>Authors /b><br><? echo("$listauthorid. $listname <br>"); ?></td>
<br><br>
Firstly it doesn't display: $listcatid, $listsubid, $listauthorid
Secondly, the results in table only dispaly one result, is there a way, i can display every record?
"Firstly it doesn't display: $listcatid, $listsubid, $listauthorid".. make sure you select all the fields you plan to use, else use *.
I usually use the following code for loops and retrieving mysql data..
(cannot use $result = mysq_fetch.. otherwise it will not work for more than one loop).
if($myrow = mysql_fetch_array($result))
do {
echo ("results here");
} while($myrow = mysql_fetch_array($result));
else
{
echo "No records!";
}
okay, now i have this:
-------
<?php
$result = mysql_query("SELECT category.ID, category.Cat, subcategory.ID, subcategory.CID, " .
"subcategory.SubCat, authors.ID, authors.Name FROM category, subcategory, authors");
if (!$result) {
echo("<P>Error retrieving from database!<BR>".
"Error: " . mysql_error());
exit();
}
?>
# there is form and table here
<select name="authorid">
<?
while ($result = mysql_fetch_array($result)) {
$listauthorid = $result[authors.ID];
$listname = $result["Name"];
echo("<option value='$listauthorid'>$listname</option> <br>");
} ?></select>
</b></td>
<td width="114"><b>SubcategoryID: </b></td>
<td width="150"> <b>
<select name="subid">
<?
while ($result = mysql_fetch_array($result)) {
$listsubid = $result[subcategory.ID];
$listsubcat = $result["SubCat"];
echo("<option value='$listsubid'>$listsubcat</option> <br>");
} ?></select>
</b></td>
</tr>
<tr>
<td width="71"><b>Title: </b></td>
<td width="151"> <b>
<input type=TEXT name="title" size=25 maxlength=100>
</b></td>
<td width="114"><b>Category /b></td>
<td width="150"> <b>
<select name="catid">
<?
while ($result = mysql_fetch_array($result)) {
$listcatid = $result[category.ID];
$listcat = $result["Cat"];
echo("<option value='$listcatid'>$listcat</option> <br>");
} ?></select>
--------
and only the first select box displays data, if i remove that select box, the next one along the line is the only to display. How can i make al display.
!!$result = mysql_fetch_array($result))!!
If you over write the value in $result, mysql is not provided with a valid array descriptor to fetch!
pretend I had code with a command in..
$command = "Peter, go and get that data and put the value in $command";
so Peter did that..
but next time, $command said something like "two oranges, three bananas", Peter wouldn't know what to do.
so, you must do something like
$somethingdifferenttoresult = mysql_fetch_array($result))
I also notice that you are trying to use the $result over and over again for each set of list boxes. This is incorrect, you would need to create a copy of $result and then copy it in to $result each time you wanted to start from the first record again.
<Edited by PeterW on 01-06-2001 at 11:00 AM>
Got it...
Thanx
It was what you said
<Edited by petesmc on 01-06-2001 at 11:02 AM>
<edit>Okay </edit>
I dont think you understand what I mean..
<?
while ($THISMUSTBEDIFFERENT = mysql_fetch_array($TOTHIS ))
{
//...
}
?>
Oh my god....i h8 PHP
Now, it doesn't add the results to database
Only the results from all of the select boxes.....
any idea?
We would probably need to see the code to tell what is wrong
<!-- newarticle.php -->
<HTML>
<HEAD>
<TITLE> Add New article </title><meta http-equiv="expires" content="0">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache"> </head>
<BODY>
<?php
include("articles.inc");
mysql_select_db("petesmc_articles");
if ($submit): // A new article has been entered
// using the form below.
$sql = "INSERT INTO articles SET " .
"AuthorID='$aauthorid', " .
"CatID='$ccatid', " .
"SubID='$ssubid', " .
"DateAdded='CURDATE()', " .
"Description='$description', " .
"Title='$title', " .
"Status='$status', " .
"Text='$text'";
if (mysql_query($sql)) {
echo("<P>New article added</P>");
} else {
echo("<P>Error adding new article: " .
mysql_error() . "</P>");
}
?>
<P><A HREF="<?php echo($PHP_SELF); ?>">Add another article</A></P>
<P><A HREF="articles.php">Return to article list</A></P>
<?php
else: // Allow the user to enter a new article
$result = mysql_query("SELECT authors.ID, authors.Name FROM authors");
$result2 = mysql_query("SELECT category.ID, category.Cat FROM category");
$result3 = mysql_query("SELECT subcategory.ID, subcategory.SubCat FROM subcategory");
?>
<FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=GET>
</tr></table><br><br>
<P><font size="6" color="#0066FF">Enter the new Article /font><BR>
</P>
<blockquote><table width="536" border="1" cellspacing="0" cellpadding="5" align="left">
<tr>
<td width="71"><b>AuthorID /b></td>
<td width="151"> <b>
<select name="authorid" size="1">
<?
while ($resulta = mysql_fetch_array($result)) {
$listauthorid = $resulta[authors.ID];
$listname = $resulta["Name"];
echo("<option value=$listauthorid>$listname</option>");
} ?></select>
</b></td>
<td width="114"><b>SubcategoryID: </b></td>
<td width="150"> <b>
<select name=subid>
<?
while ($result3a = mysql_fetch_array($result3)) {
$listsubid = $result3a[subcategory.ID];
$listsubcat = $result3a["SubCat"];
echo("<option value=$listsubid>$listsubcat</option>");
} ?></select>
</b></td>
</tr>
<tr>
<td width="71"><b>Title: </b></td>
<td width="151"> <b>
<input type=TEXT name="title" size=25 maxlength=100>
</b></td>
<td width="114"><b>Category /b></td>
<td width="150"> <b>
<select name=catid>
<?
while ($result2a = mysql_fetch_array($result2)) {
$listcatid = $result2a[category.ID];
$listcat = $result2a["Cat"];
echo("<option value=$listcatid>$listcat</option>");
} ?></select>
</b></td>
</tr>
<tr>
<td width="71"><b>Status /b></td>
<td width="151"> <b>
<select name="status">
<option>Select Status type .....</option>
<option value="active">Active</option>
<option value="featured">Featured</option>
<option value="hidden">Hidden</option>
</select>
</b></td>
<td colspan="2"><b></b><b></b></td>
</tr>
<tr>
<td colspan="4" height="26"><b> Description: </b></td>
</tr>
<tr>
<td colspan="4"> <b>
<textarea name="description" rows=6 cols=60 WRAP></textarea>
</b></td>
</tr>
<tr>
<td colspan="4"><b>Article Text /b></td>
</tr>
<tr>
<td colspan="4"> <b>
<textarea name="text" rows=25 cols=100 WRAP></textarea>
</b></td>
</tr>
<tr>
<td colspan="4">
<input type=SUBMIT name="submit" value="SUBMIT ARTICLE">
<input type="reset" name="Reset" value="RESET">
</td>
</tr>
</table></blockquote>
<P> <BR>
</P>
</FORM>
<?php endif; ?>
</BODY>
</HTML>
$sql = "INSERT INTO articles SET " .
"AuthorID='$aauthorid', " .
"CatID='$ccatid', " .
"SubID='$ssubid', " .
"DateAdded='CURDATE()', " .
"Description='$description', " .
"Title='$title', " .
"Status='$status', " .
"Text='$text'";
By looking at your subsequent sql queries it seems you have multiple tables, so trying to insert data into fields that dont exist will not work.
I am only inserting to one table in this script.
Could you try and create a simple script, where you insert using a select box?
<Edited by petesmc on 01-06-2001 at 11:49 AM>
i did it. For some reason you can't do tis:
$listcatid = $result2a["category.ID"];
I got rid of category, and made there different queries to table:
$listcatid = $result2a["ID"];
<Edited by petesmc on 01-06-2001 at 12:11 PM>
Hmmm...
Can you do this:
<select name="authorid" size="1">
<? $result = mysql_query("SELECT ID, Name FROM authors");
while ($resulta = mysql_fetch_array($result)) {
$listauthorid = $resulta["ID"];
$listname = $resulta["Name"];
echo("<option value=$listauthorid); ?><? if ($listauthorid==$authorid) {
echo "selected"; } ?> <? echo("$listname</option>");
} ?></select>
authorid is specified above the above text. I am gettign a parse error where it say echo "selected";
What i am trying to do is, pull data from table, put into select obx, then with an if statemnt, i am saying if the id is equal to the id in the other table ($authorid), then that is selected.
Don't worry, i downloaded the files from kevin Yanks tutorials:
<SELECT NAME="authorid" SIZE=1>
<?php
$authors = mysql_query("SELECT ID, Name FROM authors");
while ($author = mysql_fetch_array($authors)) {
$listauthorid = $author["ID"];
$listaname = $author["Name"];
if ($authorid == $listauthorid) {
echo("<OPTION SELECTED VALUE='$listauthorid'>$listaname\n");
} else {
echo("<OPTION VALUE='$listauthorid'>$listaname\n");
}
}
?>
</SELECT>
That is how you do it.
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks