0

The HTML:

<select name="Name" id="">
<?php
while ($row = mysql_fetch_array($result)) {
?>
<option value="<?php $row['Name']; ?>"><?php echo $row['Name']; ?>     </option>
<?php
} ?>
</select> 

The 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();
}
}

The entire form code:

<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>

The entire PHP code:

<?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";
        }
    }
}
//}
//}

?>

Take note:

  • I've tried mysqli to no avail...not sure why but it never seems to work for me.
  • I've read some duplicated posts.....still not sure what is wrong.

No error or success message. The Name column just has a NULL value inserted into it.

user2984757
  • 29
  • 3
  • 8
  • Are you POSTing or GETing in the `
    ` tag
    – RiggsFolly Jul 04 '16 at 09:47
  • 1
    you forgot to echo the value value="" – JYoThI Jul 04 '16 at 09:48
  • Have you looked in your `php error log` for these error messages – RiggsFolly Jul 04 '16 at 09:49
  • use echo : ``....let me know if it works – J. Zend Jul 04 '16 at 09:50
  • please post complete form and as @jothi mentioned please place `echo` – Vivek Singh Jul 04 '16 at 09:50
  • @RiggsFolly - POSting. I've posted the entire code. – user2984757 Jul 04 '16 at 10:10
  • @VivekSingh - I just did. Still inserting NULL. – user2984757 Jul 04 '16 at 10:11
  • Please dont use [the `mysql_` database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the `PDO` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) its really pretty easy – RiggsFolly Jul 04 '16 at 10:25
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared statement and parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Jul 04 '16 at 10:26
  • So if I switch to mysqli or PDO, will that fix the problem I am having of the NULL value being inserted into the database instead of the actual selected value? Trying to figure out what code is screwed up besides me not using mysqli/PDO. – user2984757 Jul 04 '16 at 10:32
  • @J.Zend - didn't work. code updated. – user2984757 Jul 04 '16 at 10:47
  • Just letting everyone know that after staring at my code (and making myself look insane in the process) for a long time, I noticed that the $_POST was not using the variable of the name attribute on the submit button which is "upload". Changed it to --> $Name=trim($_POST['Name']); and if (isset ($_POST['upload'])) Seems to be working fine now. The data is inserting into the database. – user2984757 Jul 04 '16 at 11:56

3 Answers3

0
<option value="<?php $row['Name']; ?>"><?php echo $row['Name']; ?>     </option>

should be

<option value="<?php echo $row['Name']; ?>"><?php echo $row['Name']; ?>     </option>

You were missing the echo keyword.

simonv
  • 99
  • 1
  • 2
  • 6
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> 
  • A **good answer** will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO that may find this question and be reading your answer. – RiggsFolly Jul 04 '16 at 10:24
0

Also try to use mysqli or 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>
Passionate Coder
  • 7,154
  • 2
  • 19
  • 44