duanpaotian2348 2012-03-08 13:24
浏览 26
已采纳

使用PHP和MYSQL上传信息时不需要使用表单字段

I need to make my form field not required when uploading information. Here is the scenario and code:

Scenario: A user wants to edit their information, without updating a different image: But obviously when i click submit and leave the image field blank it throws a error, how can i prevent this.

The field i want to be not required is the Image field (photo)

Here is code:

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

And here is the 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>
");


 //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条回答 默认 最新

  • duancai7568 2012-03-08 13:28
    关注

    Just wrap in an 'if' statement...

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

    ... of course need a similar if statement on the move_uploaded_file block too.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