...

View Full Version : PHP Upload and İnserting Data



Buk
02-05-2010, 02:58 AM
Hello everybody,

I have a script that upload image and inserts values name of image, age and gender to database. But i have encountered a problem.

The problem is that, name of image inserts correct to database but age and gender inserts wrong 0(default value). but i want to insert value i get from form.

Also, if you have any suggestions about improve the code, i want to hear them.

Thank you..

imageupload.htm



<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="imageupload.php" method="post" enctype="multipart/form-data" name="imageupload">
<table width="384" height="195" border="0" align="center">
<tr>
<td width="121" height="50">select picture</td>
<td width="12">:</td>
<td width="237"><input name="image" type="file" /></td>
</tr>
<tr>
<td height="46">age</td>
<td>:</td>
<td><select name="age">
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
<option value="32">32</option>
<option value="33">33</option>
<option value="34">34</option>
<option value="35">35</option>
<option value="36">36</option>
<option value="37">37</option>
<option value="38">38</option>
<option value="39">39</option>
<option value="40">40</option>
<option value="41">41</option>
<option value="42">42</option>
<option value="43">43</option>
<option value="44">44</option>
<option value="45">45</option>
<option value="46">46</option>
<option value="47">47</option>
<option value="48">48</option>
<option value="49">49</option>
<option value="50">50</option>
</select> </td>
</tr>
<tr>
<td height="44">gender</td>
<td>:</td>
<td><select name="gender">
<option value="female">Female</option>
<option value="male">Male</option>
</select></td>
</tr>
<tr>
<td colspan="3"><div align="center">

<label> <br />
<input type="submit" name="submit" id="bas" value="Submit" />
<br />
</label>

</div></td>
</tr>
</table>

</form>





</body>
</html>



imageupload.php


<?php
include("baglanti.php");
if($_POST['image'] = "" || $_POST['gender'] = "" || $_POST['age'] = "") {
echo "Fill all blanks";
}else{
$image=$_POST['image'];
$gender=$_POST['gender'];
$age=$_POST['age'];
$imagename=$_FILES["image"]["name"];


//uploading image

if ((($_FILES["image"]["type"] == "image/gif")
|| ($_FILES["image"]["type"] == "image/jpeg")
|| ($_FILES["image"]["type"] == "image/pjpeg"))
&& ($_FILES["image"]["size"] < 200000))
{
if ($_FILES["image"]["error"] > 0)
{
echo "Return Code: " . $_FILES["image"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["image"]["name"] . "<br />";
echo "Type: " . $_FILES["image"]["type"] . "<br />";
echo "Size: " . ($_FILES["image"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["image"]["tmp_name"] . "<br />";

if (file_exists("upload/" . $_FILES["image"]["name"]))
{
echo $_FILES["image"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["image"]["tmp_name"],
"upload/" . $_FILES["image"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["image"]["name"];
}
}
}
else
{
echo "Invalid file";
}


// insert data
$sql="INSERT INTO resim_upload (picture_name, gender, age)
VALUES ('$imagename','$gender','$age')";

if (!mysql_query($sql,$bag))
{
die('Error: ' . mysql_error());
}
echo "1 record added";

}
?>



structure of database


rid mediumint(9)
picture_name mediumtext
gender bit(1)
age tinyint(4)

elanm
02-05-2010, 03:24 AM
The gender on your database, I think it is in wrong type. Try to change it to varchar(6).

For the insert query, please try this:
$sql="INSERT INTO resim_upload (picture_name, gender, age)
VALUES ('$imagename','$gender',$age)";

Buk
02-05-2010, 11:44 AM
But also age data insert incorrect



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum