• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

The if statement when message is not empty doest work

Adel

New Coder
PHP:
<?php
session_start();
if(isset($_SESSION['unique_id'])){
include_once "config.php";
$outgoing_id = mysqli_real_escape_string($conn, $_POST['outgoing_id']);
$incoming_id = mysqli_real_escape_string($conn, $_POST['incoming_id']);
$message = mysqli_real_escape_string($conn, $_POST['message']);

if(!empty($message)){
if(isset($_FILES['mediaB'])){
$img_name = $_FILES['mediaB']['name']; //geting user uploadded image name
$tmp_name = $_FILES['mediaB']['tmp_name'];
$img_explode = explode('.', $img_name);
$img_ext = end($img_explode);
$extensions = ['png', 'jpeg', 'jpg'];

if(in_array($img_ext, $extensions) === true){
$time = time(); //this will return us current time
// let's move the user uploaded img to our particular folder
$new_img_name = $time.$img_name;
if(move_uploaded_file($tmp_name, "images/".$new_img_name)){
$sql = mysqli_query($conn, "INSERT INTO messages (incoming_msg_id, outgoing_msg_id, msg, msg_media)
VALUES ({$incoming_id}, {$outgoing_id}, '{$message}', '{$new_img_name}')") or die();
}
}
}else{
$sql = mysqli_query($conn, "INSERT INTO messages (incoming_msg_id, outgoing_msg_id, msg)
VALUES ({$incoming_id}, {$outgoing_id}, '{$message}')") or die();
}
}else{
if(isset($_FILES['mediaB'])){
$img_name = $_FILES['mediaB']['name']; //geting user uploadded image name
$tmp_name = $_FILES['mediaB']['tmp_name'];
$img_explode = explode('.', $img_name);
$img_ext = end($img_explode);
$extensions = ['png', 'jpeg', 'jpg'];

if(in_array($img_ext, $extensions) === true){
$time = time(); //this will return us current time
// let's move the user uploaded img to our particular folder
$new_img_name = $time.$img_name;
if(move_uploaded_file($tmp_name, "images/".$new_img_name)){
$sql = mysqli_query($conn, "INSERT INTO messages (incoming_msg_id, outgoing_msg_id, msg_media)
VALUES ({$incoming_id}, {$outgoing_id}, '{$new_img_name}')") or die();
}
}

}
}
}else{
header("../login.php");
}
?>
 
Last edited by a moderator:

OldMan

Coder
I do not like the way your code is structured. Mine my not be right, but it does give you a starting point for clearer code.
I insert values to all columns in the DB (I think - I don't know the structure of the DB), sometimes the value is empty sometimes it has a value.
Tell me how this works out for you, please.
Code:
<?php
session_start();
if(isset($_SESSION['unique_id'])){
    include_once "config.php";
    $outgoing_id = mysqli_real_escape_string($conn, $_POST['outgoing_id']);
    $incoming_id = mysqli_real_escape_string($conn, $_POST['incoming_id']);
    $message = mysqli_real_escape_string($conn, $_POST['message']);
    $tmp_name = $_FILES['mediaB']['tmp_name'];
    $img_explode = explode('.', $img_name);
    $img_ext = end($img_explode);
    $extensions = ['png', 'jpeg', 'jpg'];
    $time = time();

    $img_name = '';
    $new_img_name = '';
        if(isset($_FILES['mediaB'])) $img_name = $_FILES['mediaB']['name'];

        if(in_array($img_ext, $extensions) === true) $new_img_name = $time.$img_name;

    $sql = mysqli_query($conn, "INSERT INTO messages VALUES ($incoming_id, $outgoing_id, $message, $new_img_name)");

}else{
    header("../login.php");
}
?>
 

Malcolm

Administrator
Administrator
Staff Team
I do not like the way your code is structured. Mine my not be right, but it does give you a starting point for clearer code.
I insert values to all columns in the DB (I think - I don't know the structure of the DB), sometimes the value is empty sometimes it has a value.
Tell me how this works out for you, please.
Code:
<?php
session_start();
if(isset($_SESSION['unique_id'])){
    include_once "config.php";
    $outgoing_id = mysqli_real_escape_string($conn, $_POST['outgoing_id']);
    $incoming_id = mysqli_real_escape_string($conn, $_POST['incoming_id']);
    $message = mysqli_real_escape_string($conn, $_POST['message']);
    $tmp_name = $_FILES['mediaB']['tmp_name'];
    $img_explode = explode('.', $img_name);
    $img_ext = end($img_explode);
    $extensions = ['png', 'jpeg', 'jpg'];
    $time = time();

    $img_name = '';
    $new_img_name = '';
        if(isset($_FILES['mediaB'])) $img_name = $_FILES['mediaB']['name'];

        if(in_array($img_ext, $extensions) === true) $new_img_name = $time.$img_name;

    $sql = mysqli_query($conn, "INSERT INTO messages VALUES ($incoming_id, $outgoing_id, $message, $new_img_name)");

}else{
    header("../login.php");
}
?>
Hey there, just wanted to chime in. The spaces between lines were actually the result of when I put the code within the BBCode, for some reason when you copy the code from the thread to the code insert it will add a space between the lines. I have corrected this now on the original post.
 
The manual dictates that for comparison of equality you need to use ==. what you are doing is assigning a blank value to the variable by using the assignment operator =.
So your code would be
if($pw != $re-pw)
{
print "error: passwords do not match";
}
else if($username == "" || $pw == "" || $re-pw == "") //doesn't execute? (changed from = to ==)
{
print "please fil out all fields";
}
else
{
//this executes
}
 

Top