2012-03-08 45 views
0

上传信息时,我不需要填写表单字段。这里是场景和代码:使用PHP和MYSQL上传信息时不需要填写表单字段

场景:用户想要编辑他们的信息,而不更新不同的图像:但显然,当我点击提交并将图像字段留空时,它会引发错误,我该如何防止这种情况。

我想是不是必需的领域是像场(照片)

这里是代码:

<?php 
session_start(); 
$UserName = $_SESSION['UserName']; 
require("checkLoginSession.php"); 
$adminid = $_GET['id']; 

//removed connection 
$tbl_name="admin"; // Table name 

// Connect to server and select database. 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 


echo("Logged In As: $UserName"); 
echo "<br />"; 
echo("We are editing Data for ID: $adminid"); 
echo "<br />"; 
echo "<a href=test.php>Go back to panel</a>"; 

$id=$_GET['id']; 
// Retrieve data from database 
$sql="SELECT * FROM admin WHERE id='$id'"; 
$result=mysql_query($sql) or die(mysql_error()); 

$rows=mysql_fetch_array($result); 
?> 
<table width="400" border="0" cellspacing="1" cellpadding="0"> 
<tr> 
<form enctype="multipart/form-data" name="form1" method="post" action="update_ac.php"> 
<td> 
<table width="100%" border="0" cellspacing="1" cellpadding="0"> 
<tr> 
<td>&nbsp;</td> 
<td colspan="3"><strong>Update data in mysql</strong> </td> 
</tr> 
<tr> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
</tr> 
<tr> 
<td align="center">&nbsp;</td> 
<td align="center"><strong>Name</strong></td> 
<td align="center"><strong>Main Content</strong></td> 
<td align="center"><strong>Image Locatoin</strong></td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td align="center"><input name="name" type="text" id="name" value="<? echo $rows['name']; ?>"></td> 
<td align="center"><input name="mainContent" type="text" id="mainContent" value="<? echo $rows['mainContent']; ?>" size="15"></td> 
<td align="center"><input name="photo" type="file" id="photo"> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"></td> 
<td align="center"><input type="submit" name="Submit" value="Submit"></td> 
<td>&nbsp;</td> 
</tr> 
</table> 
</td> 
</form> 
</tr> 
</table> 

<? 
mysql_close(); 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Jonathon Legg - University Year Book - Edit Page</title> 
</head> 

<body> 
<h2>Edit Page (<?php echo ("$adminid"); ?>)</h2> 
<a href="test.php"><img src="backtopanel.jpg" alt="back to panel" width="454" height="85" border="0" longdesc="back to panel" /></a> 
</body> 
</html> 

这里是update_acknowledge.php

<?php 
session_start(); 
$UserName = $_SESSION['UserName']; 
require("checkLoginSession.php"); 
include "common.php"; 
DBConnect(); 
$Link = mysql_connect($Host, $User, $Password); 

//connection removed for stack overflow // 
$tbl_name="admin"; // Table name 

$target = "images/"; 
$target = $target . basename($_FILES['photo']['name']); 
// Connect to server and select database. 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 

// update data in mysql database 
$_POST = array_map("mysql_real_escape_string", $_POST); 
$firstName = $_POST["name"]; 
$mainText = $_POST["mainContent"]; 
$pic=($_FILES['photo']['name']); 


// 



$sql="UPDATE admin SET name='$firstName', mainContent='$mainText', photo='$pic' WHERE id='$id'"; 

// 

if (mysql_db_query ($DBName, $sql, $Link)){ 
print ("A record was created <br><a href=index.php> return to index </a>\n"); 


//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 

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


} else { 

print ("Record not created"); 
} 

mysql_close($Link); 
?> 

回答

2

只是包装在'if'声明中...

if (!empty($_FILES['photo']['name'])) { 
    $pic=mysql_real_escape_string($_FILES['photo']['name']); 

    $sql="UPDATE admin SET name='$firstName', mainContent='$mainText', photo='$pic' WHERE id='$id'"; 
} else { 
    $sql="UPDATE admin SET name='$firstName', mainContent='$mainText' WHERE id='$id'"; 
} 

......当然在move_uploaded_file块中也需要类似的if语句。

+0

感谢队友,听起来很完美,谢谢! – 2012-03-08 13:53:36

+0

将现在测试并找回你。 – 2012-03-08 13:54:09

+0

工作完美的巴里,我将尝试下一个块,将发布代码,看看它是否看起来不错。 – 2012-03-09 11:41:12

0

我已经把图像逻辑条件,如果($ _ FILES [ '照片'] [ '错误'] == 0)

<?php 
session_start(); 
$UserName = $_SESSION['UserName']; 
require("checkLoginSession.php"); 
include "common.php"; 
DBConnect(); 
$Link = mysql_connect($Host, $User, $Password); 

//connection removed for stack overflow // 
$tbl_name="admin"; // Table name 

// Connect to server and select database. 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 

// update data in mysql database 
$_POST = array_map("mysql_real_escape_string", $_POST); 
$firstName = $_POST["name"]; 
$mainText = $_POST["mainContent"]; 


// 

$sql = "UPDATE admin SET name='$firstName', mainContent='$mainText'"; 

if($_FILES['photo']['error'] == 0) { 
    $target = "images/"; 
    $target = $target . basename($_FILES['photo']['name']); 
    $pic=($_FILES['photo']['name']); 
    $sql .= ", photo='$pic'"; 
} 

$sql .= "WHERE id='$id'"; 

// 

if (mysql_db_query ($DBName, $sql, $Link)) { 
    print ("A record was created <br><a href=index.php> return to index </a>\n"); 

    if($_FILES['photo']['error'] == 0) { 
     //Writes the photo to the server 
     if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { 
      //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."; 
     } 
    } 
} else { 
    print ("Record not created"); 
} 

mysql_close($Link); 
?> 
+0

谢谢队友,非常感谢。 – 2012-03-08 13:53:27

+0

将现在测试并回复给您。 – 2012-03-08 13:53:55

相关问题