I'm trying to upload an image into an SQL server using a simple form however I'm being faced with the following warning, which also prevents me from uploading the image:
Warning: odbc_exec(): in C:\wamp\www\inputform\index2.php on line 10
Line 10: $sql_run = odbc_exec($con, $sql);
The code is:
<?php
require ('connect.inc.php');
if(ISSET($_POST['submit'])){
$imagename = $_FILES["image"]["name"];
$imagedata = file_get_contents($_FILES["image"]["tmp_name"]);
echo $imagename." ".$imagedata;
$sql = "INSERT INTO test.dbo.images (imageid, imagedata) VALUES('','$imagedata')";
$sql_run = odbc_exec($con, $sql);
echo "Query with the following details has been executed: <br>".$imagename;
}
else{
echo 'Is not set';
}
?>
<form action="index2.php" method="POST" enctype="multipart/form-data">
Image: <input type="file" name="image"><br>
<input type="submit" value="Submit" name="submit">
</form>
The file connect.inc.php consists of the code below:
<?php
$serverName="TESTSERV\SQLEXPRESS";
$dsn='odbc-test';
$user = 'user';
$password = 'password';
$db = 'test';
if(!$con = odbc_connect($dsn, $user, $password)){
echo "Not Connected";
}
?>
Thanks in advance.
J
The query looks fine so the only logical explanation is that the datatypes you are trying to insert are not same. Make sure that imageid and imagedata datatypes are varchar
EDIT: Make sure that the datatypes are same. here is the updated answer.
<?php
require ('connect.inc.php');
if(ISSET($_POST['submit'])){
$imagename = $_FILES["image"]["name"];
$imagedata = (binary)file_get_contents($_FILES["image"]["tmp_name"]);
echo $imagename." ".$imagedata;
$sql = "INSERT INTO test.dbo.images (imageid, imagedata) VALUES('','.$imagedata.')";
$sql_run = odbc_exec($con, $sql);
echo "Query with the following details has been executed: <br>".$imagename;
}
else{
echo 'Is not set';
}
?>
note: $imagedata = (binary)file_get_contents($_FILES["image"]["tmp_name"]);
Related
I have made up this html form
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<input type="file" name="image1" /><br/>
<input type="file" name="image2" /><br/>
<input type="submit" name='submit' value="upload" />
</form>
this is my php code
<?php
include "conf/connect.php";
if (isset($_POST['submit'])){
$uploadpath1 = 'upload/';
$image1_name = $_FILES['image1']['name'];
$image1_size = $_FILES['image1']['size'];
$image1_type = $_FILES['image1']['type'];
$image1_url =
$image1_temp_name = $_FILES['image1']['tmp_name'];
$uploadpath1 = $uploadpath1. time() . basename($image1_name);
$image1_url = 'http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\/').'/'.$uploadpath1;
////
if(empty($errors)) {
move_uploaded_file($image1_temp_name, $uploadpath1);
$success[] = 'Uploaded!';
}
}
///
if (isset($_POST['submit'])){
$uploadpath2 = 'upload/';
$image2_name = $_FILES['image2']['name'];
$image2_size = $_FILES['image2']['size'];
$image2_type = $_FILES['image2']['type'];
$image2_temp_name = $_FILES['image2']['tmp_name'];
$uploadpath2 = $uploadpath2. time() . basename($image2_name);
$image2_url = 'http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\/').'/'.$uploadpath2;
////
if(empty($errors)) {
move_uploaded_file($image2_temp_name, $uploadpath2);
$success[]= 'Uploaded';
}
}
if(isset($_POST['submit'])){
$id = $_GET['id'];
$table = 'products';
mysqli_query($connect, "UPDATE `$table` SET `image1` = $uploadpath1, `image2` = $uploadpath2 WHERE `id` = $id");
}
?>
All in : image_multi.php
So when i post submit .. the images uploaded successfully but nothing updated in my table
my table
I run this link : mydomainname.com/image_multi.php?id=1
images uploaded but not appears in database at all
Thanks
Check type and length for fields 'image1' and 'image2' in your table
the problem is definitely in the mysql statement, you didn't quote the filenames that's why the update doesn't run through. in the future simply check the db log for errors.
to fix this should work
mysqli_query($connect, "UPDATE `$table` SET `image1` = '$uploadpath1', `image2` = '$uploadpath2' WHERE `id` = $id");
notice the ' enclosing your $vars, that's only for when you need strings in the rows - which you clearly need. I'm not sure the Id should be string, check to see if it's numeric.
i'm using a the following code to insert image into the database
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="image">
<button type="submit" name="upload">Upload</button>
</form>
<?php
if(isset($_POST['upload']) && isset($_FILES['image']))
{
if(getimagesize($_FILES['image']['tmp_name'])==FALSE)
{
echo "Please select an image";
}
else
{
$image = addslashes($_FILES['image']['tmp_name']);
$name = addslashes($_FILES['image']['name']);
$image = file_get_contents($image);
$image=base64_encode($image);
}
}
//img_name(datatype: varchar) and img_content(datatype: longblob)
$SQL = "INSERT INTO image_db (img_name, img_content) VALUES ('$name', '$image')";
$result = mysqli_query($db_handle, $SQL);
?>
Is it possible to assign an image path from a folder in the local storge to a php variable and insert it into the database? I dont want to insert the path but i want to store the image in the variable in the specified path and insert it the same way as shown above.
For example:
<?php
$image = "c:\images\default.png"
$name = "default.png"
$image = addslashes($image);
$name = addslashes($name);
$image = file_get_contents($image);
$image=base64_encode($image);
$SQL = "INSERT INTO image_db (img_name, img_content) VALUES ('$name', '$image')";
$result = mysqli_query($db_handle, $SQL);
?>
Please help me out.
Thanks in advance
i'm trying to add text and images using
Image is successfully added into folder path. but the value of text and images did not add into database
<?php
function insertpost(){
global $connect;
if(isset($_POST['sendpost']))
{
$title = mysqli_real_escape_string($connect,$_POST["title"]);
$target_image = "images/".basename($_FILES['post_image']['name']);
$post_image = $_FILES['post_image']['name'];
$insert_post_and_image = "INSERT INTO table(title, image) VALUES ('$title','$post_image')";
mysqli_query($connect, $insert_post_and_image);
if(move_uploaded_file($_FILES['post_image']['tmp_name'], $target_image))
{
echo "<h3>Posted to timeline!</h3>";
}
}
}
?>
html code
<form action="" method="post" id="form" enctype="multipart/form-data">
<input type="text" name="title"/>
<input type="file" name="post_image"/>
<input type="submit" name="sendpost" value="POST"/>
</form>
any solution? thanks
I think you have mistake in table name in your query .Just replace table with your actual table name.For example if you have defined table name as user then your query must be
$insert_post_and_image = "INSERT INTO user(title, image) VALUES ('$title','$post_image')";
Also you can check error using
if (mysqli_query($connect, $insert_post_and_image)) {
echo "success";
} else {
echo "Error: " . mysqli_error($connect);
}
*correction
Hi, already found the answer . i'm not sure if it's correct or not . please advise. thanks :)
<?php
function insertpost(){
global $connect;
if(isset($_POST['sendpost']))
{
$title = mysqli_real_escape_string($connect,$_POST["title"]);
$target_image = "images/".basename($_FILES['post_image']['name']);
$post_image = $_FILES['post_image']['name'];
$insert_post_and_image = "INSERT INTO table(title, image) VALUES ('$title','$post_image')";
$result = mysqli_query($connect, $insert_post_and_image);
if($result)
{
move_uploaded_file($_FILES['post_image']['tmp_name'], $target_image);
echo "<h3>Posted to timeline!</h3>";
}
}
}
?>
So as the title suggests, I am having trouble executing a MySQL query. The query works almost successfully, as all data fields are stored into my database except for one. The query itself is a commenting system for signed in users to comment on any given blog post. The issue I am having is that the variable '$post_id' is not recognized, and therefore '$comment_post_ID' is not stored in my database.
'$post_id' is defined in blogs.php, and after echoing this variable it does exist and is successfully defined. However, this variable is not passed onto commentsubmit.php, which is included in the same file where the variable is defined. Why is this happening?
Here are all the pieces of my code:
blogs.php (shows all posts from all users, or just one post if ?id is set in the url. If ?id is set, users can comment on the single post they are viewing.)
<?php
if (isset($_GET['id'])) {
$conn = mysqli_connect("localhost", "root", "mypassword", "mydbname");
if (mysqli_connect_errno($conn)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit;
}
$post_id = mysqli_real_escape_string($conn, $_GET['id']);
$blog_post = "SELECT * FROM blogs WHERE id = '$post_id'";
$blog_query = mysqli_query($conn, $blog_post);
while ($row = mysqli_fetch_array($blog_query)) {
$title = $row['title'];
$body = $row['body'];
$author = $row['author'];
$author_username = $row['author_username'];
$datetime = time_ago($row['datetime'], $granularity=2);
}
include ("./fullpageblog.php");
if (isset($_SESSION['id'])) {
include ("./blogcomment.php");
include ("./commentsubmit.php");
}
echo "$post_id";
mysqli_close($conn);
}
?>
blogcomment.php (form for users to make a comment)
<div class="row col-sm-12">
<div id="fullPageBlog">
<div id="center-border"></div>
<form action="commentsubmit.php" method="post">
<textarea maxlength="1000" id="blogComment" name="content" placeholder="Write your response..."></textarea>
<input type="submit" name="comment" value="Publish" />
</form>
<script type="text/javascript">$('#blogPost').elastic();</script>
</div>
</div>
commentsubmit.php (comment query itself)
<?php
session_start();
if (isset($_POST['comment'])) {
$conn = mysqli_connect("localhost", "root", "mypassword", "mydbname");
if (mysqli_connect_errno($conn)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit;
}
$comment_post_ID = $post_id;
$comment_author = $_SESSION['full_name'];
$comment_author_email = $_SESSION['email'];
$comment_author_username = $_SESSION['username'];
$comment_date = date("Y-m-d H:i:s");
$comment_content = mysqli_real_escape_string($conn, $_POST['content']);
$user_ID = $_SESSION['id'];
$comment_submit = "INSERT INTO comments (comment_ID, comment_post_ID, comment_author, comment_author_email, comment_author_username, comment_date, comment_content, user_ID) VALUES ('', '$comment_post_ID', '$comment_author', '$comment_author_email', '$comment_author_username', '$comment_date', '$comment_content', '$user_ID') ";
$comment_query = mysqli_query($conn, $comment_submit);
mysqli_close($conn);
header("Location: blogs.php");
die();
}
?>
You don't include/require the blogs.php script from the commentsubmit.php script, so the code in blogs.php would never be run after a POST that is made directly to commentsubmit.php unless you have some other request processing (i.e. a server-side rewrite or similar) that happens automatically on the server before the request ultimately reaches the portion of code shown in commentsubmit.php above.
<?php
$con = mysql_connect("localhost","root","password");
$con=mysql_select_db("database_name");
error_reporting(0);
session_start();
if(isset($_POST["submit"])){
$comment_author = $_POST['full_name'];
$comment_author_email = $_POST['email'];
$comment_author_username = $_POST['username'];
$sql="select * from table_name where `full_name`='"$comment_author "',`email`='"$comment_author_email "',`username`='"$comment_author_username "'";
$qur=mysql_query($sql);
$row= mysql_fetch_array($qur);
$num= mysql_num_rows($qur);
}
if($num>0){
$_SESSION["full_name"]=$full_name;
$_SESSION["email"]=$comment_author_email;
$_SESSION["username"]=$comment_author_username;
}
else{
echo"Username and Password are wrong";
}
?>
i have this problem regarding file upload on php.
I always get this error msg.
Warning: file_get_contents(): Filename cannot be empty in
C:\xampp\htdocs\omf2\emprecords\add8.php on line 25
this is my line 25
$data = $con->real_escape_string(file_get_contents($_FILES['uploaded_file']['tmp_name']));
But still saves the info on my database.
What i am trying to do is save the rest of the records on my database even if not selecting a file to upload. And yes the records are saved and the Attachment field (mediumblob) is [BLOB - 0 B]
Question: How can i eliminate the error/warning message? (because everything is really fine)
<meta http-equiv="refresh" content="2;URL='emphistory.php'">
<?php
{
echo "<center><font color='#AAA' size='3'><br/>Record Added!</center>";
}
?>
<?php
$con=mysqli_connect("localhost","root","","dbomf");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM valueholder");
$row = mysqli_fetch_array($result);
$count = '';
$IDNUM = $row['Val'];
$NS = addslashes($_POST ['NS']);
$ad = addslashes($_POST ['ad']);
$hr = addslashes($_POST ['HR']);
$name = $con->real_escape_string($_FILES['uploaded_file']['name']);
$data = $con->real_escape_string(file_get_contents($_FILES['uploaded_file']['tmp_name']));
include ('../dbconn.php');
$query = "INSERT INTO tblemphist1 VALUES
('".$count."', '".$IDNUM."', '".$NS."', '".$ad."', '".$hr."', '".$data."', '".$name."')";
$result = $db->query($query) or die($db->error);
$db->close();
here
<form method="post" action="add8.php" enctype="multipart/form-data">
<td><strong>Attachment</strong></td>
<td>:</td>
<td><input type="file" name="uploaded_file"></td>
</tr>
</form>
<input type = "file">
should be
<input name="uploaded_file" type = "file">
also form method should be post and use enctype='multipart/form-data
<form action="" method="post" enctype="multipart/form-data">
<input name="uploaded_file" type = "file">
</form>
also check
$name = ''; $data = '';
if ((is_uploaded_file($_FILES['uploaded_file']['tmp_name']) && !($_FILES['uploaded_file']['error'])) {
$name = $con->real_escape_string($_FILES['uploaded_file']['name']);
$data = $con->real_escape_string(#file_get_contents($_FILES['uploaded_file']['tmp_name']));
}
include ('../dbconn.php');
$query = "INSERT INTO tblemphist1 VALUES ('".$count."', '".$IDNUM."', '".$NS."', '".$ad."', '".$hr."', '".$data."', '".$name."')";
$result = $db->query($query) or die($db->error);
Use an if statement. For example:
if (!empty($_FILES)) {
$data = $con->real_escape_string(
file_get_contents($_FILES['uploaded_file'] ['tmp_name'])
);
}
Before accessing any property of $_FILES['uploaded_file'] you have to check the value $_FILES['uploaded_file']['error']. And yes, it's a good idea to check if such key exists at all - as with anything coming from the user, there is no guarantee that it exists in the request.
Simply check if the variable is not empty
$data = '';
if (!empty($_FILES['uploaded_file']['tmp_name'])) {
$data = $con->real_escape_string(file_get_contents($_FILES['uploaded_file']['tmp_name']));
}
if error doesn't affect your project just ignore it and add this code in top of your php.
<?php ERROR_REPORTING(E_ALL & ~E_NOTICE); ?>
it will ignore and hide the error. :)