Jump to content


Photo

Add record and image with php


  • Please log in to reply
2 replies to this topic

#1 Dicko_md

Dicko_md

    Advanced Member

  • Members
  • PipPipPip
  • 45 posts

Posted 03 February 2013 - 04:13 PM

Hi

I dont think that it will be difficult for some of you on here to spot my flaw but I am trying to add a record and a image to a folder and register the name of the file along with the full path to the image in the mysql record.

The issue I am having is that the image is uploading OK but nothing from the form is uploading into the database.

My columns are named as

ID, Display Name, Model Type, Fix Type, Description, Price, image

Also...where would I put the http://www.XXXX.co.uk/images/catalog in my addrecord.php script so that it inserts it along with the file name of the file in the image column, so I can call this image later ?

This is my form
<form method="post" action="addrecord.php" enctype="multipart/form-data">
   <p>
              ID
            </p>
            <input type="text" name="nameid"/>
            <p>
    <p>
              Display Part Name
            
            </p>
            <input type="text" name="namepart"/>
            <p>
              iPhone Model
            </p>
            <input type="text" name="nameiphone"/>
            <p>
              Fix Type
            </p>
            <input type="text" name="namefix"/>
            <p>
              Description
            </p>
            <input type="text" name="namedescription"/>
            <p>
              Price
            </p>
            <input type="text" name="nameprice"/>
            
            <p>
              Image:
            </p>


            <input type="file" name="photo"> 
            
            <br/>
            <br/>
            <input TYPE="submit" name="upload" title="Insert Record" value="Insert Record"/>
          </form>

This is the add record.php file
My code is
<?php


//This is the directory where images will be saved
$target = "/home/XXXX/public_html/images/catalog/";
$target = $target . basename( $_FILES['photo']['name']);


//This gets all the other information from the form
$ID=$_POST['nameid'];
$displayname=$_POST['namepart'];
$iphone=$_POST['nameiphone'];
$fix=$_POST['namefix'];
$description=$_POST['namedescription'];
$price=$_POST['nameprice'];
$pic=($_FILES['photo']['name']);


// Connects to your Database
mysql_connect("localhost", "XXXXXX", "iphone") or die(mysql_error()) ;
mysql_select_db("XXXXX_catalog") or die(mysql_error()) ;


//Writes the information to the database
mysql_query("INSERT INTO catrep (nameid,namepart,nameiphone,namefix,namedescription,nameprice,photo)
VALUES ('$ID','$displayname', '$iphone', '$fix', '$description','$price','$pic')") ;


//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{


//Tells you if its all ok
echo "The file ". basename( $_FILES['photo']['displayname']). " has been uploaded, and your information has been added to the directory";
}
else {


//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>

Thanks in advance

Martyn

#2 Love2c0de

Love2c0de

    Advanced Member

  • Members
  • PipPipPip
  • 366 posts
  • LocationThe Pleiades
  • Age:23

Posted 03 February 2013 - 04:43 PM

Not sure what the problem could be to be honest but give this a go:
//Writes the information to the database
$result = mysql_query("INSERT INTO catrep (nameid,namepart,nameiphone,namefix,namedescription,nameprice,photo)
VALUES ('{$ID}','{$displayname}', '{$iphone}', '{$fix}', '{$description}','{$price}','{$pic}')") ;

//Writes the photo to the server
if($result != FALSE && move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{


//Tells you if its all ok
echo "The file ". basename( $_FILES['photo']['displayname']). " has been uploaded, and your information has been added to the directory";
}
else {


//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}

Just overwrite everything starting from your query to the end of the script with this.

Also, try putting a var_dump($_POST); statement at the top of your script and see what that reveals. Although I don't see any problem with the form input names and your $_POST data retrieving.

With your second question, create anothe field in your table of varchar type. Then just save $_FILES['photo']['name'] to a variable (like you have done with your $_POST data) and then amend the query.

Hope this helps,

Regards,

L2c.

Edited by Love2c0de, 03 February 2013 - 04:55 PM.


#3 Love2c0de

Love2c0de

    Advanced Member

  • Members
  • PipPipPip
  • 366 posts
  • LocationThe Pleiades
  • Age:23

Posted 03 February 2013 - 05:08 PM

With your second question, create anothe field in your table of varchar type. Then just save $_FILES['photo']['name'] to a variable (like you have done with your $_POST data) and then amend the query.

Then to use the files later, you can st up a variable holding the path, then just loop though your images from the database like so:
//do query to get data from that table.....save it into variable $query

$directory = "/home/XXXX/public_html/images/catalog/";

while($row = mysql_fetch_array($query))
{
echo "<img src='{$directory}{$row['image_field']}' alt='altText' />";
}

Hope this helps,

Regards,

L2c.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com