-3

I'm making a chat feature for a site and am not good with PHP:

<?php

if (isset($_POST['send'])) {
    require 'database.php';

    $input = $_POST['input'];
} else {
    $sql = "INSERT INTO chatsys (chat) VALUES (?)";
    $stmt = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt, $sql)) {
        header("index.html?error=sqlerror");
        exit();
    } else {
        mysqli_stmt_bind_param($stmt, "sss", $input);
        mysqli_stmt_execute($stmt);
        header("index.html?request=success");
        exit();
    }
}
{
    mysqli_stmt_close($stmt);
    mysqli_close($conn);
}

And database code:

<?php

$servername = "localhost";
$dBUsername = "root";
$dBPassword = "";
$dBName = "chatsys";

$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName);

if (!$conn) {
    die("Connection failed: ".mysqli_connect_error());
}
?>

This results in:

Notice: Undefined variable: conn in C:\Users\John Doe\Desktop\server\htdocs\php\message\chat.php on line 11
Warning: mysqli_stmt_init() expects parameter 1 to be mysqli, null given in C:\Users\John Doe\Desktop\server\htdocs\php\message\chat.php on line 11
Warning: mysqli_stmt_prepare() expects parameter 1 to be mysqli_stmt, null given in C:\Users\john doe\Desktop\server\htdocs\php\message\chat.php on line 12

What have I done wrong?

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 2
    `PHP` is not shit code, but may be your code was bugged. You should not use `require database.php` inside `if`. If you do obviously it will only require the file only when your condition evaluates to true. And please post what error you are getting, so that we can help you better. – Harish ST May 29 '20 at 02:29
  • And your code contains unnecessary `opening {` and `closing paranthesis }`. Check your code. – Harish ST May 29 '20 at 02:30
  • 1
    Why the question then? – Funk Forty Niner May 29 '20 at 02:33
  • Thanks for helping I'm getting these errors: – helpppppppp May 29 '20 at 02:49
  • Notice: Undefined variable: conn in C:\Users\John Doe\Desktop\server\htdocs\php\message\chat.php on line 11 Warning: mysqli_stmt_init() expects parameter 1 to be mysqli, null given in C:\Users\John Doe\Desktop\server\htdocs\php\message\chat.php on line 11 Warning: mysqli_stmt_prepare() expects parameter 1 to be mysqli_stmt, null given in C:\Users\john doe\Desktop\server\htdocs\php\message\chat.php on line 12 – helpppppppp May 29 '20 at 02:49

2 Answers2

1

First of all, don't blame PHP because it is a powerful and easy to use server side language, try to get close and you will love it.

Second you have unneccessary if else, and also passing extra parameters to bind param, while you just have only one to pass.

<?php

if (isset($_POST['send'])) {
    require 'database.php';
    $input = $_POST['input'];
    $sql = "INSERT INTO chatsys (chat) VALUES (?)";
    $stmt = mysqli_stmt_init($conn);

    if (!mysqli_stmt_prepare($stmt, $sql)) {
        header("index.html?error=sqlerror");
        exit();
    }
    //No need to else here because if error happens you get back and exit.
    mysqli_stmt_bind_param($stmt, "s", $input);
    mysqli_stmt_execute($stmt);
    header("index.html?request=success");

    mysqli_stmt_close($stmt);
    mysqli_close($conn);
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Hardood
  • 503
  • 1
  • 5
  • 15
1

You are overcomplicating your code. You do not need all these braces or if statements. You do not need to check the return value of mysqli calls if you enable error reporting.

<?php

// If value was posted to the server
if (isset($_POST['send'])) {
    // include mysqli connection
    require 'database.php';

    // perform prepared statement. (prepare/bind/execute)
    $stmt = $conn->prepare("INSERT INTO chatsys (chat) VALUES (?)");
    $stmt->bind_param("sss", $_POST['input']);
    $stmt->execute();
    // redirect on success
    header("index.html?request=success");
    exit();
}

and your connection file:

<?php

$servername = "localhost";
$dBUsername = "root";
$dBPassword = "";
$dBName = "chatsys";

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName);
$conn->set_charset('utf8mb4'); // always set the charset
Dharman
  • 30,962
  • 25
  • 85
  • 135