我正在尝试使用我的数据库来更新特定记录。
我已编辑页面来编辑记录,现在已为该特定记录添加了上传表格,但在上传时只会创建一个包含捕获数据的新行,即文件名和上传文件正确放置,只是没有相关记录。
我开始工作了!
上传表格逐渐减弱的表格:
<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'; ?>
感谢大家的帮助和指导。
答案 0 :(得分:3)
尝试
mysql_query("UPDATE ref_employees
SET `warning1`='$warning1',`warning2`='$warning2',`warning3`='$warning3'
WHERE `idnumber`='$idnumber'"
) or die(mysql_error());
答案 1 :(得分: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
}