Picture not being stored inside mysql database properly - php

A value is being stored in the database but it is not the one i am expecting. I tried many methods before but this one kind of seems to work but the file name is not being stored and when I try to download the file directly from the database, it downloads a .bin file format which looks something like table_Name-column_Name.bin. The file name being stored is BLOB - ## B.
My Form
<form class="form-horizontal" method="post" action="productsValidate.php" name="myForm" enctype="multipart/form-data">
<fieldset>
<legend>Add Product</legend>
<div class="form-group">
<label for="Product_Name" class="col-lg-2 control-label">Product Name</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="Product_Name" placeholder="Name" required="required" name="Product_Name">
</div>
</div>
<div class="form-group">
<label for="Size" class="col-lg-2 control-label">Size</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="Size" placeholder="Size" required="required" name="Size">
</div>
</div>
<div class="form-group">
<label for="Color" class="col-lg-2 control-label">Color</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="Color" placeholder="Size" required="required" name="Color">
</div>
</div>
<div class="form-group">
<label for="price" class="col-lg-2 control-label">Price</label>
<div class="col-lg-10">
<input type="number" class="form-control" id="price" placeholder="price" required="required" name="price">
</div>
</div>
<div class="form-group">
<label for="image" class="col-lg-2 control-label">Select Image</label>
<div class="col-lg-10">
<input type="file" name="image" id="image">
</div>
</div>
<div class="form-group">
<label for="categoryId" class="col-lg-2 control-label">Category Id</label>
<div class="col-lg-10">
<?php
//your connection to the db and query would go here
include "../include/settings.php";
$conn = new mysqli($host, $user, $pwd, $sql_db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT distinct Category_Id FROM products";
$result = mysqli_query($conn, $sql);
?>
<select id="categoryId" name="categoryId">
<option value = ""></option>
<?php
while($row = mysqli_fetch_array($result)) {
echo '<option value='.$row['Category_Id'].'>'.$row['Category_Id'].'</option>';
}
?>
</select>
</div>
</div>
<div class="form-group">
<label for="description" class="col-lg-2 control-label">Description</label>
<div class="col-lg-10">
<textarea type="text" class="form-control" id="description" placeholder="Description" required="required" name="description" pattern="[\sA-Za-z]+"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-lg-6 col-lg-offset-2">
<button type="submit" class="btn btn-primary">Add Product</button>
</div>
</div>
</fieldset>
</form>
My Form validation
<?php
$name = $_POST["Product_Name"];
$size = $_POST["Size"];
$color = $_POST["Color"];
$price = $_POST["price"];
$image = addslashes($_FILES['image']['tmp_name']);
$image = file_get_contents($image);
$image = base64_encode($image);
$image=basename( $_FILES["image"]["tmp_name"],".jpg");
$category = $_POST['categoryId'];
$description = $_POST['description'];
insertProduct($name, $size, $color, $price, $image, $category, $description);
function insertProduct($name, $size, $color, $price, $image, $category, $description){
require_once ("../include/settings.php"); // Load MySQL log in credentials
$conn = #mysqli_connect ($host,$user,$pwd,$sql_db); // Log in and use database
if ($conn) { // check is database is avialable for use
$query = "INSERT INTO products
(Product_Id, Product_Name, Size, Color, Price, Picture, Category_Id, Description)
VALUES ('', '$name', '$size', '$color', '$price', '$image', '$category', '$description')";
$result = mysqli_query ($conn, $query);
if ($result) { // check if query was successfully executed
echo 'Successfully Added';
} else {
echo 'Product could not be added';
}
mysqli_close ($conn); // Close the database connect
} else {
echo "<p>Unable to connect to our database for adding the product.</p>";
}
}
?>

I guess you're trying to store the actual encoded image in the database, not a pointer to it. It looks to me like your eleven-byte BLOB has the pointer in it instead.
Your code contains this sequence of lines.
$image = addslashes($_FILES['image']['tmp_name']);
$image = file_get_contents($image);
$image = base64_encode($image);
$image=basename( $_FILES["image"]["tmp_name"],".jpg");
The third line puts an encoded, not binary, version of the image into a text string. That's close to what you want, but you probably should not base64-encode it if you're putting in a BLOB.
The fourth line discards the image itself and overwrites it with an image name. I think that's wrong.
If you're going to use BLOB data this way, you also need to use mysqli's facilities to prepare your SQL statements, and then bind your parameters. The bind_param() function gives you a way to declare a parameter to be a blob. That's better than trying to trick php's string processing into accepting it.
All that being said, most people use a file system or content server rather than BLOBs to store and serve images to web clients. BLOB programming is a pain in the neck. Also, using a DBMS to store and retrieve images quickly becomes a performance bottleneck in an application that scales up.

Related

large php form upload not working [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Upload doesn't work right when the file is too big
(4 answers)
Display error message PHP Mysql
(6 answers)
Closed 4 years ago.
I'm trying to create a custom blog system for a website I am working on, I have created all of the systems however when I try to insert the form to the database, the script executes however the data is not saved to the database. Some of the fields do contain large amounts of text and formatting using TinyMCE could this be the problem? When I test it with smaller amounts of text it works fine.
PHP script:
$conn = connect();
$action = $_GET['a'];
$id = $_GET['id'];
switch($action) {
case 'delete':
$sql = "DELETE FROM articles WHERE id='$id'";
if(mysql_query($sql)) {
echo "<script type='text/javascript'> alert('Article Deleted'); </script>";
header("Location: manage.php");
}
break;
case 'add':
if(isset($_POST['submit'])) {
$slug = $_POST['slug'];
$datecreated = $_POST['datecreated'];
$datepublish = $_POST['datepublish'];
$author = $_POST['author'];
$status = $_POST['status'];
$title = $_POST['title'];
$miniexcerpt = $_POST['miniexcerpt'];
$teaser = $_POST['teaser'];
$body = $_POST['body'];
$sql = "INSERT INTO articles (slug,datecreated,datepublish,author,status,title,miniexcerpt,teaser,body) values ('$slug','$datecreated','$datepublish','$author','$status','$title','$miniexcerpt','$teaser','$body')";
if(mysql_query($sql)) {
echo "<script type='text/javascript'> alert('Article Added'); </script>";
header("Location: manage.php");
}
}
break;
case 'edit':
if(isset($_POST['submit'])) {
$slug = $_POST['slug'];
$datecreated = $_POST['datecreated'];
$datepublish = $_POST['datepublish'];
$author = $_POST['author'];
$status = $_POST['status'];
$title = $_POST['title'];
$miniexcerpt = $_POST['miniexcerpt'];
$teaser = $_POST['teaser'];
$body = $_POST['body'];
$sql = "UPDATE articles SET slug='$slug',datecreated='$datecreated',datepublish='$datepublish',author='$author',status='$status',title='$title',miniexcerpt='$miniexcerpt',teaser='$teaser',body='$body' WHERE id='$id'";
if(mysql_query($sql)) {
echo "<script type='text/javascript'> alert('Article Updated'); </script>";
header("Location: manage.php");
}
}
break;
}
$conn = connect();
$action = $_GET['a'];
$id = $_GET['id'];
switch($action) {
case 'publish':
$sql = "UPDATE articles SET status='published' WHERE id='$id'";
if(mysql_query($sql)) {
echo "<script type='text/javascript'> alert('Article Published'); </script>";
header("Location: manage.php");
}
break;
}
HTML Form:
<form id="form" name="form" action="articlefunctions.php?a=add" method="post">
<div class="form-group row">
<label for="title" class="col-sm-2 col-form-label">Article Title</label>
<div class="col-sm-10">
<input name="title" type="text" id="title" class="form-control" placeholder="Article Title" />
</div>
</div>
<div class="form-group row">
<label for="slug" class="col-sm-2 col-form-label">Article URL (Slug)</label>
<div class="col-sm-10">
<input name="slug" type="text" id="slug" class="form-control" placeholder="Article URL - Must NOT include spaces - use '-' instead" />
</div>
</div>
<div class="form-group row">
<label for="teaser" class="col-sm-2 col-form-label">Article Excerpt</label>
<div class="col-sm-10">
<textarea name="teaser" id="teaser" placeholder="Article Excerpt" rows="4"></textarea>
</div>
</div>
<div class="form-group row">
<label for="miniexcerpt" class="col-sm-2 col-form-label">Homepage Excerpt</label>
<div class="col-sm-10">
<textarea name="miniexcerpt" id="miniexcerpt" maxlength="180" placeholder="Snippet from excerpt to go on homepage" rows="4"></textarea>
<span id='remainingC' class="pull-right"></span>
</div>
</div>
<div class="form-group row">
<label for="body" class="col-sm-2 col-form-label">Article Content</label>
<div class="col-sm-10">
<textarea name="body" id="body" rows="8"></textarea>
</div>
</div>
<div class="form-group row">
<label for="author" class="col-sm-2 col-form-label">Article Author</label>
<div class="col-sm-10">
<input name="author" type="text" id="author" readonly class="form-control" value="<?php echo $_SESSION['user_name'];?>" />
</div>
</div>
<div class="form-group row">
<label for="status" class="col-sm-2 col-form-label">Article Status</label>
<div class="col-sm-10">
<select name="status" id="status" class="form-control" />
<option value="draft" selected>draft</option>
<option value="published">published</option>
</select>
</div>
</div>
<input name="datecreated" type="hidden" id="datecreated" value="<?php echo date('Y-m-d'); ?>" class="form-control" />
<div class="form-group row">
<label for="datepublish" class="col-sm-2 col-form-label">Publish Date</label>
<div class="col-sm-10">
<input name="datepublish" type="date" id="datepublish" class="form-control" />
</div>
</div>
<div class="form-group row">
<input type="submit" value="Save" id="submit" name="submit" class="btn btn-primary"/>
</div>
</form>
Check your php.ini. Some parameters affect POST payload size.
post_max_size integer
Sets max size of post data allowed. This setting also affects file upload. To upload large files, this value must be larger than upload_max_filesize. Generally speaking, memory_limit should be larger than post_max_size. When an integer is used, the value is measured in bytes. Shorthand notation, as described in this FAQ, may also be used. If the size of post data is greater than post_max_size, the $_POST and $_FILES superglobals are empty. This can be tracked in various ways, e.g. by passing the $_GET variable to the script processing the data, i.e. , and then checking if $_GET['processed'] is set.
source

Mysql insert error from html form

I have built a database called jobs, and I am trying to insert data into it via an html form, that calls a php file. After submitting the form I see the following error in the console.
ERROR: Could not able to execute INSERT INTO jobs (id, title, pay, description, location, max_people, people_going, tasks, start_time, end_time, start, end)
VALUES (Default, 'testTitle', '4.00', 'testd', 'testl', '4', '1', 'testt', '13:00:00', '14:00:00', '2016-05-31 13:00:00', '2016-05-31 14:00:00').
I can manually input data through phpMyadmin, this only happens if I try to update the database via the form. I am relatively new to working with databases, so I am sure it is something very simple. I would appreciate any help that can be given.
The database is laid out as follows and nothing can be Null:
id type:int,auto increment
title type:varchar
pay type:decimal(15,2)
description type:text
location type:text
max_people type:int
people_going type:int
tasks:text
start_time type:time
end_time type:time
start type:datetime
end type:datetime
insert.php
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$host= "localhost";
$user= "";
$pass= "";
$link = mysql_connect($host, $user, $pass);
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$job_title = $_POST['j_title'];
$job_pay = $_POST['j_pay'];
$job_start_time = $_POST['j_start'];
$job_end_time = $_POST['j_end'];
$original_job_date = $_POST['j_date'];
$job_summary = $_POST['j_description'];
$job_location = $_POST['j_location'];
$job_people = $_POST['j_people'];
$job_tasks = $_POST['j_tasks'];
$j_going=1;
$job_date=date('Y-m-d',strtotime("$original_job_date"));
$event_start= date('Y-m-d H:i:s', strtotime("$job_date $job_start_time"));
$event_end= date('Y-m-d H:i:s', strtotime("$job_date $job_end_time"));
// attempt insert query execution
$sql = "INSERT INTO jobs (id, title, pay, description, location, max_people, people_going, tasks, start_time, end_time, start, end) VALUES (DEFAULT, '$job_title', '$job_pay', '$job_summary', '$job_location', '$job_people', '$j_going', '$job_tasks', '$job_start_time', '$job_end_time', '$event_start', '$event_end')";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// close connection
mysqli_close($link);
?>
Form code from job_creation.html
<div class="container container-wide z-index">
<h2>Job Creation</h2>
<form class='rd-mailform row' id="job_form" method="post" action="insert.php">
<!-- RD Mailform Type -->
<input type="hidden" name="form-type" value="contact"/>
<!-- END RD Mailform Type -->
<div class="col-xs-12 col-sm-6">
<div class="form-group">
<label class="form-label" data-add-placeholder for="j_title">Job Title</label>
<input id="j_title"
type="text"
name="j_title"
/>
</div>
<div class="form-group">
<label class="form-label" data-add-placeholder for="j_pay">Job Pay</label>
<input id="j_pay"
type="number"
min="0"
step="0.01"
data-number-to-fixed="2"
data-number-stepfactor="100"
name="j_pay"
/>
</div>
<div class="form-group">
<label class="form-label" data-add-placeholder for="j_start">Job Start Time</label>
<input id="j_start"
class="time"
type="text"
name="j_start"
/>
</div>
<div class="form-group">
<label class="form-label" data-add-placeholder for="j_end">Job End Time</label>
<input id="j_end"
class="time"
type="text"
name="j_end"
/>
</div>
<div class="form-group">
<label class="form-label" data-add-placeholder for="j_date">Job Date</label>
<input id="j_date"
class="datepicker"
type="text"
name="j_date"
/>
</div>
<div class="form-group">
<label class="form-label" data-add-placeholder for="j_location">Job Location</label>
<input id="j_location"
type="text"
name="j_location"
/>
</div>
<div class="form-group">
<label class="form-label" data-add-placeholder for="j_people">Number of People</label>
<input id="j_people"
type="number"
name="j_people"
/>
</div>
</div>
<div class="col-xs-12 col-sm-6">
<div class="form-group textarea">
<label class="form-label" data-add-placeholder for="j_description">Job Description</label>
<textarea id="j_description"
name="j_description"
></textarea>
</div>
<div class="form-group textarea">
<label class="form-label" data-add-placeholder for="j_tasks">What Needs to be Done</label>
<textarea id="j_tasks"
name="j_tasks"
></textarea>
</div>
</div>
<div class="form-group btn-wr text-center">
<input type="submit" class="btn btn-sm btn-success" value="Create Job" >
<div class="mfInfo"></div>
</div>
</form>
</div>
Solved
Solution: There were actually multiple problems with the code. After removing the quotes around the integers and decimals, as well as switching all of my statements to use mysqli; I was given the error that it could not connect to a database. This was fixed by adding mysqli_connect to the code, as well as a few variables.
Try this one
$sql = "INSERT INTO jobs (`title`, `pay`, `description`, `location`, `max_people`, `people_going`, `tasks, `start_time, `end_time`, `start`, `end`) VALUES ('$job_title', '$job_pay', '$job_summary', '$job_location', '$job_people', '$j_going', '$job_tasks', '$job_start_time', '$job_end_time', '$event_start', '$event_end')";
start and end is reserved words from MySQL

An empty row getting inserted in database

Hey I am trying to get this code running for the past few days now. I do not know what is the problem. Whenever I run the code I can see it running but an empty row gets inserted. Basically I ave tried to hard code the data and the data gets inserted. Here is the HTML form:
<form action="register.php" id="contactForm" type="post">
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>First name *</label>
<input type="text" class="form-control" name="fname" >
</div>
<div class="col-md-6">
<label>Last name *</label>
<input type="text" class="form-control" name="lname" >
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>Gender *</label><br>
<select name="gender">
<option> Male </option>
<option> Female </option>
</select>
</div>
<div class="col-md-6">
<label>Stream *</label><br>
<select name="stream">
<option> B-Tech </option>
<option> M-Tech </option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>Email *</label>
<input type="text" class="form-control" name="email" >
</div>
<div class="col-md-6">
<label>Mobile *</label>
<input type="text" class="form-control" name="mobile">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>College *</label>
<input type="text" class="form-control" name="college" >
</div>
<div class="col-md-6">
<label>Job Kind *</label>
<input type="text" class="form-control" name="job" >
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
&nbsp&nbsp&nbsp&nbsp
<input type="submit" value="Register" class="btn btn-primary btn-lg"
data-loading-text="Loading..." name="submit">
</div>
</div>
</form>
Here is the registration.php
<?php
$connection = mysql_connect("EDITED by billy, was an I.P and port number", "user", "password"); // Establishing Connection with Server
$db = mysql_select_db("Registrations_connect", $connection); // Selecting Database from Server
$first_name = $_POST["fname"];
$last_name = $_POST["lname"];
$sex = $_POST["gender"];
$field = $_POST["stream"];
$contact = $_POST["mobile"];
$eaddress = $_POST["email"];
$institute = $_POST["college"];
$naukri = $_POST["job"];
$query = mysql_query("insert into students(fname, lname, gender, stream, mobile, email, college, job)
values ('$name', '$last_name', '$sex', '$field','$contact', '$eaddress', '$intitute', '$naukri')");
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
mysql_close($connection); // Closing Connection with Server
?>
After running; In the inspect element I checked the response:- It shows Data Inserted successfully but actually an empty row is getting inserted. Basically what i think I am not able to correctly grab the data properly from form. Can somebody please check what is the problem. It will be a great help.
The attribute is method, not type. This typo is causing your form to process a GET rather than a POST. So all your variable assignments are wrong.
$first_name = $_POST["fname"];
would be
$first_name = $_GET["fname"];
or you could use the $_REQUEST; or you can just correct the attribute,
<form action="register.php" id="contactForm" method="post">
Your code also is wide open to SQL injections and is using the deprecated mysql_ functions. You should update to mysqli or pdo and be using prepared statements with parameterized queries.
More on SQL injections:
http://php.net/manual/en/security.database.sql-injection.phpHow can I prevent SQL injection in PHP?https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet#Defense_Option_1:_Prepared_Statements_.28Parameterized_Queries.29

How to upload image to database in php?

here i am trying to upload image to directory and add the path in database.
here i am first adding some product details and trying to upload the image to uploads directory and add the uploaded image path in database.
here is what i have done:
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "wan_products_box");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$product_name = mysqli_real_escape_string($link, $_POST['product_name']);
$product_category = mysqli_real_escape_string($link, $_POST['product_category']);
$product_price = mysqli_real_escape_string($link, $_POST['product_price']);
$pro_url = mysqli_real_escape_string($link, $_POST['pro_url']);
$co_owners = mysqli_real_escape_string($link, $_POST['co_owners']);
// attempt insert query execution
$sql = "INSERT INTO product_list (product_name, product_category, product_price,product_referrence_URL,product_co_owners) VALUES ('$product_name', '$product_category', '$product_price', '$pro_url', '$co_owners')";
if(mysqli_query($link, $sql)){
echo "New product created.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
//image upload code
if($_POST)
{
if($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
if(file_exists("uploaded_images/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
if(move_uploaded_file($_FILES["file"]["tmp_name"],"uploaded_images/" . $_FILES["file"]["name"]))
{
$query_image = "INSERT INTO product_list (product_image_url) values ('".$_FILES['file']['name']."')";
if(mysql_query($query_image))
{
echo "Stored in: " . "uploaded_images/" . $_FILES["file"]["name"];
}
else
{
echo 'Unable to store';
}
}
}
}
}
mysqli_close($link);
?>
this is my html form:
<div id="menu2" class="tab-pane fade">
<h4>Add new product</h4>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<div class="panel panel-default">
<div class="panel-body">
<form class="form-horizontal" method="post" action="files/insert.php" role="form">
<div class="form-group">
<label class="control-label col-sm-2" for="product_name">Product Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="product_name" id="product_name" placeholder="Iphone 5c" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Product Category</label>
<div class="col-sm-10">
<select class="btn-btn-primary form-control" name="product_category">
<option>Mobile</option>
<option>Television</option>
<option>Printer</option>
<option>Watch</option>
<option>Monitor</option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="product-pic">Upload your profile picture</label>
<div class="col-sm-10">
<input type="file" class="form-control" name="file" id="file" />
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="product_price">Product Price</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="product_price" id="product_price" placeholder="Rs.36,000" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pro_url">Reference URL</label>
<div class="col-sm-10">
<input type="URL" class="form-control" name="pro_url" id="pro_url" placeholder="http://www.amazon.com" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="co_owners">Co-owners</label>
<div class="col-sm-10">
<select class="btn-btn-primary form-control" name="co_owners">
<option>Select no. owners</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Add product</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
the data is inserted to database but the image upload part is not happening...
how can i do this? how can i modify the code to upload the image to database and add path to database?
when i run this the output i get is
New product created.
Notice: Undefined index: file in D:\xampp\htdocs\wan\files\insert.php on line 31
Notice: Undefined index: file in D:\xampp\htdocs\wan\files\insert.php on line 37
Notice: Undefined index: file in D:\xampp\htdocs\wan\files\insert.php on line 43
Notice: Undefined index: file in D:\xampp\htdocs\wan\files\insert.php on line
There are a few things wrong in your code.
Firstly, your form is missing a valid enctype enctype="multipart/form-data" it is required when dealing with files.
http://php.net/manual/en/features.file-upload.post-method.php
You're also mixing MySQL APIs in your second query.
$query_image = "INSERT INTO product_list (product_image_url) values ('".$_FILES['file']['name']."')";
if(mysql_query($query_image))
So it will fail.
Use the same method you used in your first query.
Those different APIs do not intermix.
So change that to if(mysqli_query($link, $query_image))
Also add or die(mysqli_error($link)) to mysqli_query() to check for errors.
Also make sure the folder has proper permissions to be written to.
Another thing, your <select>'s options have no values
<option>Select no. owners</option>
<option>1</option>
...
You need to add those
<option value="empty_value">Select no. owners</option>
<option value="1">1</option>
...
and do the same for the others.
Same thing for <option>Mobile</option>. There should be values for those too.
<option value="mobile">Mobile</option>
...
You will not get anything entered in your database for those.
Apart from the problems in HTML part like the lack of enctype="multipart/form-data". There is a problem in your second Query and this is the cause of these errors:
$query_image = "INSERT INTO product_list (product_image_url) values ('".$_FILES['file']['name']."')";
You need to change this to an UPDATE query and update the already created row in database.
Also it is a better approach to use the row's index number for the image name.

Populate database with data from a form

I want to fill all the fields in my database by getting the ones inputted on my form but it won't fill my database.
I don't know what's wrong with it:
<?php
$handle = mysql_connect("", "root" , "");
return mysql_select_db("bonggarden", $handle);
$SQL = "INSERT INTO table1(fname,femail,fphone,fmsg)
values('".$_POST['name']."','".$_POST['email']."','".$_POST['number']."','".$_PO ST['message']."')";
mysql_query($SQL);
?>
here is my form
<form id="main-contact-form" accept-charset="utf-8" class="" method="post" >
<div class="col-sm-5 col-sm-offset-1">
<div class="form-group">
<label>Name *</label>
<input type="text" name="name" id="name" class="form-control" required="required">
</div>
<div class="form-group">
<label>Email *</label>
<input type="email" name="email" id="email" class="form-control" required="required">
</div>
<div class="form-group">
<label>Phone *</label>
<input type="text" name="number" id="number" class="form-control">
</div>
</div>
<div class="col-sm-5">
<div class="form-group">
<label>Message *</label>
<textarea name="message" id="message" required="required" class="form-control" rows="8"></textarea>
</div>
<div class="form-group">
<button type="submit" name="submit" class="btn btn-primary btn-lg" required="required">Submit Message</button>
</div>
</div>
</form>
<?php
$fname = $_POST['name'];
$femail = $_POST['email'];
$fphone = $_POST['number'];
$fmsg = $_POST['message'];
$handle = mysql_connect("localhost", "root" , "");
mysql_select_db("bonggarden", $handle);
$SQL = "INSERT INTO table1(fname,femail,fphone,fmsg) values('$fname','$femail','$fphone','$fmsg')";
mysql_query($SQL);
?>
// if it still not working echo the variable $fname, $femail, $fphone, $fmsg check value is proparly getting in veriable or not.
Try changing
return mysql_select_db("bonggarden", $handle);
to
mysql_select_db("bonggarden", $handle);
The return is exiting the code before you execute the insert...
You do not need the 'return' in:
return mysql_select_db("bonggarden", $handle);
You may also consider adding the location of your server to your 'mysql_connect' function. EX:
mysql_connect("localhost","root","");
A couple of suggestions too:
1.) Organizationally, assigning your values from $_POST to variables might make it easier to see what values you are sending to your database.
2.) Add a die function that outputs a MySQL error if your code cannot be run. This can often help with debugging and finding out the general location of your error. EX:
mysql_query($SQL) or die(mysql_error());
3.) Finally, and this gets said a lot on this website, consider switching to MySQLi as traditional 'mysql_' commands are now deprecated for security reasons.

Categories