Hi Guys i have been struggling with this script for almost a week now. I am trying to get images uploaded into a MySQL DB with a php script. this is the script that does it for me. add.html ( just the page where an image is selected to be uploaded) <form enctype="multipart/form-data" action="insert.php" method="post" name="changer"> <input name="MAX_FILE_SIZE" value="1024000" type="hidden"> <input name="image" accept="image/jpeg" type="file"> <input value="Submit" type="submit"> HTML: then insert.php ( the main script) <?php // Create MySQL login values and // set them to your login information. $username = "####"; $password = "####"; $host = "#####"; $database = "binary"; // Make the connect to MySQL or die // and display an error. $link = mysql_connect($host, $username, $password); if (!$link) { die('Could not connect: ' . mysql_error()); } // Select your database mysql_select_db ($database); // Make sure the user actually // selected and uploaded a file if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { // Temporary file name stored on the server $tmpName = $_FILES['image']['tmp_name']; // Read the file $fp = fopen($tmpName, 'r'); $data = fread($fp, filesize($tmpName)); $data = addslashes($data); fclose($fp); // Create the query and insert // into our database. $query = "INSERT INTO tbl_images "; $query .= "(image) VALUES ('$data')"; $results = mysql_query($query, $link); // Print results print "Thank you, your file has been uploaded."; } else { print "No image selected/uploaded"; } // Close our MySQL Link mysql_close($link); ?> Code (markup): now heres the tricky part(maybe just for me) there needs to be a extra box in the add.html so that the person uploading the image can insert(type) in the customer number(lets say its "1") then the script needs to go and where the id(Primary Key) is equal to what the user entered("1") needs to go upload it in that row. Any help would be appreciated. Cheers
ok got it almost working, just need 1 small fix this is my new html file <form enctype="multipart/form-data" action="insert2.php" method="post" name="changer"> <input name="MAX_FILE_SIZE" value="1024000" type="hidden"> <input name="image" accept="image/jpeg" type="file"> Customer ID: <input type="text" name="customerno" id="customerno" /> <input value="Submit" type="submit"> HTML: and my php that i edited <?php // Create MySQL login values and // set them to your login information. $username = "root"; $password = "#####"; $host = "localhost"; $database = "#####"; // Make the connect to MySQL or die // and display an error. $link = mysql_connect($host, $username, $password); if (!$link) { die('Could not connect: ' . mysql_error()); } // Select your database mysql_select_db ($database); // Make sure the user actually // selected and uploaded a file if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { // Temporary file name stored on the server $tmpName = $_FILES['image']['tmp_name']; // Read the file $fp = fopen($tmpName, 'r'); $data = fread($fp, filesize($tmpName)); $data = addslashes($data); fclose($fp); // Create the query and insert // into our database. $query = "UPDATE customer SET image = ($data) WHERE id = $customerno"; $results = mysql_query($query, $link) ; // Print results print "Thank you, your file has been uploaded."; } else { print "No image selected/uploaded"; } // Close our MySQL Link mysql_close($link); ?> PHP: If i replace WHERE id = $customerno"; with "WHERE id = '1' "; it adds the image as a blob to the image column in the first row of the perfect but my question now is how can i link that html input type to my php file, i have tried almost everything including quotes, dollar signs. any help would be appreciated x2
I could not see where you populate value of $customerno. I suggest replacing $query = "UPDATE customer SET image = ($data) WHERE id = $customerno"; Code (markup): with $customerno = $_POST['customerno']; $query = "UPDATE customer SET image = ($data) WHERE id = $customerno"; Code (markup): I hope I've understood your query correct, if not, please elaborate a bit more.
Hi Thanks for reply and trying to help out. when i replace that code, it doesnt seem to work. here are 2 screenshots to illustrate what im trying to achieve. addhtml.png what the html looks like. I need the script to take the value entered by the user through the browser and add it into the row of the id entered by the user. mysql.png shows where the blob needs to be inserted, for the 1st and 3rd row I added this line "WHERE id = '1' "; and "WHERE id = '3' "; just to check if it worked, now i just need the right statement, expression, query, whatever to use the value entered by the user and add it to that row which id exists. So if you enter 2 in the html file, it needs to put it in row 2 Cheers
Oh! So you wish to update customer_no column in database with Customer ID provided by user. Try following: $customerno = $_POST['customerno']; $query = "UPDATE customer SET image = ($data), customer_no = '$customerno' WHERE id = $customerno"; Code (markup): Though I do not understand purpose of it, but above shall hopefully solve your problem.
ok i also tried that part but doesn't seem to be doing what im looking for when i browse for an image and put "2" in the Customer id tab, it does not upload the image as a blob to the second line If i put in "3" (without quotes) it should upload the image as a blob in the third row in the image Column, if 4 then the fourth row, etc Thanx for help sofar
What is the key for updation in table? id or customer_no? Keep the key in where condition and it should solve the problem.
The Primary key is ID, i have replaced customer_no with id, but with no result. in my html file theres this line, Customer ID: <input type="text" name="customerno" id="customerno" /> my bad on naming it almost the same as customer_no, but it defines the ID number in which row it should be entered. maybe that might help
ok got it working, thanx your queries worked just had to put the ($data) in quotes like ('$data') Thanx for your help mastermunj Cheers