Jump to content


Creating PHP and MySql Form


2 replies to this topic

#1 Tirus

    P2L Jedi

  • Members
  • PipPipPip
  • 764 posts
  • Gender:Male
  • Location:Montreal, Canada
  • Interests:Web Design, Programming, Music, Martial Arts

Posted 16 August 2006 - 08:46 PM

Hello Everyone. (excuse the spelling error in the title...meant to write merging :P )

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.


#2 NGPixel

    Senior Programmer

  • P2L Staff
  • PipPipPipPip
  • 1,410 posts
  • Gender:Male
  • Location:Montreal, Canada
  • Interests:Web Design : Coding : Animation

Posted 16 August 2006 - 10:47 PM

It should work :P , please use correct indents next time lol, it was hard to read.

<?php

include ('mysql_connect.php');

if(isset($_POST['submit2'])) {

	function escape_data ($data) {
		global $dbc;
			if (ini_get('magic_quotes_gpc')) {
				$data = stripslashes($data);
			}
			return mysql_real_escape_string($data, $dbc);
	}
	
	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;

if(!isset($_POST['myselect'])) {

?>

<form name="form1" action="addreport.php" method="post">
	<select name="myselect">
	<?php
	$result=mysql_query("SELECT username FROM users") or die(mysql_error());
	while($row=mysql_fetch_assoc($result)) {
		echo "<option value=$row[username]>$row[username]</a></option>";
	}
	?>
	</select> <input name="submit" type="submit" value="Submit" />
	</form>
	
<?php 

} else {

?>

<form name="form2" action="addreport.php" method="post">
<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" value="<?= stripslashes($_POST['myselect']); ?>"></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" align="right">
		<input name="submit2" type="submit" class="style2" id="submit" value="Add">
	</td>
  </tr>
</table>
</div>
</form>
<p align="center"> <a href="reports.php" class="style1">Back</a></p> 

<?php } ?>


#3 Tirus

    P2L Jedi

  • Members
  • PipPipPip
  • 764 posts
  • Gender:Male
  • Location:Montreal, Canada
  • Interests:Web Design, Programming, Music, Martial Arts

Posted 16 August 2006 - 11:35 PM

:P sorry about the indents

so just to reply quickly, since i should really be getting to bed, i copy/pasted the exact coding and I am able to select the username, the page then changes to show the name and comment field with the username in the name field. I press submit and it says report cannot be added.

Also, I am not trying to import the username from the drop down menu into the name field. I am attemping to have something where I choose a username, I.E. Tirus, then a Name for the report (which would be located just under the username drop down menu), I.E. "Report on System Bugs", and put a comment etc etc.

I really do appreciate that you took the time help, tomorrow I am going to sit down and possibly play with the coding you gave me in order to achieve what I want...hopefully you solved my merging problem as ordering the php coding properly was a little confusing.

feel free to jump back in and give it another go (or anyone else for that matter if you want :P )

Again, thanks for taking the time to help, I will reply here tomorrow with my progress.

Tirus.


EDIT: I managed to get it working the way I wanted. Thank you anyways for your help NGPixel. :)

Edited by Tirus, 19 August 2006 - 08:17 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users