-2

我尝试制作一个包含图片上传的表格。假设我有participant要插入的表格:

INSERT INTO `participant`(`Matric`, `Name`, `IC`, `Address`, `Tel`, `Phone`, 
`Email`, `Phone_Ref`, `Institute`, `Course`, `Pic_Participant`, `Exp_Work`) 
VALUES ([value-1],[value-2],[value-3],[value-4],[value-5],[value-6],[value-7],
[value-8],[value-9],[value-10],[value-11],[value-12])

我想要做的是插入数据并上传图像。它的属性是Pic_Participant

我搜索使用 ajax Ajax Image Upload 和 Resize with jQuery 和 PHP 的上传。然后我想流程,填写表格然后在同一页面上传图片,然后在上传图片后将图片数据发送到数据库,但表格还没有提交。如何从表格图像中获取属性以添加到表格参与者中?

请帮我。我对此很陌生。


编辑

我尝试这段代码但得到一个错误:未定义的变量

 <?php

session_start();

include 'dbconnect.php';

function is_valid_type($file)
{
$valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif");

if (in_array($file['type'], $valid_types))
return 1;
return 0;
}


function showContents($array)
{
echo "<pre>";
print_r($array);
echo "</pre>";
}


$TARGET_PATH = "upload/";

//ERROR START HERE

$Matric = $_POST['Matric'];
$Name = $_POST['Name'];
$IC = $_POST['IC'];
$Address = $_POST['Address'];
$Tel = $_POST['Tel'];
$Phone = $_POST['Phone'];
$Email = $_POST['Email'];
$Phone_Ref = $_POST['Phone_Ref'];
$Institute = $_POST['Institute'];
$Course = $_POST['Course'];
/* $fname = $_POST['fname'];
$lname = $_POST['lname']; */
$image = $_FILES['image'];
$Exp_Work =$_POST['Exp_Work'];
//ERROR END HERE


$Matric = mysql_real_escape_string($Matric);
$Name = mysql_real_escape_string($Name);
$IC = mysql_real_escape_string($IC);
$Address = mysql_real_escape_string($Address);
$Tel = mysql_real_escape_string($Tel);
$Phone = mysql_real_escape_string($Phone);
$Email = mysql_real_escape_string($Email);
$Phone_Ref = mysql_real_escape_string($Phone_Ref);
/* $Total_sales = addslashes($_POST['Total_sales']);
$Date = addslashes($_POST['Date']); */
/* $Cer_name = mysql_real_escape_string($Cer_name); */
$Institute = mysql_real_escape_string($Institute);
$Course = mysql_real_escape_string($Course);
/* $Cat_name = addslashes($_POST['Cat_name']);
$Product_name = addslashes($_POST['Product_name']); */
/* $fname = mysql_real_escape_string($fname);
$lname = mysql_real_escape_string($lname); */
$image['name'] = mysql_real_escape_string($image['name']);
$Exp_Work = mysql_real_escape_string($Exp_Work);


$TARGET_PATH .= $image['name'];


if ( $Matric == "" ||$Name == "" ||$IC == "" ||$Address == "" ||$Tel == "" ||$Phone == "" ||$Email == "" ||$Phone_Ref == "" || $Institute == "" || $Course == ""|| $image['name'] == ""|| $Exp_Work == "" )
{
$_SESSION['error'] = "All fields are required";
echo "All fields are required";
exit;
}


if (!is_valid_type($image))
{
$_SESSION['error'] = "You must upload a jpeg, gif, or bmp";
echo"You must upload a jpeg, gif, or bmp";
exit;
}


if (file_exists($TARGET_PATH))
{
$_SESSION['error'] = "A file with that name already exists";
echo"A file with same name exists already";
exit;
}


if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))
{

$sql = "insert into participant (Matric, Name, IC, Address, Tel, Phone, Email, Phone_Ref, Institute, Course, image, Exp_Work) values ('$Matric','$Name','$IC','$Address','$Tel','$Phone','$Email','$Phone_Ref','$Institute', '$Course','" . $image['name'] . "','$Exp_Work')";
$result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());
echo"Imgage uploaded successfully";
exit;
}
else
{

$_SESSION['error'] = "Could not upload file. Check read/write persmissions on the directory";
header("Location: fail.php");
exit;
}
?>
4

2 回答 2

0

如果我正确理解了您想要的是将图像插入(可能是 MYSQL)数据库。

您可以通过使用base64_encode()图像二进制数据并在数据库中插入生成的纯文本来实现这一点。

$image = 'path/to/image/image.png';
$imagefordbs  =  base64_encode($image);
/*now your image is ready to be stored in database*/

但是,这种方法有缺点,因为 base64_encode() 占用的内存比原始内存多 33%,需要一些时间来处理输入二进制文件,并且 mysql - BLOB中有专门的数据类型来满足这种要求。

于 2013-03-16T17:20:27.050 回答
0

当我阅读您的问题时,您希望已经使用您找到的教程/代码上传图像,然后在图像已经上传时单独提交表单。

要知道文件上传后在哪里可以找到图像,您有两种选择:

  1. 上传完成后让图片上传返回文件路径,并在表单中包含该变量(例如作为隐藏输入);
  2. 将图像路径存储在会话变量中,以便在提交数据字段时,可以访问该变量以获取图像的信息。

编辑:您需要查看表单插件的文档以获取更多详细信息,但您可以从上传的 php 脚本中返回一些内容。例如echo,您可以使用文件名,然后在您的success函数中使用它:

    function afterSuccess(return_value)  {
        console.log(return_value);  // here you have what was echoed out by php

        $('#UploadForm').resetForm();  // reset form
        $('#SubmitButton').removeAttr('disabled'); //enable submit button
    }
于 2013-03-16T17:25:17.213 回答