I am creating a backend system for my website using php, mysql etc.
I have a database setup along with my required tables.
What I am attempting to do at this point is:
1)Select the usernames from the table 'users' and display them in a drop down menu.
2)Insert other information into various other forum fields such as "name", "comment", "date" etc.
3)Upon the form being submitted, the information is inserted into a table called 'reports'.
Now the thing is, I have successfully created a dynamic drop down menu that displays a list of the usernames in the table 'users'. I have also, on a seperate php page, successfully created a form that inserts the form information into my other table, called 'reports'.
My problem is merging the two PHP code sections together so that they both work at the same time.
Here is my coding for the dynamic drop down menu:
<?php
include ('mysql_connect.php');
?>
<?php
$result=mysql_query("SELECT username FROM users") or die(mysql_error());
echo "<select name=myselect>";
while($row=mysql_fetch_assoc($result)) {
echo "<option value=$row[username]>$row[username]</a></option>";
}
echo "</select>";
?>
Here is my coding for inserting other information from a form into a table:
<form name="form1" method="post" action="addreport.php">
<div align="center"><br>
<table border="0" cellspacing="0" cellpadding="3">
<tr>
<td align="right"><span class="style1">Name:</span></td>
<td align="left"><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td align="right"><span class="style1">Comment:</span></td>
<td align="left"><input name="comment" id="comment"></td>
</tr>
<tr>
<td colspan="2">
<div align="right">
<input name="submit" type="submit" class="style2" id="submit" value="Add">
</div></td>
</tr>
</table>
</div>
</form>
<p align="center"> <a href="reports.php" class="style1">Back</a></p>
<p> <br>
</p>
</td>
</tr>
</table></td>
</tr>
<tr>
<td><img src="images/prs_footer.jpg" width="780" height="17"></td>
</tr>
</table>
</body>
</html>
<?php
if (isset($_POST['submit'])) {
require_once ('mysql_connect.php');
function escape_data ($data) {
global $dbc;
if (ini_get('magic_quotes_gpc')) {
$data = stripslashes($data);
}
return mysql_real_escape_string($data, $dbc);
}
$message = NULL;
if (empty($_POST['comment'])) {
$c = FALSE;
$message .= '<p>Please enter a comment</p>';
} else {
$c = escape_data($_POST['comment']);
}
if (empty($_POST['name'])) {
$n = FALSE;
$message .= '<p>Please enter a name</p>';
} else {
$n = escape_data($_POST['name']);
}
if ($n && $c) {
$query = "SELECT (username) FROM `users` UNION INSERT INTO `reports` (name, comment, date) VALUES ('$n', '$c', NOW())";
$result = @mysql_query ($query);
if ($result) {
echo "<br><center><font face=\"verdana\" size=\"2\" color=\"#FFFFFF\">Report successfully added</font></center><meta http-equiv=\"REFRESH\" content=\"0; URL=reports.php\">";
exit();
} else {
$message = 'Report could not be added';
}
} else {
$message .= '<p>Failed</p>';
}
}
echo $message;
?>
How do I merge these 2 sections? As far as the queries are concerned, I have tried simply using one and using "UNION" in order to combine the select query and the insert query. (there is also a thread right next to this one regarding 2 queries and a method is mentioned about simply adding a second "mysql-query(...). I am also a bit confused as to what order everything would go in as certain pieces of coding have to precede others.
Any and all help would be greatly appreciated!
Tirus
Edited by Tirus, 16 August 2006 - 10:07 PM.
