PHP MYSQL更改我数据库中的照片


PHP MYSQL Changing the photo on my database

我是PHP新手;

ADD.PHP -我有一个收集以下信息的表单1。名2。电子邮件3。电话和照片

图片存储在我的服务器的目录文件夹中,然后该照片的文件名存储在我的SQL表中。

VIEW.PHP - mysql中的所有数据都显示在这个页面中,包括表格格式的照片,包括每条记录的id。所显示的id是一个超链接,当单击该链接时,您将被引导到可以编辑记录内容的页面:

下面是EDIT.PHP 的代码
 <?php 
 // Connects to your Database 
 mysql_connect("localhost", "user1", "12345") or die(mysql_error()) ; 
 mysql_select_db("test") or die(mysql_error()) ;

 // Check whether the value for jobnumber is transmitted
if (isset($_GET['id'])) {
// Put the value in a separate variable
$id = $_GET['id'];
// Query the database for the details of the chosen jobnumber
$result = mysql_query("select id, name, email,
phone, picture from employees where id = $id");
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = "Invalid query: " . mysql_error() . "'n";
$message .= "Whole query: " . $query;
die($message);
}
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(),etc.
while ($row = mysql_fetch_array($result)) {
$name = $row['name'];
echo $name. "'n";
echo $row['email'] . "'n";
echo $row['phone'] . "'n";
echo "<img width=500px height=500px src=pics/" . $row['picture'].">" . "'n";
// form tag
echo '<form action="add2.php" method="POST">';
//display name
echo 'Name: <input type="text" name="name" value="';
echo $row['name'];
echo '"><br>';
//display email
echo 'email: <input type="text" name="email" value="';
echo $row['email'];
echo '"><br>';
//display phone
echo 'Phone: <input type="text" name="phone" value="';
echo $row['phone'];
echo '"><br>';
//display photo
echo 'Photo: <input type="text" name="photo" value="';
echo $row['picture'];
echo '"><br>';
echo '<input type="submit" value="Add">';
echo '</form>';
}
} else {
die("No valid data specified!");
}
?>

使用这段代码,测试字段进行得很好,但照片的输入框是空白的,当我点击按钮时,我的数据库中的照片字段将是空白的,除非我上传了一张新照片?用户如何更改现有的照片?或者保留旧照片,如果不被改变?

假设您的employees.picture列存储了图像的路径。

上传一张新照片,你需要修改一些东西…

您的表单需要使用正确的编码类型,即

<form action="add2.php" method="post" enctype="multipart/form-data">

您还需要提供一个"file"输入元素来接受新照片,例如

<input type="file" name="photo">

查看是否提供了新照片,只需检查(在检测到有效的POST请求后)

if ($_FILES['photo']['error'] == UPLOAD_ERR_OK) {
    // file uploaded
}

查看此链接:http://www.w3schools.com/PHP/php_file_upload.asp

为了更改现有的照片,你还应该在add2.php

页面中保存db表单中照片名称的名称。

当任何用户想要第二次上传照片时,则应该在add2.php中进行检查,以查找之前是否上传了照片。然后你可以从2个是,否按钮中听取用户的决定。如果是,则在db表中更新(而不是插入)相应的列。

你可以使用jquery做出决定并相应地工作。需要jquery的任何帮助?

如果这是第一次上传,那么你可以绕过检查过程。

清楚了吗?