How can i select data stored in a database with PHP
How can i select data stored in a database with PHP
the problem i am facing is i want to read a value that is auto incemented and used, my database takes the following design:
mysql_query("INSERT INTO category (category,image) VALUES ('$name','$default_item')");
$image_name = '$id' . '.jpg'
move_uploaded_file($_FILES["image"]["tmp_name"], "../images/category/" . $image_name);
I have been able to find the solution of what i needed on my own, Thanks anyway
– Malek Salameh
Jun 30 at 20:06
2 Answers
2
A few things to be mentioned:
You should not use mysql_query anymore. Use mysqli_query instead. mysql_* is deprecated and has been removed in the latest PHP version.
mysql_query
mysqli_query
Secondly: Never use user generated content in an SQL query directly. Use a prepared statement. Otherwise your website is vulnerable to SQL injections.
About grabbing the auto increment value - see this thread.
Instead of using auto increment id as the image name.
I will suggest generating a unique name of the image and insert that name in the database and use that name for uploading the image as well.
Use below code to create the image name
$uniquesavename = time().uniqid(rand());
mysql_query("INSERT INTO category (category,image) VALUES ('$name','$uniquesavename')");
$image_name = $uniquesavename . '.jpg'
move_uploaded_file($_FILES["image"]["tmp_name"], "../images/category/" . $image_name);
By this way user uploaded image will always have a unique name.
Try this and let me know if you face any problem
Your code is vulnerable to SQL injections and doesn't work on latest PHP version.
– Matthias Bö
Jun 30 at 8:51
$image_name = '$uniquesavename' . '.jpg' have you tried printing $image_name? What does it look like?– kerbholz
Jun 30 at 8:54
$image_name = '$uniquesavename' . '.jpg'
$image_name
Yeah I know that I was focusing Marjory on problem that guy is facing. I think so that guy is also trying on older version of PHP And sorry my bad for not focusing on SQL injection concern
– Sourabh
Jun 30 at 8:56
@kerbholz sorry I have updated my answer
– Sourabh
Jun 30 at 9:01
I think this answer should be accepted as correct, Sourabh proposed him an standard approach to store the images with unique name everytime, (he can use current timestamp for unique combination of name) . According to me it looks good , Upvoted the answer. Ideally you should not consider auto increment field value to name images.
– Dinesh Nagar
Jun 30 at 9:14
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Why dont you just auto increment image as well? You dont need it to be a string
– Arex
Jun 30 at 8:39