Hi all, I want to ask how i insert the selected data (Institution Name) and the input text data (School name) into MYSQL?? Thanks~
my database as below:

--
-- Database: `inno_education`
--

-- --------------------------------------------------------

--
-- Table structure for table `institution`
--

CREATE TABLE IF NOT EXISTS `institution` (
`i_id` int(11) NOT NULL AUTO_INCREMENT,
`i_name` varchar(100) NOT NULL,
`address` varchar(1000) NOT NULL,
`website` varchar(100) NOT NULL,
PRIMARY KEY (`i_id`),
UNIQUE KEY `i_name` (`i_name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `institution`
--


/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;



--
-- Database: `inno_education`
--

-- --------------------------------------------------------

--
-- Table structure for table `school`
--

CREATE TABLE IF NOT EXISTS `school` (
`s_id` int(11) NOT NULL AUTO_INCREMENT,
`s_name` varchar(100) NOT NULL,
`institute` int(11) NOT NULL,
PRIMARY KEY (`s_id`),
KEY `FK_institute` (`institute`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `school`
--


--
-- Constraints for dumped tables
--

--
-- Constraints for table `school`
--
ALTER TABLE `school`
ADD CONSTRAINT `school_ibfk_1` FOREIGN KEY (`institute`) REFERENCES `institution` (`i_id`) ON DELETE CASCADE ON UPDATE CASCADE;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
<?php
$connection = mysql_connect("localhost", "root", "") or die("Could not connect to MySQL " .mysql_error() );
$selection = mysql_select_db("inno_education") or die("Unable to select database. " .mysql_error());
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
var counter = 0;

function moreFields(){
counter++;
var newFields = document.getElementById('readroot').cloneNode(true);
newFields.id = '';
newFields.style.display = 'block';
var newField = newFields.childNodes;
for (var i=0;i<newField.length;i++) 
{
var theName = newField[i].name
if (theName)
newField[i].name = theName + counter;
}
var insertHere = document.getElementById('writeroot');
insertHere.parentNode.insertBefore(newFields,insertHere);
}

window.onload = moreFields;
</script>
</head>

<body>
<table>
<tr>
<td>Institution Name</td>
<td>:</td>
<td>
<?php $sql = "SELECT i_name from institution";
// execute query
$result = mysql_query($sql);
if(isset($_POST["InstitutionName"]))
$InstitutionName = $_POST["InstitutionName"];
echo"<select name='InstitutionName'>";
while ($row = mysql_fetch_array($result))
{ $i_name=$row["i_name"];
echo"<option value='$i_name'";
if (isset($InstitutionName) && $InstitutionName ==$i_name)
echo " selected> $i_name";
else	echo "> $i_name"; }
echo "</select><br>"; 
?>
</td>
</tr>
</table>
<div id="readroot" style="display: none">
<input type="button" value="Remove" onclick="this.parentNode.parentNode.removeChild(this.parentNode);" />
<table>
<tr>
<td>School Name</td>
<td>:</td>
<td><input name="s_name[]" type="text" size="30" maxlength="100"></td>
</tr>
</table>
</div>

<form method="post" action="">

<span id="writeroot"></span>

<input type="button" value="Add" onClick="moreFields()"/>
<input type="submit" name="send" value="Send form" />

</form>

</body>
</html>

Recommended Answers

Check whether you are able to see the values after posting the data.

Just try printing your data after submitting the data

echo "<pre>";
print_r($_REQUEST);
echo "</pre>";

The main problem in your code is you have to put your select box code inside the form. Then …

Jump to Post

All 3 Replies

Check whether you are able to see the values after posting the data.

Just try printing your data after submitting the data

echo "<pre>";
print_r($_REQUEST);
echo "</pre>";

The main problem in your code is you have to put your select box code inside the form. Then only you can see the submitted data.

<?php
$connection = mysql_connect("localhost", "root", "")  or die("Could not connect to MySQL " .mysql_error() );
$selection = mysql_select_db("inno_education")  or die("Unable to select database.  " .mysql_error());

for ($x=0; $x<count($_POST['i_name']); $x++) {
$Name=mysql_real_escape_string($_POST['i_name'][$x]);
$SchoolName=mysql_real_escape_string($_POST['s_name'][$x]);
$sql = "INSERT INTO school (i_name, s_name) VALUES ('".$Name."', '".$SchoolName."');";
$result = mysql_query($sql, $connection) or die ("Error inserting record");
}
?>
<body>
<?php
	echo "<form action='' name='form'>";
	echo "<table align='left' border ='0' width = '100%'>";
	echo "<tr bgcolor='d5d5d5'><td width ='20%'><strong>Institution Name</strong></td><td width ='60%'><strong>School Name</strong> </td></tr>";
for ($x=0; $x<count($_POST['s_name']); $x++){
$Name=$_POST['i_name'][$x];
$SchoolName=$_POST['s_name'][$x];
echo "<tr bgcolor='e5e5e5'><td> $Name </td><td>$SchoolName</td></tr>";}
echo "</table>";   
echo "</form>";
?>
</body>

I am trying like this but it just show school name and it do not save to database... can u help me??

Where is your submit button, where is your select query, How you are fetching and displaying the results with out select query?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.