js脚本写入php_PHP脚本上传图像并写入MySQL

js脚本写入php

Website owners use PHP and MySQL database management software to enhance their website capabilities. Even if you want to allow a site visitor to upload images to your web server, you probably don't want to bog down your database by saving all the images directly to the database. Instead, save the image to your server and keep a record in the database of the file that was saved so you can reference the image when needed. 

网站所有者使用PHPMySQL数据库管理软件来增强其网站功能。 即使您希望允许站点访问者将图像上传到Web服务器,您也可能不想通过将所有图像直接保存到数据库来破坏数据库。 而是将映像保存到服务器,并在保存的文件的数据库中保留一条记录,以便您可以在需要时引用该映像。

创建一个数据库 ( Create a Database )

First, create a database using the following syntax:

首先,使用以下语法创建数据库:

This SQL code example creates a database called visitors that can hold names, email addresses, phone numbers, and the names of the photos.

这个SQL代码示例创建一个名为游客数据库,可容纳姓名,电子邮件地址,电话号码和照片的名称。

建立表格 ( Create a Form )

Here is an HTML form that you can use to collect information to be added to the database. You can add more fields if you want, but then you'd also need to add the appropriate fields to the MySQL database.

这是一个HTML表单,您可以用来收集要添加到数据库中的信息。 您可以根据需要添加更多字段,但随后还需要将适当的字段添加到MySQL数据库。


<form enctype="multipart/form-data"
action="add.php" method="POST">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name = "email"><br>
Phone: <input type="text" name = "phone"><br>
Photo: <input type="file" name="photo"><br>
<input type="submit" value="Add">   </form>

处理数据 ( Process the Data )

To process the data, save all the following code as add.php. Basically, it gathers the information from the form and then writes it to the database. When that is done, it saves the file to the /images directory (relative to the script) on your server. Here is the necessary code along with an explanation of what is going on.

要处理数据,请将以下所有代码另存为add.php 。 基本上,它从表单收集信息,然后将其写入数据库。 完成后,它将文件保存到服务器上的/ images目录(相对于脚本)。 这是必要的代码,并对发生的情况进行了解释。

Designate the directory where the images will be saved with this code:

使用以下代码指定将图像保存到的目录:


<?php
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']); 

Then retrieve all the other information from the form: 

然后从表单中检索所有其他信息:


$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$pic=($_FILES['photo']['name']); 

Next, make the connection to your database: 

接下来,建立与数据库的连接:


mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()) ;
mysql_select_db("Database_Name") or die(mysql_error()) ; 

This writes the information to the database: 

这会将信息写入数据库:


mysql_query("INSERT INTO 'visitors' VALUES ('$name', '$email', '$phone', '$pic')") ; 

This writes the photo to the server 

这会将照片写入服务器


if(move_uploaded_file($_FILES['photo']['tmp_name'],$target))

This code tells you if it is all ok or not.

该代码告诉您是否一切正常。


echo "The file ". basename( $_FILES['uploadedfile']
['name']). " has been uploaded, and your information has been added to the directory";
}
else {echo "Sorry, there was a problem uploading your file."; }?> 

If you only allow photo uploads, consider limiting the allowed file types to JPG, GIF, and PNG. This script doesn't check if the file already exists, so if two people both upload a file called MyPic.gif, one overwrites the other. A simple way to remedy this is to rename each incoming image with a unique ID.

如果仅允许照片上传,请考虑将允许的文件类型限制为JPG,GIF和PNG。 该脚本不会检查文件是否已存在,因此,如果两个人都上传了一个名为MyPic.gif的文件,一个将覆盖另一个。 解决此问题的一种简单方法是使用唯一ID重命名每个传入的图像。

查看您的数据 ( View Your Data )

To view the data, use a script like this one, which queries the database and retrieves all the information in it. It echos each back until it has shown all the data.

要查看数据,请使用类似于此脚本的脚本,该脚本查询数据库并检索其中的所有信息。 它回显每个对象,直到显示了所有数据。


<?php
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()) ;
mysql_select_db("Database_Name") or die(mysql_error()) ;
$data = mysql_query("SELECT * FROM visitors") or die(mysql_error());
while($info = mysql_fetch_array( $data )) {
Echo "<img src=http://www.yoursite.com/images/".$info['photo'] ."> <br>"; Echo "<b>Name:</b> ".$info['name'] . "<br> "; Echo "<b>Email:</b> ".$info['email'] . " <br>"; Echo "<b>Phone:</b> ".$info['phone'] . " <hr>"; } ?>

To show the image, use normal HTML for the image and only change the last part—the actual image name—with the image name stored in the database. More information on retrieving information from the database can be found in a PHP MySQL tutorial.

要显示图像,请对图像使用普通HTML,并仅使用存储在数据库中的图像名称更改最后一部分(实际图像名称)。 有关从数据库检索信息的更多信息,请参见PHP MySQL教程。

翻译自: https://www.thoughtco.com/upload-a-file-and-write-to-mysql-2694113

js脚本写入php

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值