PollyJuice
Technical User
Hello,
I have some trouble getting a posting-script to work for a forum I'm developing. I know the sessions work, I've tried that, but nothing gets posted into the db. I suspect the lines
$user = $_SESSION['userid'];
and
$query = "INSERT INTO thread(title, text, datum, Kat, forfattare) VALUES ('$title', '$text', '$datum', '$kat', '$user')";
are to blame. If I change '$user' to the member's number, like 1 or 2 or something, it inserts the new post properly, but with '$user', all that happends is that it goes back to the index-page.
Why doesn't the database understand who the user is?
<?php
session_start();
if (isset($_SESSION['userid'])) {
$title = $_POST["title"];
$text = $_POST["text"];
$datum = date("Y-m-d H:i:s");
$kat = $_POST["kategori"];
if ($kat == "Webb") {
$kat = "1";
}
elseif ($kat == "Grafik") {
$kat = "2";
}
elseif ($kat == "Flash") {
$kat = "3";
}
$user = $_SESSION['userid'];
// Anslut till MySQL på datorn 'localhost'
$conn = mysql_connect("**.***.**.**", "****", "********") or die("Could not connect: " . mysql_error());
// Anslut till databasen med namn 'grafiket'
$db_selected = mysql_select_db("grafiket", $conn) or die("Can't use testdb : " . mysql_error());
// Skapa den SQlsats som ska lägga in data i tabellen 'thread'
$query = "INSERT INTO thread(title, text, datum, Kat, forfattare) VALUES ('$title', '$text', '$datum', '$kat', '$user')";
// Exekvera SQLsatsen och lagra resultatet i variabeln $result
$result = mysql_query($query);
//gå tillbaka till index_online.php
header("Location: index_online.php");
//Avsluta databaskopplingen
mysql_close($conn);
exit;
}
else {
// if not set, send back to login
header("Location: login.html");
}
?>
I have some trouble getting a posting-script to work for a forum I'm developing. I know the sessions work, I've tried that, but nothing gets posted into the db. I suspect the lines
$user = $_SESSION['userid'];
and
$query = "INSERT INTO thread(title, text, datum, Kat, forfattare) VALUES ('$title', '$text', '$datum', '$kat', '$user')";
are to blame. If I change '$user' to the member's number, like 1 or 2 or something, it inserts the new post properly, but with '$user', all that happends is that it goes back to the index-page.
Why doesn't the database understand who the user is?
<?php
session_start();
if (isset($_SESSION['userid'])) {
$title = $_POST["title"];
$text = $_POST["text"];
$datum = date("Y-m-d H:i:s");
$kat = $_POST["kategori"];
if ($kat == "Webb") {
$kat = "1";
}
elseif ($kat == "Grafik") {
$kat = "2";
}
elseif ($kat == "Flash") {
$kat = "3";
}
$user = $_SESSION['userid'];
// Anslut till MySQL på datorn 'localhost'
$conn = mysql_connect("**.***.**.**", "****", "********") or die("Could not connect: " . mysql_error());
// Anslut till databasen med namn 'grafiket'
$db_selected = mysql_select_db("grafiket", $conn) or die("Can't use testdb : " . mysql_error());
// Skapa den SQlsats som ska lägga in data i tabellen 'thread'
$query = "INSERT INTO thread(title, text, datum, Kat, forfattare) VALUES ('$title', '$text', '$datum', '$kat', '$user')";
// Exekvera SQLsatsen och lagra resultatet i variabeln $result
$result = mysql_query($query);
//gå tillbaka till index_online.php
header("Location: index_online.php");
//Avsluta databaskopplingen
mysql_close($conn);
exit;
}
else {
// if not set, send back to login
header("Location: login.html");
}
?>