0

I have the following form:

<!DOCTYPE html>
<html>
<head>
<title>Update Notifications</title> 
</head>
<body>
<form enctype="multipart/form-data" action="update1.php" method="post">
<fieldset>
<legend>Enter Date and Notification, select the requisite categories</legend>       
    HOD<input type="checkbox" name="update[]" id="update" value="ihod"><br>
    CR<input type="checkbox" name="update[]" id="update" value="icr"><br>
    Non Teaching Staff<input type="checkbox" name="update[]" id="update" value="intstaff"><br>
    Teaching Staff<input type="checkbox" name="update[]" id="update" value="itstaff"><br>
    FE<input type="checkbox" name="update[]" id="update" value="ife"><br>
    SE IT<input type="checkbox" name="update[]" id="update" value="iseit"><br>
    SE EXTC<input type="checkbox" name="update[]" id="update" value="iseextc"><br>
    SE COMPS<input type="checkbox" name="update[]" id="update" value="isecomp"><br>
    TE IT<input type="checkbox" name="update[]" id="update" value="iteit"><br>
    TE EXTC<input type="checkbox" name="update[]" id="update" value="iteextc"><br>
    TE COMPS<input type="checkbox" name="update[]" id="update" value="itecomp"><br>
    BE IT<input type="checkbox" name="update[]" id="update" value="ibeit"><br>
    BE EXTC<input type="checkbox" name="update[]" id="update" value="ibeextc"><br>
    BE COMPS<input type="checkbox" name="update[]" id="update" value="ibecomp"><br>
    Notification: <input type="file" name="photo"><br> 
    Date: <input type="text" name="date"><br>       
    <input type="submit" name="Submit">
</fieldset>
</form>
</body>
</html>

My update1.php contains the following code:

<?php    
if (isset($_POST['table']) && isset($_POST['date']) && isset($_POST['pic'])       && isset($_POST['target'])){
$target = "notifications/";  //This is the directory where images will be saved 
$target = $target . basename( $_FILES['photo']['name']);  
$date=$_POST['date'];  
$pic=($_FILES['photo']['name']);   

mysql_connect("127.0.0.1", "root", "") or die(mysql_error()) ;  
mysql_select_db("login") or die(mysql_error()) ; 

foreach ($_POST[$name] as $update) {
    if($update == $value){
        $table = $_POST[$name]; 
        mysql_query("INSERT INTO `$table` VALUES ('$date', '$pic')") ;
        if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) {   
            echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";  
        }  
        else {   
            echo "Sorry, there was a problem uploading your file.";  }  
        }
    }
}

What I want to do is to store data into tables (which have already been created in a "login" database with names corresponding to checkbox values). After the checkboxes have been ticked, I would like the program to automatically update the corresponding tables with the necessary values (i.e. date and filename). I am currently confused with the foreach() syntax and how it really works. Also, can implode be used for this purpose?

PS: Is it possible to accept values using the tag in the form and store it into the database using the following method?

Tom
  • 3,450
  • 22
  • 31
Joel Menezes
  • 27
  • 1
  • 5
  • Duplicate question please have a look at this http://stackoverflow.com/q/4997252/3293985 – TheSk8rJesus Apr 01 '15 at 21:13
  • @TheSk8rJesus I'm still confused with the SQL implementation. I still cant get it to accept individual table names based on the value in the html form and then store the values in it. – Joel Menezes Apr 02 '15 at 05:25

1 Answers1

0

It looks like you are using $_POST[$name] inside your foreach when you should be using $update see below

if( isset( $_POST['update'] && !empty( $_POST['update'] ) ) {
    foreach( $_POST['update'] as $update ) {
        if( $update == $value ) {
            $table = $update;
            mysql_query( INSERT INTO '$table' VALUES( '$date', '$pic' );
        }
    }
}

Please also have a look at either mysqli or PDO as mysql functions were depreciated as of PHP 5.5.0

I have also seen that you are not posting either $_POST['table'] or $_POST['pic'] anywhere in your html, so you may need to look at this aswell.

TheSk8rJesus
  • 418
  • 2
  • 12