What i am doing is i am displaying last inserted data when form data is submitted,the form is multipart/form-data. I am getting this form data using jquery,here i am sending this data to php file using Ajax POST.In that php file i am inserting that data in db table..where i am getting the id of inserted data..on success of Ajax call i am sending that id to another PHP file..where using that id i am displaying the last inserted data...
My form is:
<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>
My js file:
$("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>";
?>
but the ID what i am returning in insert.php containg array of elements...i dont want all these thing i want only ID(which is number)..
please tell me what is wrong in my code...