tjkuster Posted March 18, 2009 Share Posted March 18, 2009 Any ideas why the following image insert coding is not working, but giving me a success statement? If you can't see how to fix my code, does anyone know an easily-editable way to upload either an image or an image url to an sql database? <html> <head><title>Store binary data into SQL Database</title></head> <body> <?php // code that will be executed if the form has been submitted: if ($submit) { // connect to the database // (you may have to adjust the hostname,username or password) MYSQL_CONNECT("10.8.11.72","CommBizGuide","Abcd1234"); mysql_select_db("free_listing"); $data = addslashes(fread(fopen($form_data, "r"), filesize($form_data))); $result=MYSQL_QUERY("INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) ". "VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')"); $id= mysql_insert_id(); print "<p>This file has the following Database ID: <b>$id</b>"; MYSQL_CLOSE(); } else { // else show the form to submit new data: ?> <form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data"> File Description:<br> <input type="text" name="form_description" size="40"> <input type="hidden" name="MAX_FILE_SIZE" value="1000000"> <br> File to upload/store in database:<br> <input type="file" name="form_data" size="40"> <p><input type="submit" name="submit" value="submit"> </form> <?php } ?> </body> </html> Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/149992-phpsql-insert-with-image-not-working/ Share on other sites More sharing options...
Andy-H Posted March 18, 2009 Share Posted March 18, 2009 Any ideas why the following image insert coding is not working, but giving me a success statement? If you can't see how to fix my code, does anyone know an easily-editable way to upload either an image or an image url to an sql database? <html> <head><title>Store binary data into SQL Database</title></head> <body> <?php // code that will be executed if the form has been submitted: if ( isSet($_POST['submit']) ) { // connect to the database // (you may have to adjust the hostname,username or password) MYSQL_CONNECT("10.8.11.72","CommBizGuide","Abcd1234"); mysql_select_db("free_listing"); $data = addslashes(fread(fopen($form_data, "r"), filesize($form_data))); $result=MYSQL_QUERY("INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) ". "VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')"); $id= mysql_insert_id(); print "<p>This file has the following Database ID: <b>$id</b>"; MYSQL_CLOSE(); } else { // else show the form to submit new data: ?> <form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data"> File Description:<br> <input type="text" name="form_description" size="40"> <input type="hidden" name="MAX_FILE_SIZE" value="1000000"> <br> File to upload/store in database:<br> <input type="file" name="form_data" size="40"> <p><input type="submit" name="submit" value="submit"> </form> <?php } ?> </body> </html> Thanks! I think you need to use the $_FILES['key'] to collect the data for the image rather than fread / fopen etc. as I cant see where the file gets uploaded... Quote Link to comment https://forums.phpfreaks.com/topic/149992-phpsql-insert-with-image-not-working/#findComment-787714 Share on other sites More sharing options...
Maq Posted March 18, 2009 Share Posted March 18, 2009 You need to use $_POST for all your input fields from your form (i.e. $_POST['form_description']). The reason you get the success message is because you print out the success message no matter what happens after submission. Quote Link to comment https://forums.phpfreaks.com/topic/149992-phpsql-insert-with-image-not-working/#findComment-787716 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.