unable to insert data into table user_detail using jquery i've three files index.php, upload.php, function.php
// index.php
<?php
require_once('function.php');
session_start();
if (!is_user()) {
redirect('signin.php');
}
$ip = $_SERVER['REMOTE_ADDR'];
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Home</title>
<link rel="stylesheet" type="text/css" href="css/pure-min.css">
<link rel="stylesheet" type="text/css" href="css/style.css">
<link href="css/uploadfile.css" rel="stylesheet">
<script src="js/jquery.min.js"></script>
<script src="js/jquery.uploadfile.min.js"></script>
</head>
<body>
<div class="container">
<h1>Hello <?php echo $_SESSION['username'];?>!</h1>
<p>You are now logged in!</p>
<a href="signout.php" class="pure-button">Logout</a>
<br /><br />
<div id="mulitplefileuploader">Upload</div>
<div id="status"></div>
<script>
$(document).ready(function()
{
var settings = {
url: "upload.php",
method: "POST",
allowedTypes:"prn",
fileName: "myfile",
formData: {"username":"<? $_SESSION['username']; ?>"},
multiple: false,
onSuccess:function(files,data,xhr)
{
$("#status").html("<font color='green'>Upload is success</font>");
},
onError: function(files,status,errMsg)
{
$("#status").html("<font color='red'>Upload is Failed</font>");
}
}
$("#mulitplefileuploader").uploadFile(settings);
});
</script>
<p>Your IP is: <?php print $ip; ?></p>
</div>
</body>
</html>
// upload.php
<?php
//If directory doesnot exists create it.
$output_dir = "uploads/";
$username = $_POST['username'];
if(isset($_FILES["myfile"]))
{
$ret = array();
$error =$_FILES["myfile"]["error"];
{
if(!is_array($_FILES["myfile"]['name'])) //single file
{
$fileName = $_FILES["myfile"]["name"];
move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir. $_FILES["myfile"]["name"]);
//echo "<br> Error: ".$_FILES["myfile"]["error"];
require_once('function.php');
dbconnect();
session_start();
readusers($username);
$ip = $_SERVER['REMOTE_ADDR'];
$date = date('Y-m-d');
insertuser($username, $smsuser, $fileName, $ip, $date);
$ret[$fileName]= $output_dir.$fileName;
}
else
{
$fileCount = count($_FILES["myfile"]['name']);
for($i=0; $i < $fileCount; $i++)
{
$fileName = $_FILES["myfile"]["name"][$i];
$ret[$fileName]= $output_dir.$fileName;
move_uploaded_file($_FILES["myfile"]["tmp_name"][$i],$output_dir.$fileName );
}
}
}
echo json_encode($ret);
}
?>
// function.php
function dbconnect()
{
global $pdo;
try {
$pdo = new PDO('mysql:host=localhost;dbname=sendsms', 'root', '');
} catch (PDOException $e) {
die('MySQL connection fail! ' . $e->getMessage());
}
}
function readusers($username)
{
global $pdo;
$stmt = $pdo->prepare('
SELECT id, username, smsuser
FROM users
WHERE username = :username
LIMIT 1');
$stmt->execute(array(':username' => $username, ':smsuser' => $smsuser));
if ($data = $stmt->fetch( PDO::FETCH_OBJ )) {
# set session
$smsuser = $data->smsuser;
return true;
} else {
return false;
}
}
function insertuser($username, $smsuser, $filename, $date, $ip)
{
# insert new user info
global $pdo;
$stmt = $pdo->prepare('
INSERT INTO user_detail
(username, smsuser, filename, sentdate, ip)
values (:username, :smsuser, fileName, date, ip)');
$stmt->execute( array(':username' => $username, ':smsuser' => $smsuser, ':filename' => $fileName, ':sentdate' => $date, ':ip' => $ip) );
if ($pdo->lastInsertId())
return true;
else
return false;
}
i hope somebody will help me to fix this issue.