-1

i want to store image and text in database after authenticate username and password?

I have two files:

  1. login.html
  2. insert.php

login.html

<html>
<body>

<form action="insert.php" method="post">
username <input type="text" name="username" />
password <input type="text" name="password" />
image <input type="file" name="image" />
message <input type="text" name="message" />

<input type="Submit" /></form>

insert.php

<?php

$conn_error='could not connect.';
$mysql_host='localhost';
$mysql_user='ommzbocg_myappu';
$mysql_pass='d*BQTk2zScNb';
$mysql_db='ommzbocg_myapp';
$tbl_name='data';

$username=$_POST['username'];
$password=$_POST['password'];
$image=$_POST['image'];
$message=$_POST['message'];

if(!@mysql_connect($mysql_host, $mysql_user, $mysql_pass)      ||!@mysql_select_db($mysql_db))
{ die($conn_error);
}
$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row $count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row if($count==1){

$query = "INSERT INTO data VALUES('$username','$password','$image','$message')";
}else{
echo '0';
}
if($query)
{
echo "1";

}
else
{ 
echo"Error";
}

mysql_query($query);
mysql_close();
?>

i can only authenticate but i can't able to store image and data .

Raptor
  • 53,206
  • 45
  • 230
  • 366
  • And what is your question? What is the problem? Any error message? anything unexpected happening? Does the universe implode? What do the logfiles say? Why don't you check the errors of your mysql statements? Sorry: first try to solve this yourself, only if you have a specific problem you cannot solve yourself, _then_ ask here. This is not a place to dump some code and have others fix it. – arkascha Oct 18 '13 at 05:52
  • http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Ryan Oct 18 '13 at 05:53
  • @arkascha the problem is that i can't able to store image and text in my database... – user2893257 Oct 18 '13 at 05:57
  • You didn't really read my comment, did you? – arkascha Oct 18 '13 at 06:15
  • 1
    *sidenote:* stop using deprecated `mysql_*` functions. use MySQLi or PDO instead. – Raptor Oct 18 '13 at 06:15

1 Answers1

0

first modify your login.html and replace your form tag with below form tag to upload the file

<form method="post" action="insert.php" enctype="multipart/form-data">

and make sure that data type should be blob or long blob(depend on your file size), in which you are storing image/file.

than use code below to before insert into table

$tmpName  = $_FILES['image']['tmp_name'];
$fp = fopen($tmpName, 'r');
$image = fread($fp, filesize($tmpName));
$image = addslashes($content);
fclose($fp);

I will suggest you to go through with this nice article http://mirificampress.com/permalink/saving_a_file_into_mysql to add image in db.

Ram Sharma
  • 8,676
  • 7
  • 43
  • 56