-2

我正在尝试让我的表单使用我的数据库更新特定记录。

我有一个编辑页面来编辑记录,现在已经为该特定记录的文档添加了一个上传表单,但是当它上传时,它只会使用捕获的数据创建一个新行,即文件名和上传的文件被正确放置,只是没有根据相关记录。

我让它工作了!

渐弱上传表单所在的表格:

    <table cellpadding="5" cellspacing="0">
 <form enctype="multipart/form-data" action="includes/add.php?ID=<?php echo "$idnumber" ?>" method="POST"> 
  <input type="hidden" name="idenumber" value="<?php echo $idnumber; ?>"/>
 <th>Ad Warnings Documents</th>
 <tr>
 <td>Warning File 1</td>
 <td><input type="file" name="warning1" value="<?php echo $warning1;?>" /></td>

 </tr>
  <tr>
 <td>Warning File 2</td>
 <td><input type="file" name="warning2" value="<?php echo $warning2;?>" ></td>

 </tr>
  <tr>
 <td>Warning File 3</td>
 <td><input type="file" name="warning3" value="<?php echo $warning3;?>">

</td>
 </tr>

  <tr><td><input type="submit" value="Add"> </td></tr>
 </table>

最终的工作上传脚本

        <?php
    include 'core/init.php';
     // Connects to your Database 
    include 'includes/overall/header.php';
    error_reporting(1);
    $idnumber = 'idnumber';
    $connect_error = 'Sorry, we\'re experiencing connection problems.';
    mysql_connect('localhost', '', '') or die($connect_error);
    mysql_select_db('') or die($connect_error);
   $query= "SELECT idnumber FROM ref_employees WHERE  $idnumber = `idnumber`" or die($connect_error);

     $idnumber=$_POST['idnumber'] ;
      //This is the directory where images will be saved 
    $target = "../files/empdocs/"; 
    $target1 = $target . basename( $_FILES['warning1']['name']); 
    $target2 = $target . basename( $_FILES['warning2']['name']);
    $target3 = $target . basename( $_FILES['warning3']['name']);
     //This gets all the other information from the form 


     $warning1=($_FILES['warning1']['name']); 
     $warning2=($_FILES['warning2']['name']);
     $warning3=($_FILES['warning3']['name']); 


      //Writes the information to the database 
  $q = "UPDATE ref_employees SET `warning1` = '$warning1',
                               `warning2` = '$warning2',
                               `warning3` = '$warning3'
         WHERE $idnumber=`idnumber`";
$res = mysql_query( $q );
if ( ! $res ) {
    // an error occured
}
      //Writes the file to the server 
    if (move_uploaded_file($_FILES['warning1']['tmp_name'], $target1))
    echo "The file ". basename( $_FILES['warning1']['name']). " has been uploaded, and your information has been added to the directory"; 
    if (move_uploaded_file($_FILES['warning2']['tmp_name'], $target2))
     echo "The file ". basename( $_FILES['warning2']['name']). " has been uploaded, and your information has been added to the directory"; 
    if (move_uploaded_file($_FILES['warning3']['tmp_name'], $target3))
        echo "The file ". basename( $_FILES['warning3']['name']). " has been uploaded, and your information has been added to the directory";
     //Tells you if its all ok 

     else { 

     //Gives and error if its not 
     echo "Sorry, there was a problem uploading your file."; 

     }
     ?>

     <?php
    include 'includes/overall/footer.php'; ?>

感谢大家的帮助和指导。

4

2 回答 2

3

尝试

mysql_query("UPDATE ref_employees 
  SET `warning1`='$warning1',`warning2`='$warning2',`warning3`='$warning3' 
  WHERE `idnumber`='$idnumber'"
  ) or die(mysql_error());
于 2013-10-14T20:27:21.317 回答
2

然后您需要在 INSERT sql 中包含 idnumber。

//Writes the information to the database 
mysql_query("INSERT INTO ref_employees (`idnumber`, `warning1`,`warning2`,`warning3`) 
                    VALUES ('$idnumber','$warning1', '$warning2', '$warning3')")  or die(mysql_error());

或者,如果该行已存在于表中,而您只想将此信息添加到现有行中,则需要进行更新

$q = "UPDATE ref_employees SET `warning1` = '$warning1',
                               `warning2` = '$warning2',
                               `warning3` = '$warning3'
         WHERE `idnumber` = '$idnumber'";
$res = mysql_query( $q );
if ( ! $res ) {
    // an error occured
}
于 2013-10-14T20:06:37.213 回答