0

我正在做基于 PHP 的数据输入程序,每件事都没有问题(仍在建设中),但有一个问题我找不到解决方案。问题是当数据库没有任何数据时,图像路径不会被注册。

现在我正在使用 uplodify 上传图像文件,同时我也在发送一些数据。数据包含名称、联系方式和描述,这将直接插入到数据库中,我还将上传图像,该图像将保存到文件夹中,名称将保存在数据库中。但是这个过程由两个不同的 PHP 文件处理,一个是 AddPOIPro.PHP,它处理数据添加和 uplodify.php,用于处理文件上传和文件名的更新。

我遇到的第一个问题是 uplodify 不是在与提交的数据相同的行中添加文件名,所以我所做的是获取最后一个 ID 并将其递减一个,并更新在有数据时有效的行,但当他们的没有数据失败。那么我该怎么做才能克服这个问题。以下是我的完整脚本,感谢您的时间。

形式

<?php

/**
 * @author SiNUX
 * @copyright 2013
 */

include ('lId.php');
?>
<!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" />

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript" src="Upl/jquery.uploadify.min.js"></script>
<script type="text/javascript" src="http://malsup.github.com/jquery.form.js"></script>

<script type="text/javascript">
$(function() {
    $('#imgUpload').uploadify({
        'auto'     : false,
        'swf'      : 'Upl/uploadify.swf',
        'uploader' : 'Upl/uploadify.php',
        'height'   : 20,
        'width'    : 200,
    }
        // Put your options here
    });
});
</script>

<script type="text/javascript">
 $(document).ready(function(){
  $("#sendData").click(function(){
    var name  = document.getElementById("Name").value;
    var desc = document.getElementById("Descrip").value;
    var con = document.getElementById("ConInfo").value;
    var dataString = 'Name='+name+'&Descrip='+desc+'&ConInfo='+con;

    $.ajax({
      type:'POST',
      data:dataString,
      url:'AddPoiPro.php',
      success:function(data){
       if(data="Data inserted") {
          //alert("Data  Success");
          document.getElementById('msg').innerHTML= "<div style=\"background-color:#0F0; text-align:center; color: #060\">Data Saved</dive>";
          $('#msg').delay(1500).fadeOut();

        } else {
          //alert("Not Inserted");
          document.getElementById('msg').innerHTML= "<div style=\"background-color:#0F0; text-align:center; color: red\">Data Not Saved</div>";
        }
     } 
   });
  });
});
</script>

<link rel="stylesheet" type="text/css" href="Upl/uploadify.css" />

<title>AddPOI</title>
</head>

<body>
<form method="post" enctype="multipart/form-data" name="form1" id="form1">
  <p>
    <label for="poiid">ID :</label>
    <input type="text" name="poiid" id="poiid" readonly="readonly" style="width:70px;" value="<?php echo $tId; ?>" />
  </p>
  <p>
    <label for="Name">POI Name :</label>

    <input type="text" name="Name" id="Name" />
  </p>
  <p>
    <label for="Descrip" style="alignment-adjust:middle">POI Description :</label>
    <textarea name="Descrip" id="Descrip" cols="45" rows="5"></textarea>
  </p>
  <p>
    <label for="ConInfo">Contact Infomation :</label>
    <textarea name="ConInfo" id="ConInfo" cols="45" rows="5"></textarea>
  </p>
  <p>
    <label for="Img">POI Image</label>
    <input type="file" name="imgUpload" id="imgUpload" />
  </p>
  <p><div id="msg"></div></p>
  <p>  
  <div align="center">
    <input type="button" name="Submit" id="sendData" value="Submit" onclick="$('#imgUpload').uploadify('upload','*');" style="width:100px;" />
    <input type="reset" name="reset" id="reset" value="Rest Data" style="width:100px;" />
  </div>
  </p>
</form>
</body>
</html>

处理 PHP

<?php

/**
 * @author SiNUX
 * @copyright 2013
 */

include ('connect.php');

$getId = mysql_query("SELECT ID FROM poiinfo");
$row = mysql_fetch_array($getId);

$poiName = $_REQUEST['Name'];
$poiDes = $_REQUEST['Descrip'];
$poiCon = $_REQUEST['ConInfo'];

if($row['ID']== 1){

    $updLn = "UPDATE `poiinfo` SET `Name`='$poiName',`Des.`='$poiDes',`Contact`='$poiCon' WHERE 1";
    $updDone = mysql_query($updLn);
    if ($updDone){
        echo "Data inserted";
    }else {
        echo "Not Done";
    }  

}else {
 $dbData = "INSERT INTO poiinfo(`Name`, `Des.`, `Contact`) VALUES ('$poiName','$poiDes','$poiCon')";

        $putData = mysql_query($dbData);
        if ($putData){
            echo "Data inserted";
        }else {
            echo "Not Done";
        }
 }       
