2015-04-01 307 views
0

我有以下形式:基于通过复选框获取的表将数据存储在SQL中?

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

我update1.php包含以下代码:

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

我想要做的是把数据存储进表(这已经在创建“登录”数据库的名称与复选框值相对应)。勾选复选框后,我希望程序自动更新具有必要值(即日期和文件名)的相应表格。我目前困惑于foreach()语法以及它如何工作。另外,可以使用implode来达到这个目的吗?

PS:是否可以使用表单中的标记接受值,并使用以下方法将其存储到数据库中?

+0

重复的问题请看看这个http://stackoverflow.com/q/4997252/3293985 – TheSk8rJesus 2015-04-01 21:13:08

+0

@ TheSk8rJesus我仍然与SQL实现混淆。我仍然无法根据html表单中的值接受单独的表名,然后将值存储在其中。 – 2015-04-02 05:25:05

回答

0

它看起来像你正在使用$_POST[$name]您的foreach内部时,你应该使用$update见下文

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'); 
     } 
    } 
} 

还请看看在任mysqliPDO作为mysql职能折旧为PHP 5.5.0的

我也看到你没有发布$_POST['table']$_POST['pic']你的html的任何地方,所以你可能需要看看这个。

相关问题