HTML:
<select name="Name" id="">
<?php
while ($row = mysql_fetch_array($result)) {
?>
<option value="<?php $row['Name']; ?>"><?php echo $row['Name']; ?> </option>
<?php
} ?>
</select>
PHP:
$Name=$_POST['Name'];
if (isset ($_POST['submit']))
{
//$Name=$_POST['Name'];
$ins=mysql_query("INSERT INTO Colleges (Name) VALUES ('$Name')") or die(mysql_error()) ;
if($ins)
{
echo "<br>".$Name."inserted";
}
else
{
echo mysql_error();
}
}
整个表单代码:
<form method="post" action="template-insertdataprocessor.php" enctype="multipart/form-data" style="width: 80%; background: lightblue none repeat scroll 0% 0%; border: 2px solid darkcyan; margin-right: 55px; padding: 35px; margin-left: 55px;">
<p> Upload your school logo in gif or jpeg format. The file name should be named after the school's name. If the same file name is uploaded twice it will be overwritten! </p>
<p> Logo: </p>
<input type="hidden" name="size" value="350000">
<input type="file" name="schoollogo"><br>
<br><table><tr><td style="width: 15%;"><p> Name of School:</td><td style="width: 42.5%;">
<?php
//$host = "localhost:3306";
//$db_name="univers1_test";
//$user = "";
//$pass = "";
//$conn = new mysqli($host, $user, $pass, $db_name) or die("DB Connection failed!!");
mysql_connect("localhost:3306", "", "") or die(mysql_error()) ;
mysql_select_db("univers1_test") or die(mysql_error()) ;
$sql = "SELECT Name FROM Colleges2";
$result = mysql_query($sql) or die(mysqli_error()) ;
//$stmt=$conn->query($sql);
?>
<select name="Name" id="">
<?php
while ($row = mysql_fetch_array($result)) {
?>
<option value="<?php echo $row['Name']; ?>"><?php echo $row['Name']; ?> </option>
<?php
} ?>
</select>
</td></p></tr></table>
<br><br><table><tr><td style="width: 15%;"><p>Brief description/history of college
</td><td style="width: 42.5%;"><textarea rows="10" cols="35" name="Description"> </textarea> </td><td style="width: 42.5%;">eg: "Built in 1880, UP was constructed by Engineer John Smith....It was recognized as an educational institution on.....It remains to be a leader in......"</p></td></tr></table>
<script type="text/javascript">
CKEDITOR.replace( 'Description' );
</script>
<br><br>
<table><tr><td style="width: 15%;"><p>Term Type? (eg: Quarter, Semester) </td><td style="width: 42.5%;"><input style="width: 330px;" type="text" name="TermType"/> </td><td style="width: 42.5%;">eg: Quarter</p></td></tr></table>
<table><tr><td style="width: 15%;"><p>Entrance Exam Required? </td><td style="width: 42.5%;"><input style="width: 330px;" type="text" name="EntranceExam"/> </td><td style="width: 42.5%;">eg: No</p></td></tr></table>
<table><tr><td style="width: 15%;"><p>What is your college's website URL? (Please make sure your input is correct) </td><td style="width: 42.5%;"><input style="width: 330px;" type="text" name="collegewebsite"/> </td><td style="width: 42.5%;">eg: http://www.up.edu.ph</p></td></tr></table>
<table><tr><td style="width: 15%;"><p>What is the best way for a student or parent to contact the school? Any contact info?
</td><td style="width: 42.5%;"><textarea rows="10" cols="35" name="ContactInfo"> </textarea> </td><td style="width: 42.5%;">eg: Email and Phone<br><br>Email: name@up.edu.ph<br>Phone: 123-4567</p></td></tr></table>
<script type="text/javascript">
CKEDITOR.replace( 'ContactInfo' );
</script>
<br><br><table><tr><td style="width: 15%;"><p> Campus Locations (please make sure your input is correct): </td><td style="width: 42.5%;"><input style="width: 330px;" type="text" name="CampusLocations"/> </td><td style="width: 42.5%;">eg: Diliman, Baguio, Visayas, Los Banos....</p></td></tr></table>
<table><tr><td style="width: 15%;"><p> Certifications Offered:
</td><td style="width: 42.5%;"><textarea rows="10" cols="35" name="Certifications"> </textarea> </td><td style="width: 42.5%;">eg: Computer Repair<br>Health Tech<br>Electrician</p></td></tr></table>
<script type="text/javascript">
CKEDITOR.replace( 'Certifications' );
</script>
<br><br><table><tr><td style="width: 15%;"><p> Diploma Courses Offered:
</td><td style="width: 42.5%;"><textarea rows="10" cols="35" name="Diplomas"> </textarea> </td><td style="width: 42.5%;">eg: Computer Tech<br>Advanced Health Tech<br>Advanced Electrician</p></td></tr></table>
<script type="text/javascript">
CKEDITOR.replace( 'Diplomas' );
</script>
<br><br><table><tr><td style="width: 15%;"><p> Associate (2-Year) Degrees Offered:
</td><td style="width: 42.5%;"><textarea rows="10" cols="35" name="AssociateDegrees"> </textarea> </td><td style="width: 42.5%;">eg: Nursing<br>IT<br>Web development</p></td></tr></table>
<script type="text/javascript">
CKEDITOR.replace( 'AssociateDegrees' );
</script>
<br><br><table><tr><td style="width: 15%;"><p> Bachelors (4-Year) Degrees Offered:
</td><td style="width: 42.5%;"><textarea rows="10" cols="35" name="BachelorsDegrees"> </textarea> </td><td style="width: 42.5%;">eg: Nursing<br>Computer Engineering<br>Math<br>Marketing</p></td></tr></table>
<script type="text/javascript">
CKEDITOR.replace( 'BachelorsDegrees' );
</script>
<br><br><table><tr><td style="width: 15%;"><p> Masters (5-6 Year) Degrees Offered:
</td><td style="width: 42.5%;"><textarea rows="10" cols="35" name="MastersDegrees"> </textarea> </td><td style="width: 42.5%;">eg: Business Administration<br>Nursing<br>Math</p></td></tr></table>
<script type="text/javascript">
CKEDITOR.replace( 'MastersDegrees' );
</script>
<br><br><table><tr><td style="width: 15%;"><p>Doctorate Degrees Offered
</td><td style="width: 42.5%;"><textarea rows="10" cols="35" name="DoctorateDegrees"> </textarea> </td><td style="width: 42.5%;">eg: Theology<br>Math<br>Philosophy<br>Medicine</p></td></tr></table>
<script type="text/javascript">
CKEDITOR.replace( 'DoctorateDegrees' );
</script>
<br><br><table><tr><td style="width: 15%;"><p>Does your college offer any special degree programs?
</td><td style="width: 42.5%;"><textarea rows="10" cols="35" name="SpecialDegreePrograms"> </textarea> </td><td style="width: 42.5%;">eg: ETEEAP</p></td></tr></table>
<script type="text/javascript">
CKEDITOR.replace( 'SpecialDegreePrograms' );
</script>
<br><br><table><tr><td style="width: 15%;"><p> Accreditations:
</td><td style="width: 42.5%;"><textarea rows="10" cols="35" name="Accreditation"> </textarea> </td><td style="width: 42.5%;">eg: ABET<br>PAASCU</p></td></tr></table>
<script type="text/javascript">
CKEDITOR.replace( 'Accreditation' );
</script>
<br>
<input TYPE="submit" name="upload" title="Add data to the Database" value="Add College"/>
</form>
整个PHP代码:
<?php
ini_set("display_errors",1); error_reporting(E_ALL);
//This is the directory where images will be saved
$targetscreenshots = "images/";
$targetscreenshots = $targetscreenshots . basename( $_FILES['files[]']['name']);
$target = "images/logo/";
$target = $target . basename( $_FILES['schoollogo']['name']);
$targetotherimage = "images/otherimage/";
$targetotherimage = $targetotherimage . basename( $_FILES['otherimage']['name']);
//This gets all the other information from the form
$schoollogo=($_FILES['schoollogo']['name']);
$otherimage=($_FILES['otherimage']['name']);
$Name=$_POST['Name'];
$Motto=$_POST['Motto'];
$Description=$_POST['Description'];
$Type=$_POST['Type'];
$ReligiousAffiliation=$_POST['ReligiousAffiliation'];
$OtherPartnerships=$_POST['OtherPartnerships'];
$GenderAdmission=$_POST['GenderAdmission'];
$TermType=$_POST['TermType'];
$EntranceExam=$_POST['EntranceExam'];
$EntranceExamDate=$_POST['EntranceExamDate'];
$TotalEnrolled=$_POST['TotalEnrolled'];
$collegewebsite=$_POST['collegewebsite'];
$ContactInfo=$_POST['ContactInfo'];
$CampusLocations=$_POST['CampusLocations'];
$Certifications=$_POST['Certifications'];
$Diplomas=$_POST['Diplomas'];
$AssociateDegrees=$_POST['AssociateDegrees'];
$BachelorsDegrees=$_POST['BachelorsDegrees'];
$MastersDegrees=$_POST['MastersDegrees'];
$DoctorateDegrees=$_POST['DoctorateDegrees'];
$SpecialDegreePrograms=$_POST['SpecialDegreePrograms'];
$Accreditation=$_POST['Accreditation'];
$CostofTuitionLocal=$_POST['CostofTuitionLocal'];
$CostofTuitionForeign=$_POST['CostofTuitionForeign'];
$Housing=$_POST['Housing'];
$AcceptanceRate=$_POST['AcceptanceRate'];
$CoE=$_POST['CoE'];
$CoD=$_POST['CoD'];
//$files=($_FILES['files[]']['name']);
// Connects to your Database
mysql_connect("localhost:3306", "", "") or die(mysql_error()) ;
mysql_select_db("univers1_test") or die(mysql_error()) ;
//Writes the information to the database
$text = trim($_POST['BachelorsDegrees']);
$textAr = explode("\n", $text);
$textAr = array_filter($textAr, 'trim'); // remove any extra \r characters left behind
foreach ($textAr as $BachelorsDegrees) {
mysql_query("INSERT INTO Colleges (BachelorsDegrees) VALUES ('$BachelorsDegrees')") or die(mysql_error()) ;
}
if (isset ($_POST['submit']))
{
//$Name=$_POST['Name'];
$ins=mysql_query("INSERT INTO Colleges (Name) VALUES ('$Name')") or die(mysql_error()) ;
if($ins)
{
echo "<br>".$Name."inserted";
}
else
{
echo mysql_error();
}
}
mysql_query("INSERT INTO Colleges (schoollogo,Motto,Description,Type,ReligiousAffiliation,OtherPartnerships,GenderAdmission,TermType,EntranceExam,EntranceExamDate,TotalEnrolled,collegewebsite,ContactInfo,CampusLocations,Certifications,Diplomas,AssociateDegrees,MastersDegrees,DoctorateDegrees,SpecialDegreePrograms,Accreditation,CostofTuitionLocal,CostofTuitionForeign,Housing,AcceptanceRate,CoE,CoD) VALUES ('$schoollogo', '$Motto', '$Description', '$Type', '$ReligiousAffiliation', '$OtherPartnerships', '$GenderAdmission', '$TermType', '$EntranceExam', '$EntranceExamDate', '$TotalEnrolled', '$collegewebsite', '$ContactInfo', '$CampusLocations', '$Certifications', '$Diplomas', '$AssociateDegrees', '$MastersDegrees', '$DoctorateDegrees','$SpecialDegreePrograms','$Accreditation','$CostofTuitionLocal','$CostofTuitionForeign','$Housing','$AcceptanceRate','$CoE','$CoD')") or die(mysql_error()) ;
//Writes the logo to the server
if(move_uploaded_file($_FILES['schoollogo']['tmp_name'], $target))
//&& (move_uploaded_file($_FILES['otherimage']['tmp_name'], $targetotherimage)) //optional second input single-upload
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
//Writes the screenshots to the server
//$imageArr = array();
//foreach ($_FILES['files']['error'] as $key => $error)
// {
// if($error == UPLOAD_ERR_OK) {
// $tmp_name = $_FILES["files"]["tmp_name"][$key];
// $name = $_FILES["image"]["name"][$key];
// move_uploaded_file($tmp_name, $targetscreenshots/$name");
// array_push($imageArr,$name);
// }
// }
//$sql=mysql_query("INSERT into Colleges SET files='".json_encode($imageArr)."'"); // inserting data if file is moved
//if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
$valid_formats = array("jpg", "png", "gif", "zip", "bmp");
$max_file_size = 1024*100; //100 kb
$count = 0;
$upath="../images/";
// Loop $_FILES to execute all files
foreach ($_FILES['files']['name'] as $key => $file) {
if ($_FILES['files']['error'][$key] == 4) {
continue; //Skip file if any error found
}
if ($_FILES['files']['error'][$key] == 0) {
if ($_FILES['files']['size'][$key] > $max_file_size) {
$message[] = "$file is too large!.";
continue; //Skip large files
}
elseif( ! in_array(pathinfo($file, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$file is not a valid format";
continue; //Skip invalid file formats
}
else{ //No error found! Move uploaded files
$target = $upath.$file;
$path=substr($target,3);
if(move_uploaded_file($_FILES["files"]["tmp_name"][$key], $targetscreenshots.$file))
mysql_query("INSERT into Colleges (`files`) VALUES ('$path')"); //inserting data if file is moved
$count++; //Number of successfully uploaded file
echo "yay";
}
}
}
//}
//}
?>
注意:
没有错误或成功消息。 Name列只有一个NULL值插入其中。
答案 0 :(得分:0)
<option value="<?php $row['Name']; ?>"><?php echo $row['Name']; ?> </option>
应该是
<option value="<?php echo $row['Name']; ?>"><?php echo $row['Name']; ?> </option>
你错过了echo关键字。
答案 1 :(得分:0)
<select name="Name" id="">
<?php
while ($row = mysql_fetch_array($result)) {
?>
<option value="<?php echo $row['Name']; ?>"><?php echo $row['Name']; ?></option>
<?php
}
?>
</select>
答案 2 :(得分:0)
还尝试使用mysqli
或PDO
。
<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "yourdb";
mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database);
if (isset ($_POST['submit']))
{
$Name=trim($_POST['Name']);
$ins=mysql_query("INSERT INTO Colleges (Name) VALUES ('$Name')") or die(mysql_error()) ;
if($ins)
{
echo "<br>".$Name." inserted";
}
else
{
echo mysql_error();
}
}
?>
<form method="post">
<select name="Name">
<?php
$result = mysql_query("select Name from Colleges");// your select query
while ($row = mysql_fetch_array($result)) {
?>
<option value="<?php echo $row['Name']; ?>"><?php echo $row['Name'];?> </option>
<?php
} ?>
</select>
<input type="submit" name="submit" value="submit">
</form>