我正在做的是在提交表单数据时显示最后插入的数据,表单是multipart / form-data。我使用jquery获取此表单数据,这里我使用Ajax POST将此数据发送到php文件。在那个php文件中我将数据插入db table..where我得到插入数据的id ..成功Ajax调用我将该id发送到另一个PHP文件..使用该id我正在显示最后插入的数据......
我的表格是:
<form method="post" enctype="multipart/form-data" name="upload_form" id="data">
<select id="sel">
<option>Select the Project Stream</option>
<option value="1">Computer Science</option>
<option value="2">Mechanical</option>
<option value="3">IT</option>
<option value="4">Web Development</option>
<option value="5">MCA</option>
<option value="6">Civil</option>
</select><br />
<input type="text" id="title" placeholder="Project Title"/><br />
<input type="text" id="vurl" placeholder="If You have any video about project write your video url path here" style="width:435px;"/><br />
<textarea id="prjdesc" name="prjdesc" rows="20" cols="80" style="border-style:groove;box-shadow: 10px 10px 10px 10px #888888;"placeholder="Please describe Your Project"></textarea>
<label for="file">Filename:</label>
<input type="file" name="file" id="file"/><br />
<button>Submit</button>
</form>
我的js文件:
$("form#data").submit(function() {
alert("update");
var sid=$("#sel").val();
alert(sid);
var ttle = $("#title").val();
alert(ttle);
var text = $("#prjdesc").val();
var vurl = $("#vurl").val();
/*var dataString = 'param='+text+'¶m1='+vurl+'¶m2='+ttle+'¶m3='+id;*/
var formData = new FormData($(this)[0]);
formData.append('param',text);
formData.append('param1',vurl);
formData.append('param2',ttle );
formData.append('param3',sid );
$.ajax({
type:'POST',
data:formData,
url:'insert.php',
success:function(id) {
alert(id);
window.location ="another.php?id="+id;
},
cache: false,
contentType: false,
processData: false
});
return false;
});
insert.php:
<?php
print_r($_FILES);
$desc = $_POST['param'];
echo $desc;
$video = $_POST['param1'];
echo $video ;
$title = $_POST['param2'];
echo $title;
$tech_id=$_POST['param3'];
echo $tech_id;
$host="localhost";
$username="root";
$password="";
$db_name="geny";
$tbl_name="project_details";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
$url_dir = "C:/wamp/www/WebsiteTemplate4/upload/";
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 50000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
if (file_exists($url_dir . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],$url_dir. $_FILES["file"]["name"]);
// echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
$tmp = "C:/wamp/www/WebsiteTemplate4/upload/" . $_FILES["file"]["name"];
$sql="INSERT INTO $tbl_name (title, content, img_path, video_url, project_tech_Id) VALUES ('$title','$desc','$tmp','$video','$tech_id')";
if(mysql_query($sql)) {
echo mysql_insert_id();
} else {
echo "Cannot Insert";
}
}
}
}
else
{
echo "Invalid file";
}
?>
another.php:
<?php
$temp=$_GET['id'];
echo $temp;
$host="localhost";
$username="root";
$password="";
$db_name="geny";
$tbl_name="project_details";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$query = mysql_query("SELECT content FROM project_details WHERE id=". $temp);
if (!$query)
{
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($query);
echo "<div id='uprjct' style='background:#336699;'>
<p>$row[0]</p>
</div>";
?>
但我在insert.php中返回的ID包含元素数组...我不想要所有这些东西我只想要ID(这是数字).. 请告诉我我的代码有什么问题......
答案 0 :(得分:1)
不推荐使用mysql_ *,而是使用mysqli_ *或pdo。
在mysqli_ *中,您通过mysqli_insert_id($link);
获取了ID。 mysqli_insert_id
$database->lastInsertId();
pdo lastInsertId 在pdo中
答案 1 :(得分:0)
在你的文件insert.php中你有
<?php
print_r($_FILES);
$desc = $_POST['param'];
echo $desc;
....
echo $video;
....
echo mysql_insert_id();
你的脚本中有很多'echo'打印$ desc和$ video的值,print_r函数在插入id之前打印完整数组$ _FILES。
删除除echo mysql_insert_id ();
顺便说一下,尝试使用mysqli_ *方法,不推荐使用mysql_方法。
修改强>
删除print_r($_FILES);
语句,从insert.php中删除无用的打印件,这样只会打印id。
要显示图像,您只需使用从查询中检索到的img_path创建一个:
<?php
$temp=$_GET['id'];
$host="localhost";
$username="root";
$password="";
$db_name="geny";
$tbl_name="project_details";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$query = mysql_query("SELECT content, img_path FROM project_details WHERE id=". $temp);
if (!$query)
{
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($query);
echo "<div id='uprjct' style='background:#336699;'>".
"<img src='".$row['img_path']."' alt='myimg' />".
"<p>".$row['content']."</p></div>";
?>