zazu Posted October 5, 2015 Share Posted October 5, 2015 (edited) Hi there guys, I've a little problem with inserting a file name into a database table. I can't see wich is the problem. The code is bellow and i think the problem is at INSERT INTO part. <?php $path = "./cv/"; $valid_formats = array("doc", "pdf"); if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") { $name = $_FILES['photoimg']['name']; $size = $_FILES['photoimg']['size']; if(strlen($name)) { list($txt, $ext) = explode(".", $name); if(in_array($ext,$valid_formats)) { if($size<(20480*20480)) // Image size max 20 MB { $actual_image_name = time().$id.".".$ext; $tmp = $_FILES['photoimg']['tmp_name']; if(move_uploaded_file($tmp, $path.$actual_image_name)) { mysqli_query($mysqli,"INSERT INTO formular_client (client_cv = '$actual_image_name')"); } else echo "failed"; } else echo "Image file size max 20 MB"; } else echo "Invalid file format.."; } } ?> <input type="file" name="photoimg" id="photoimg" /> Edited October 5, 2015 by zazu Quote Link to comment https://forums.phpfreaks.com/topic/298443-insert-image-name-to-database-problem/ Share on other sites More sharing options...
cobusbo Posted October 5, 2015 Share Posted October 5, 2015 Hi there guys, I've a little problem with inserting a file name into a database table. I can't see wich is the problem. The code is bellow and i think the problem is at INSERT INTO part. <?php $path = "./cv/"; $valid_formats = array("doc", "pdf"); if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") { $name = $_FILES['photoimg']['name']; $size = $_FILES['photoimg']['size']; if(strlen($name)) { list($txt, $ext) = explode(".", $name); if(in_array($ext,$valid_formats)) { if($size<(20480*20480)) // Image size max 20 MB { $actual_image_name = time().$id.".".$ext; $tmp = $_FILES['photoimg']['tmp_name']; if(move_uploaded_file($tmp, $path.$actual_image_name)) { mysqli_query($mysqli,"INSERT INTO formular_client (client_cv = '$actual_image_name')"); } else echo "failed"; } else echo "Image file size max 20 MB"; } else echo "Invalid file format.."; } } ?> <input type="file" name="photoimg" id="photoimg" /> Check my topic I had similar situation regarding Mime checking I think You should use mime types as well and not explode to get file ext. I'm not an expert with mysqli but try mysqli_query($mysqli,"INSERT INTO `formular_client` (`client_cv`) = (`$actual_image_name`)"); Quote Link to comment https://forums.phpfreaks.com/topic/298443-insert-image-name-to-database-problem/#findComment-1522390 Share on other sites More sharing options...
Barand Posted October 5, 2015 Share Posted October 5, 2015 The syntax is INSERT INTO `formular_client` (`client_cv`) VALUES ('$actual_image_name') or INSERT INTO `formular_client` SET `client_cv` = '$actual_image_name' Quote Link to comment https://forums.phpfreaks.com/topic/298443-insert-image-name-to-database-problem/#findComment-1522391 Share on other sites More sharing options...
zazu Posted October 5, 2015 Author Share Posted October 5, 2015 The syntax is INSERT INTO `formular_client` (`client_cv`) VALUES ('$actual_image_name') or INSERT INTO `formular_client` SET `client_cv` = '$actual_image_name' Doesn't work, and i don't know why because the code seems to be correct. Quote Link to comment https://forums.phpfreaks.com/topic/298443-insert-image-name-to-database-problem/#findComment-1522392 Share on other sites More sharing options...
scootstah Posted October 5, 2015 Share Posted October 5, 2015 You need to be more specific than "doesn't work". Quote Link to comment https://forums.phpfreaks.com/topic/298443-insert-image-name-to-database-problem/#findComment-1522396 Share on other sites More sharing options...
zazu Posted October 5, 2015 Author Share Posted October 5, 2015 You need to be more specific than "doesn't work". When i execute the script the file i choose is sent to the specified folder, but the file name is not stored to the database! To be more specific the INSERT function doesn't work well. Quote Link to comment https://forums.phpfreaks.com/topic/298443-insert-image-name-to-database-problem/#findComment-1522397 Share on other sites More sharing options...
Solution benanamen Posted October 5, 2015 Solution Share Posted October 5, 2015 (edited) Have you checked to see if there is any data in $actual_image_name? In the following line you have $id, but I dont see it anwhere in your code gettting a value. $actual_image_name = time().$id.".".$ext; Edited October 5, 2015 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/298443-insert-image-name-to-database-problem/#findComment-1522398 Share on other sites More sharing options...
mac_gyver Posted October 5, 2015 Share Posted October 5, 2015 you must ALWAYS check for and handle query errors. there are multiple possible reasons that any query can fail. for some quick debugging, just echo mysqli_error($mysqli); on the next line after your mysqli_query() statement, assuming you have a valid database connection in $mysqli. do you have php's error_reporting set to E_ALL and display_errors set to ON so that php would help you by reporting and displaying all the errors it detects? Quote Link to comment https://forums.phpfreaks.com/topic/298443-insert-image-name-to-database-problem/#findComment-1522399 Share on other sites More sharing options...
zazu Posted October 5, 2015 Author Share Posted October 5, 2015 Have you checked to see if there is any data in $actual_image_name? In the following line you have $id, but I dont see it anwhere in your code gettting a value. $actual_image_name = time().$id.".".$ext; Ok mate! Thanks for help i've manage to solve the problem. The working code is listed bellow: mysqli_query($mysqli,"UPDATE evaluatori_users SET client_cv='$actual_image_name' WHERE id='$id'"); Quote Link to comment https://forums.phpfreaks.com/topic/298443-insert-image-name-to-database-problem/#findComment-1522403 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.