?>

上传 php

<?php
/*
Uploadify
Copyright (c) 2012 Reactive Apps, Ronnie Garcia
Released under the MIT License <http://www.opensource.org/licenses/mit-license.php> 
*/

include ('../../POIWeb/connect.php');
include ('../../POIWeb/lId.php');


// Define a destination
$path = 'POIWeb/img/';
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $path ; // Relative to the root

/*$verifyToken = md5('unique_salt' . $_POST['timestamp']);

if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
    $targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];

    // Validate the file type
    $fileTypes = array('jpg','jpeg','gif','png'); // File extensions
    $fileParts = pathinfo($_FILES['Filedata']['name']);

    if (in_array($fileParts['extension'],$fileTypes)) {
        move_uploaded_file($tempFile,$targetFile);

        echo $fName;
        echo '1';
    } else {
        echo 'Invalid file type.';
    }
}*/

//if (!empty($_FILES)) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetFile = $targetPath . $_FILES['Filedata']['name'];

    // Validate the file type
    $fileTypes = array('jpg','jpeg','gif','png'); // File extensions
    $fileParts = pathinfo($_FILES['Filedata']['name']);    
    $tId = $tId--;


    if (in_array($fileParts['extension'],$fileTypes)) {
        move_uploaded_file($tempFile,$targetFile);

        $fName = $_FILES['Filedata']['name'];
        $imgPath = "UPDATE poiinfo SET Img = '$fName' WHERE ID = '$tId'";
        mysql_query($imgPath);  

     echo '1';       

    } else {
        echo 'Invalid file type.';
    }
?>

请帮忙。

4

3 回答 3

0

我发现我的问题的解决方案从 StackOverflow 中的人那里得到了一些帮助 下面是我的完整代码,如果有人遇到与我类似的问题,您可以使用此代码。如果它对你有用,请给我投票,谢谢。

我的数据输入表

<?php

/**
 * @author SiNUX
 * @copyright 2013
 */

include ('lId.php');
?>
<!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" />

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript" src="Upl/jquery.uploadify.min.js"></script>

<script type="text/javascript">
$(function() {
    $(function() {
        $('#imgUpload').uploadify({
            'auto'     : false,
            'swf'      : 'Upl/uploadify.swf',
            'uploader' : 'Upl/uploadify.php',
            'height'   : 20,
            'width'    : 200,
            'fileTypeDesc' : 'Image Files',
            'fileTypeExts' : '*.gif; *.jpg; *.png'
    });
        // Put your options here
    });
});
</script>

<script type="text/javascript">
 $(document).ready(function(){
  $("#save_data").click(function(){
    var name  = document.getElementById("Name").value;
    var desc = document.getElementById("Descrip").value;
    var con = document.getElementById("ConInfo").value;

    var dataString = 'Name='+name+'&Descrip='+desc+'&ConInfo='+con;

    $.ajax({
      type:'POST',
      data:dataString,
      url:'AddPoiPro.php',
      success:function(data){
       if(data="Data inserted") {
          //alert("Data  Success");
          document.getElementById('msg').innerHTML= "<div style=\"background-color:#0F0; text-align:center; color: #060\">Data Saved</dive>";
          $('#msg').delay(1500).fadeOut();
        } else {
          //alert("Not Inserted");
          document.getElementById('msg').innerHTML= "<div style=\"background-color:#0F0; text-align:center; color: red\">Data Not Saved</div>";
          $('#msg').delay(1500).fadeOut();
        }
     } 
   });
  });
});
</script>

<link rel="stylesheet" type="text/css" href="Upl/uploadify.css" />

<title>AddPOI</title>
</head>

<body>
<form method="post" enctype="multipart/form-data" name="form1" id="form1">
  <p>
    <label for="poiid">ID :</label>
    <input type="text" name="poiid" id="poiid" readonly="readonly" style="width:70px;" value="<?php echo $tId; ?>" />
  </p>
  <p>
    <label for="Name">POI Name :</label>

    <input type="text" name="Name" id="Name" />
  </p>
  <p>
    <label for="Descrip" style="alignment-adjust:middle">POI Description :</label>
    <textarea name="Descrip" id="Descrip" cols="45" rows="5"></textarea>
  </p>
  <p>
    <label for="ConInfo">Contact Infomation :</label>
    <textarea name="ConInfo" id="ConInfo" cols="45" rows="5"></textarea>
  </p>
  <p>
    <label for="Img">POI Image</label>
    <input type="file" name="imgUpload" id="imgUpload" />
  </p>
  <p><div id="msg"></div></p>
  <p>  
  <div align="center">
    <input type="button" name="Submit" id="save_data" value="Submit" onclick="$('#imgUpload').uploadify('upload','*');" style="width:100px;" />
    <input type="reset" name="reset" id="reset" value="Rest Data" style="width:100px;" />
  </div>
  </p>
