Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Buk
    Buk is offline
    New Coder
    Join Date
    Feb 2010
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy PHP Upload and İnserting Data

    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
    PHP Code:

    <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 Code:
    <?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
    Code:
    rid                    mediumint(9)
    picture_name     mediumtext
    gender              bit(1)
    age                  tinyint(4)
    Last edited by Buk; 02-05-2010 at 11:44 PM.

  • #2
    New to the CF scene
    Join Date
    Jul 2008
    Location
    Cebu City, Philippines
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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)";

  • #3
    Buk
    Buk is offline
    New Coder
    Join Date
    Feb 2010
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts
    But also age data insert incorrect
    Last edited by Buk; 02-05-2010 at 11:48 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •