0

I wonder if anyone could help me with my problem? I want to send value (input text) to mysql database but it is always blank text. I am the beginner and I think I've made stupid mistake... Code:

<form name="form" method="get">
    <input type="text" name="nick">
    <input type="text" name="message" height="300px">
</form>
        <?php
$servername = "localhost";
$username = "root";
$password = "xxx";
$dbname = "xxxxx";

if (isset($_POST['button1'])) 
{ 
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$nickval = $_POST['nick'];
$messageval = $_REQUEST['message'];
$sql = "INSERT INTO Messages (nick, message)
VALUES ('$_GET[nick]', '$_GET[message]')";

if ($conn->query($sql) === TRUE) {
    echo "OK";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
$result = mysqli_query($conn,"SELECT * FROM Messages");

echo "<table border='1'>
<tr>
<th>Nick</th>
<th>Message</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['nick'] . "</td>";
echo "<td>" . $row['message'] . "</td>";
echo "</tr>";
}
echo "</table>";
$conn->close();
}
?>
<form method="POST" action=''>
<input type="submit" name="button1"  value="Send">
</form>
1
  • 1
    Why are you using $_POST, $_REQUEST, and also $_GET? Your form method is POST so you should only be using $_POST to retrieve your inputs from the the form. Commented Dec 6, 2014 at 15:39

2 Answers 2

1

This:

<input type="submit" name="button1"  value="Send">

needs to go inside your first form, where your other inputs are.

<form name="form" method="post">
    <input type="text" name="nick">
    <input type="text" name="message" height="300px">
    <input type="submit" name="button1"  value="Send">
</form>

And also @Joe T's answer. Many problems wrong with this question it seems

Sign up to request clarification or add additional context in comments.

Comments

0

Your SQL should look more like this, the other answer is also right (about moving your submit button inside the same form with your inputs)

$nickval =  mysqli_real_escape_string ( $conn , $_POST['nick']);
$messageval = mysqli_real_escape_string ( $conn , $_POST['message']);
$sql = "INSERT INTO Messages (nick, message)
VALUES ( '$nickval', '$messageval')";

As the commenter wrote, don't use $_REQUEST and $_GET, only $_POST is used here.
And if you don't escape your inputs, (as i've done here with mysqli_real_escape_string) you are asking for a world of hurt.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.