Reputation: 45
I want to insert image into my database table. I have search google but I can not insert image into my database. My database field name is 'photo' and data type is 'blob'. please help...
here is my code:
<?php
$id="";
$opr="";
if(isset($_GET['opr']))
$opr=$_GET['opr'];
if(isset($_GET['rs_id']))
$id=$_GET['rs_id'];
//--------------add data-----------------
if(isset($_POST['btn_sub'])){
$fac_name=$_POST['factxt'];
$batch=$_POST['batchtxt'];
$reg=$_POST['regtxt'];
$s_name=$_POST['snametxt'];
$f_name=$_POST['fnametxt'];
$gender=$_POST['gender']
$phone=$_POST['phonetxt'];
$mail=$_POST['emailtxt'];
$photo=$_POST['photos'];
$sql_ins=mysql_query("INSERT INTO stu_tbl
VALUES(
NULL,
'$fac_name',
'$batch',
'$reg',
'$s_name',
'$f_name' ,
'$gender',
'$phone',
'$mail',
'$photo'
)
");
if($sql_ins==true)
$msg="1 Row Inserted";
else
$msg="Insert Error:".mysql_error();
}
?>
<!DOCTYPE html>
<head>
</head>
<body>
<h2 class="custom-light-heading" align="center">Add New Student</h2>
<hr>
<form method="post">
<table>
<tr>
<td>Faculties's Name</td>
<td>
<input type="text" />
</td>
</tr>
<tr>
<td>Batch:</td>
<td>
<input type="text" class="form-control" name="batchtxt" id="textbox" />
</td>
</tr>
<tr>
<td>Registration:</td>
<td>
<input type="text" class="form-control" name="regtxt" id="textbox" />
</td>
</tr>
<tr>
<td>Student Name:</td>
<td>
<input type="text" class="form-control" name="snametxt" id="textbox" />
</td>
</tr>
<tr>
<td>Father's Name:</td>
<td>
<input type="text" class="form-control" name="fnametxt" id="textbox" />
</td>
</tr>
<tr>
<td>Gender:</td>
<td>
<input type="radio" name="gender" value="Male" checked="checked" />Male
<input type="radio" name="gender" value="Female" />Female
</td>
</tr>
<tr>
<td>Phone:</td>
<td>
<input type="text" class="form-control" name="phonetxt" id="textbox" />
</td>
</tr>
<tr>
<td>E-mail:</td>
<td>
<input type="text" class="form-control" name="emailtxt" id="textbox" />
</td>
</tr>
<tr>
<td>photo:</td>
<td>
<input type="File" name="photos" />
</td>
</tr>
<tr>
<td>
<input type="submit" name="btn_sub" class="btn btn-success" value="Add" id="button-in" />
</td>
</tr>
</table>
</div>
</form>
</div>
<?php ?>
</body>
</html>
Upvotes: 0
Views: 132
Reputation: 74232
As I said in comments: I don't see an opening <form>
tag. plus it needs to be a post method with a valid enctype when working with files.
This being in your original posted question:
Add the following to your code, replacing the action to your own.
<form action="handler.php" method="post" enctype="multipart/form-data">
If doing this inside the same file, you can just do action=""
as "self".
Also this $photo=$_POST['photos'];
needs to be $photo=$_FILES['photos'];
using $_FILES
and not $_POST
since it's a file and not a text input or anything other than a file.
Then this <input type="text" />
for Faculties's Name
you don't have a name attribute for it, and seems related to $_POST['factxt']
.
So modify it to <input type="text" name="factxt" />
.
Sidenote: Your present code is open to SQL injection. Use mysqli
with prepared statements, or PDO with prepared statements, they're much safer.
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
Footnotes:
You should use proper bracing techniques. Many of your conditional statements don't have braces if(condition){...}
and this could cause havoc with your code.
If you truly want to check if your query was successful, use mysql_affected_rows()
.
If you see notices about mysql_
being deprecated, then it will be time for you to switch over to mysqli_
or PDO, which you should be using anyway, since it will be removed from future PHP releases.
Upvotes: 4