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 7 of 7
  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts

    can any point out a way of doing this (insert title name into mysql)

    I got my form to submit an image and display it but i want to add extra info i setup the table with the right fields. But it will not add title information into the table with the mage data.

    So am trying to get it to display a custom title above the image that the user inputs into the form along with the image. I also might want to add other information in the future if someone could show me a way of doing this i would be very grateful i have been trying since Friday.


    form.php

    PHP Code:

    <?
    echo '
    <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <form method="post" enctype="multipart/form-data" action="add_image.php">
    Select Image File: 
    <td>
    <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
    <tr>
    <td colspan="3" bgcolor="#E6E6E6"><strong>Create News</strong> </td>
    </tr>
    <tr>
    <td width="14%"><strong>Title</strong></td>
    <td width="2%">:</td>
    <td width="84%"><input name="title" type="text" id="title" size="50" /></td>
    </tr>
    <tr>
    <td valign="top"><strong>Preview image</strong></td>
    <td valign="top">:</td>
    <td><input type="file" name="userfile" size="40"></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>
    </tr>
    </table>
    </td>
    </form>
    </tr>
    </table>'
    ;


    ?>
    add_image.php

    PHP Code:
    <?php

    // database connection
    $conn mysql_connect("localhost""username""password"
    OR DIE (
    mysql_error());
    @
    mysql_select_db ("image"$conn) OR DIE (mysql_error());

    $title=$_POST['title'];

    $postdate=date("d/m/y h:i:s"); 


    $sql2="INSERT INTO image(title)VALUES('$title')";



    if (
    $_FILES) {
    $image_types = Array ("image/bmp",
    "image/jpeg",
    "image/pjpeg",
    "image/gif",
    "image/x-png");
    if (
    is_uploaded_file ($_FILES['userfile']['tmp_name'])) {
    $userfile addslashes (fread 
    (fopen ($_FILES["userfile"]["tmp_name"], "r"), 
    filesize ($_FILES["userfile"]["tmp_name"])));
    $file_name $_FILES["userfile"]["name"];
    $file_size $_FILES["userfile"]["size"];
    $file_type $_FILES["userfile"]["type"];

    if (
    in_array (strtolower ($file_type), $image_types)) {
    $sql "INSERT INTO image "
    "(image_type, image, image_size, image_name, image_date) ";
    $sql.= "VALUES (";
    $sql.= "'{$file_type}', '{$userfile}', '{$file_size}', "
    "'{$file_name}', NOW())";
    @
    mysql_query ($sql$conn);
    Header("Location:http://www.test.com/ty.php");
    exit();
    }
    }
    }




    ?>
    Last edited by Lyconpire; 01-08-2007 at 08:22 AM.

  • #2
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    In that case you will have to add an extra column called e.g. 'title' in your image table in your database to hold that extra piece of information.

    After this is done, modify your insert query to this
    PHP Code:
    $sql "INSERT INTO image (title, image_type, image, image_size, image_name, image_date) ";
    $sql.= "VALUES ('{$title}', '{$file_type}', '{$userfile}', '{$file_size}', '{$file_name}', NOW())"
    Tips:
    1. Perhaps also include image/png to allow png images.
    2. Also be sure to find out whether magic_quotes are on or off in your php.ini settings. If off you better also apply addslashes to $_POST['title'] before running your query. Or perhaps even better: use mysql_real_escape_string

  • #3
    Registered User
    Join Date
    Jul 2005
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I modified my query to the code you shown but now it does not even add the image into the database? Any ideas to way itĺs stopped adding anything into it now?

  • #4
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Until you get the script working remove the last two statements: header... and exit(); because otherwise you won't see the error messages.

    If this doesn't help then try to echo $sql immediately after it is defined to see what query you are sending.
    PHP Code:
    echo $sql
    Also, are you sure that you actually have all the needed columns in your table?

    Edit: Until you get it firing you should also remove the @-operators in front of mysql_... 2 places in order to see warning messages.
    Last edited by koyama; 01-08-2007 at 11:11 AM. Reason: addition

  • #5
    Registered User
    Join Date
    Jul 2005
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you for your help so this is what i get????

    INSERT INTO image (title, image_type, image, image_size, image_name, image_date) VALUES ('hope', 'image/pjpeg', ' ě Ó\0JFIF\0,,\0\0 Ý LPhotoshop 3.0\08BIMÝ Resolution\0\0\0\0,\0\0\0\0,\0\0\0\08BIM FX Global Lighting Angle\0\0\0\0\0\0\0x8BIMFX Global Altitude\0\0\0\0\0\0\08BIMˇ Print Flags\0\0\0 \0\0\0\0\0\0\0\0\08BIM Copyright Flag\0\0\0\0\0\08BIM\'Japanese Print Flags\0\0\0\0 \0\0\0\0\0\0\0\08BIM§Color Halftone Settings\0\0\0H\0/ff\0\0lff\0\0\0\0\0\0\0/ff\0\0íÖÜ\0\0\0\0\0\0\02\0\0\0\0Z\0\0\0\0\0\0\0\0\05\0\0\0\0-\0\0\0\0\0\0\0\08BIM°Color Transfer Settings\0\0\0p\0\0                      Ŕ\0\0\0\0                      Ŕ\0\0\0


    ok it seems to see what i have put into the form. If you look in values hope was the title and am guessing that text is the image? But still no luck with getting it to add into the database?
    Last edited by Lyconpire; 01-08-2007 at 11:35 AM.

  • #6
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    And you don't get any warning messages? Which would be most likely if it wasn't working.

    In that case warning messages have probably been disabled. Try to put in the very top of your script:
    PHP Code:
    <?php
    ini_set
    ('display_errors'1);
    error_reporting(E_ALL);
    ...
    Then execute again.

  • #7
    Registered User
    Join Date
    Jul 2005
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    Thanks for your help my script is now working.

    I just rewrote it from the ground up and added your code that you shown from the start and it worked. I guess i just had something i did not see or forgot about causing the problem. It works now any way thanks a lot.


  •  

    Posting Permissions

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