I'm working on a blog right now that aims to display messages to only people it belongs to, so I have a select in html where people can select a person and then it sends it to that table in MySQL.
What I now have in the index.html:
<form action="post.php" method="post">
<label>Naam:</label>
<input type="text" name="name" placeholder="Naam" class="form-control">
<label>Voor wie is dit bericht bestemd?</label>
<select name="portal" class="form-control">
<option id="0">Selecteer</option>
<option id="1">Leerlingen</option>
<option id="2">Docenten</option>
<option id="3">Ouders</option>
<option id="4">Bedrijven</option>
</select>
</div>
<div class="paper col-sm-6">
<label>Email:</label>
<input type="email" placeholder="Email" class="form-control">
<label>Onderwerp:</label>
<input type="textarea" class="form-control" placeholder="Onderwerp" name="subject"/>
</div>
<div class="paper col-sm-12">
<label>Korte informatie:</label>
<input class="form-control" type="textarea" name="short"/>
<label>Volledige informatie</label>
<textarea class="form-control" rows="4" cols="50" name="long"></textarea>
</div>
<div class="paper col-sm-12 text-center">
<div class="col-xs-12" style="height:25px;"></div>
<button class="btn btn-default">Verstuur!</button>
</form>
and this in my post.php:
<?php
$servername = "localhost";
$username = "a1070rik";
$password = "";
$dbname = "portals";
$title = '$_POST[subject]';
$by = '$_POST[name]';
$short = '$_POST[short]';
$long = '$_POST[long]';
$portal = '$_POST[portal]';
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO $portal (id, title, by, short, long)
VALUES ('', $title, $by, $short, $long)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
When I try to run it it gives me this vague error:
Error: INSERT INTO $_POST[portal] (id, title, by, short, long) VALUES ('', $_POST[subject], $_POST[name], $_POST[short], $_POST[long])
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[portal] (id, title, by, short, long) VALUES ('', $_POST[subject], $_POST[name],' at line 1
Thanks
EDIT:
Thanks everyone that helped,
this code eventually worked for me:
<?php
$servername = "localhost";
$username = "a1070rik";
$password = "";
$dbname = "portals";
$title = $_POST['subject'];
$by_information = $_POST['name'];
$short = $_POST['short'];
$long_information = $_POST['long'];
$portal = $_POST['portal'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO $portal (`id`, `title`, `info_bys`, `info_shorts`, `info_longs`) VALUES ('', '$title', '$by', '$short', '$long')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>