0

I am very new to the Mysql/php environment so would appreciate guidance here please.
1) I have a php program that uses a Form to enter data into a MySQL table. This is working OK when data is entered into the fields manually.
2) I have a short php script that reads data from another table and creates a drop down list of the data. This is working OK too.

Now I want to substitue the drop down list for the existing input box on the form, and for the user 'selected data' in the php list to be saved in the appropriate field when the form is submitted.

I have pasted the php script just above the form input box which I want to replace.
See code:

<form action="ADD2.php" method="post" style="padding: 5px; height: 550px" class="style13">

<?php
$con = mysql_connect($host,$user,"$pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db($db, $con);
$query="SELECT reporter,id FROM swaag_reporters";
/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */
$result = mysql_query ($query);
echo "<select name=swaag_reporters value=''>reporter</option>";
// printing the list box select command
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[reporter]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box 
//$nt[1]=$_POST['recorder_by'];
?>


Recorded by:&nbsp;  <input type="text"  size="30" name="recorded_by" />

In effect I want to save the selected 'reporter' from the drop down list, to be posted where the existing name="recorded_by is being saved by the current form entry.

What do I need to do?

Many thanks for taking time to reply

Stephen

3
Contributors
3
Replies
15
Views
7 Years
Discussion Span
Last Post by Yuges
0

Try this:
Database name : news
Table holding information about reports : reporters
Column names in the reporters table : reporter_id, name

Table holding news items reported by your reporters : reports
Columns in the reports table : report_id, reporter_id, report, date

Note: The report_id is assigned automatically by mysql

Here is the code:

<?php
$connection = mysql_pconnect("localhost", "root", "password") or die("Error connecting. ".mysql_error());
mysql_select_db("news") or die("Error selecting db. ".mysql_error());

if(!isset($_POST['save']))
{
	$query = "SELECT * FROM reporters ORDER BY name ASC";

	$result = mysql_query($query) or die("Error in in query. ".mysql_error());

	echo "<form method = 'post' action = '".$_SERVER['PHP_SELF']."'>";
	echo "<table width = '450' align = 'center'>";
	echo "<tr>";
	echo "<td align = 'right'>Reporter : &nbsp;</td>";
	echo "<td>";
		echo "<select name = 'reporter'>";
			echo "<option value = ''>Select reporter</option>";
			while($reporter = mysql_fetch_object($result))
			{
				echo "<option value = '".$reporter->reporter_id."'>".$reporter->name."</option>";
			}
		echo "</select>";
	echo "</td>";
	echo "</tr>";
	echo "<tr>";
		echo "<td align = 'right'>Report : &nbsp;</td>";
		echo "<td>";
		echo "<textarea name = 'report' cols = '40' rows = '5'></textarea>";
		echo "</td>";
	echo "</tr>";
	echo "<tr>";
		echo "<td></td>";
		echo "<td>";
		echo "<input type = 'submit' name = 'save' value = 'SAVE REPORT'>";
		echo "<input type = 'reset' value = 'CLEAR FORM'>";
		echo "</td>";
	echo "</tr>";
	echo "</table>";
	echo "</form>";
}
else
{
	if(empty($_POST['reporter']) || empty($_POST['report']))
	{
		echo "Either you didn't select a reporter or the report field is empty!";
	}
	else
	{
		$report = mysql_real_escape_string($_POST['report']);
		$query = "INSERT INTO reports (reporter_id,report, date) VALUES('".$_POST['reporter']."','$report', NOW())";
		
		if(mysql_query($query))
		{
			echo "Report successfully sent!";
		}
		else
		{
			echo "Unknown error has occurred.";
		}
	}
}

?>
0

Many thanks for your reply - much appreciated

A little knowledge is a great gift!

Thanks

Stephen

0

hi Mr cossay, if i want to display all the reports in table like in database, when user select their reporter what i need to do?
Thanks.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.