</form>
</body>
</html>

我的数据处理 PHP

<?php

/**
 * @author SiNUX
 * @copyright 2013
 */

include ('connect.php');

$getId = mysql_query("SELECT ID FROM dbname");
$row = mysql_fetch_array($getId);

$poiName = $_REQUEST['Name'];
$poiDes = $_REQUEST['Descrip'];
$poiCon = $_REQUEST['ConInfo'];

if($row['ID']== 1){

    $updLn = "UPDATE `poiinfo` SET `Name`='$poiName',`Des.`='$poiDes',`Contact`='$poiCon' WHERE 1";
    $updDone = mysql_query($updLn);

    if ($updDone){
        echo "Data inserted";
    }else {
        echo "Not Done";
    }  

}else {
    $dbData = "INSERT INTO poiinfo(`Name`, `Des.`, `Contact`) VALUES ('$poiName','$poiDes','$poiCon')";        
    $putData = mysql_query($dbData);

    if ($putData){
        echo "Data inserted";
    }else {
        echo "Not Done";
    }
 }       
?>

这对于uploadify PHP 来说真的很棘手,但让它以我想要的方式工作。

上传 PHP(我的版本)

<?php
/*
Uploadify
Copyright (c) 2012 Reactive Apps, Ronnie Garcia
Released under the MIT License <http://www.opensource.org/licenses/mit-license.php> 
*/

include ('../../POIWeb/connect.php');
include ('../../POIWeb/lId.php');


// Define a destination
$path = 'POIWeb/img/';
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $path ; // Relative to the root

/*$verifyToken = md5('unique_salt' . $_POST['timestamp']);

if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
    $targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];

    // Validate the file type
    $fileTypes = array('jpg','jpeg','gif','png'); // File extensions
    $fileParts = pathinfo($_FILES['Filedata']['name']);

    if (in_array($fileParts['extension'],$fileTypes)) {
        move_uploaded_file($tempFile,$targetFile);

        echo $fName;
        echo '1';
    } else {
        echo 'Invalid file type.';
    }
}*/

//if (!empty($_FILES)) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetFile = $targetPath . $_FILES['Filedata']['name'];

    // Validate the file type
    $fileTypes = array('jpg','jpeg','gif','png'); // File extensions
    $fileParts = pathinfo($_FILES['Filedata']['name']);    
    $tId = $tId--;


    if (in_array($fileParts['extension'],$fileTypes)) {
        move_uploaded_file($tempFile,$targetFile);

        $fName = $_FILES['Filedata']['name'];
        $imgPath = "UPDATE poiinfo SET Img = '$fName' WHERE ID = '$tId'";
        mysql_query($imgPath);  

     echo '1';       

    } else {
        echo 'Invalid file type.';
    }
?>

如果有人面临与我遇到的相同情况,则此脚本现在可以完美运行,并查看它是否适合您,并且您也可以进行任何更改。

谢谢你。

于 2013-01-29T06:42:32.307 回答
0

我认为这将是一个更好的方法

 $("#sendData").click(function(){
    //upload the file programatically (Method Call'upload')[http://www.uploadify.com/documentation/uploadify/upload/]
    // echo the name of the uploaded file (make sure to rename the file for uniquiness)
    // create a hidden element with the filename as the value inside the form
    // call the ajax submit and submit the form. You can use (onuploadsuccess)[http://www.uploadify.com/documentation/uploadify/onuploadsuccess/] to trigger the element creation and ajax submit functions
 });

您当前的实现可能会导致问题考虑,
请求 1:ID 10
上传 1:ID 10
请求 2:ID 11
请求 3:ID 12
上传 2:ID 12
上传 3:ID 12

于 2013-01-24T09:41:26.373 回答
0

要获取 Last id,您可以按以下方式进行查询

SELECT `ID`
FROM `poiinfo`
ORDER BY `ID` DESC
LIMIT 0 , 1

如果你遇到任何问题,那么你可以敲我..

于 2013-01-24T09:18:43.560 回答