I've worked out this code to upload properly along with some help and now I can't see the username in the database when I do upload the image. Is there something I need to include like session_start()
into the upload file? I think I already have my session set whenever I log in.
<?php
$msg = "";
if (isset($_POST['upload'])) {
$target = "profile/images/".basename($_FILES['image']['name']);
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
$db = mysqli_connect("localhost", "root", "", "database");
$result = mysqli_query($con, "SELECT * FROM users WHERE username = '" . $username. "'");
if ($row = mysqli_fetch_array($result)) {
$_SESSION['username'] = $row['username'];
}
$username = $row['username'];
$images = $_FILES['image']['name'];
$sql = "INSERT INTO user (images,username) VALUES ('$images', '$username')";
mysqli_query($db, $sql);
$msg = "Image Uploaded Successfully";
header("Location: images.php?uploadsuccess");
} else {
$msg = "There Was A problem uploading image";
}
}
?>
There might be something wrong with my query I believe.
<?php
session_start();
if(isset($_SESSION['user_id'])) {
header("Location: index.php");
}
include_once 'dbconnect.php';
//set validation error flag as false
$error = false;
//check if form is submitted
if (isset($_POST['signup'])) {
$first_name = mysqli_real_escape_string($con, $_POST['first_name']);
$last_name = mysqli_real_escape_string($con, $_POST['last_name']);
$username = mysqli_real_escape_string($con, $_POST['username']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$password = mysqli_real_escape_string($con, $_POST['password']);
$cpassword = mysqli_real_escape_string($con, $_POST['cpassword']);
$quote = mysqli_real_escape_string($con, $_POST['quote']);
$who = mysqli_real_escape_string($con, $_POST['who']);
//name can contain only alpha characters and space
$sql = "SELECT * FROM users WHERE username = '".$username."'";
$result = mysqli_query($con,$sql);
if(mysqli_num_rows($result)>=1){
$error = true;
$username_error ='<div class="error-notice">
<div class="oaerror danger">
<strong>Uh oh!</strong> - That Username is already taken. Please use a different Username.
</div>';
}else{
}
$sql = "SELECT * FROM users WHERE email = '".$email."'";
$result = mysqli_query($con,$sql);
if(mysqli_num_rows($result)>=1){
$error = true;
$email_exist ='<div class="error-notice">
<div class="oaerror danger">
<strong>Uh oh!</strong> - That E-mail already in use. Please use a different E-mail.
</div>';
}else{
}
if (!preg_match("/^[a-zA-Z ]+$/",$first_name)) {
$error = true;
$firstname_error = '<div class="error-notice">
<div class="oaerror danger">
<strong>Uh oh!</strong> - First Name must contain only alphabets and space.
</div>';
}
if (!preg_match("/^[a-zA-Z ]+$/",$last_name)) {
$error = true;
$lastname_error = '<div class="error-notice">
<div class="oaerror danger">
<strong>Uh oh!</strong> - Last Name must contain only alphabets and space.
</div>';
}
if(!filter_var($email,FILTER_VALIDATE_EMAIL)) {
$error = true;
$email_error = '<div class="error-notice">
<div class="oaerror danger">
<strong>Uh oh!</strong> - Please Enter Valid E-mail.
</div>';
}
if(strlen($password) < 6) {
$error = true;
$password_error = '<div class="error-notice">
<div class="oaerror danger">
<strong>Uh oh!</strong> - Password must be minimum of 6 characters.
</div>';
}
if($password != $cpassword) {
$error = true;
$cpassword_error = '<div class="error-notice">
<div class="oaerror danger">
<strong>Uh oh!</strong> - Make sure your passwords match.
</div>';
}
if (!$error) {
if(mysqli_query($con, "INSERT INTO users(first_name,last_name,username,email,password,quote,who) VALUES('" . $first_name . "', '" . $last_name . "', '" . $username . "', '" . $email . "', '" . md5($password) . "', '" . $quote . "', '" . $who . "')")) {
$successmsg = '<div class="error-notice">
<div class="oaerror success">
<strong>Wooh!</strong> - Successfully Registered! <a href="login.php">Click here to Login</a>
</div>';
} else {
$errormsg = '<div class="error-notice">
<div class="oaerror info">
<strong>Hmm.</strong> - Error in registering...Please try again later.
</div>';
}
}
}
?>