PDA

View Full Version : Not inserting my query AGAIN!?



Hitman
01-12-2007, 10:39 PM
Man, my coding is getting better. I see no problems with this, nor does Beau. But the problem is this: It wont insert the info into the database! I don't get any error messages or anything... the vars work, I've tested. It's just not putting anything into the db!


<?php
include 'config.php';
session_start();
$date = date("d/m/Y");

foreach($_POST as $key=>$val) {

$$key = clean($val);

}

if ($_SESSION['logged_user'] == true) {
$query = mysql_query("SELECT * FROM `users` WHERE `username` = '".$_SESSION['logged_user']."'");

$rows = mysql_fetch_array($query);

if ($rows['level'] == 5) {
if ($_POST['submit']) {
echo "News added!";
$sql = mysql_query("INSERT INTO `news` (title, shortcontent, content, by, date) VALUES ('$title','$shortcontent','$content',''".$_SESSION['logged_user']."'','$date')");
}
if (!$_POST['submit']){
echo "<form action=\"addnews.php\" method=\"POST\">
News title: <input type=\"text\" size=\"20\" name=\"title\">
</br>
</br>
Short content: <input type=\"text\" size=\"20\" name=\"shortcontent\">
</br>
</br>
Content: <input type=\"text\" size=\"20\" name=\"content\">
</br>
</br>
<input type=\"submit\" name=\"submit\" value=\"Add news!\">
</form>
";
}
} else {
echo "You're not meant to be here, shoo. <meta http-equiv=\"REFRESH\" content=\"1;url=./members.php\">";
}
} else {
echo "Not logged in... Redirecting... <meta http-equiv=\"REFRESH\" content=\"1;url=./login.php\">";
}
?>

Tell me whats wrong! :(

rh4u
01-12-2007, 10:43 PM
<?php
include 'config.php';
session_start();
$date = date("d/m/Y");

foreach($_POST as $key=>$val) {

$$key = clean($val);

}

if ($_SESSION['logged_user'] == true) {
$query = mysql_query("SELECT * FROM `users` WHERE `username` = '".$_SESSION['logged_user']."'");

$rows = mysql_fetch_array($query);

if ($rows['level'] == 5) {
if ($_POST['submit']) {
echo "News added!";
echo ("$sql");
$sql = mysql_query("INSERT INTO `news` (title, shortcontent, content, by, date) VALUES ('$title','$shortcontent','$content',''".$_SESSION['logged_user']."'','$date')");
}
if (!$_POST['submit']){
echo "<form action=\"addnews.php\" method=\"POST\">
News title: <input type=\"text\" size=\"20\" name=\"title\">
</br>
</br>
Short content: <input type=\"text\" size=\"20\" name=\"shortcontent\">
</br>
</br>
Content: <input type=\"text\" size=\"20\" name=\"content\">
</br>
</br>
<input type=\"submit\" name=\"submit\" value=\"Add news!\">
</form>
";
}
} else {
echo "You're not meant to be here, shoo. <meta http-equiv=\"REFRESH\" content=\"1;url=./members.php\">";
}
} else {
echo "Not logged in... Redirecting... <meta http-equiv=\"REFRESH\" content=\"1;url=./login.php\">";
}
?>

Dentafrice,
01-12-2007, 10:43 PM
<?php
include 'config.php';
session_start();
$date = date("d/m/Y");

if ($_SESSION['logged_user'] == true) {
$query = mysql_query("SELECT * FROM `users` WHERE `username` = '" . $_SESSION['logged_user'] .
"'");

$rows = mysql_fetch_array($query);

if ($rows['level'] == 5) {
if ($_POST['submit']) {
echo "News added!";
$title = $_POST["title"];
$shortcontent = $_POST["shortcontent"];
$content = $_POST["content"];
$username = $_SESSION["logged_user"];
mysql_query("INSERT INTO news (title, shortcontent, content, by, date) VALUES ('$title', '$shortcontent', '$content', '$username', '$date')");

}
if (!$_POST['submit']) {
echo "<form action=\"addnews.php\" method=\"POST\">
News title: <input type=\"text\" size=\"20\" name=\"title\">
</br>
</br>
Short content: <input type=\"text\" size=\"20\" name=\"shortcontent\">
</br>
</br>
Content: <input type=\"text\" size=\"20\" name=\"content\">
</br>
</br>
<input type=\"submit\" name=\"submit\" value=\"Add news!\">
</form>
";
}
} else {
echo "You're not meant to be here, shoo. <meta http-equiv=\"REFRESH\" content=\"1;url=./members.php\">";
}
} else {
echo "Not logged in... Redirecting... <meta http-equiv=\"REFRESH\" content=\"1;url=./login.php\">";
}
?>

rh4u
01-12-2007, 10:43 PM
try that :)

Hitman
01-12-2007, 10:45 PM
But why is
foreach($_POST as $key=>$val) {

$$key = clean($val);

} taken away on dentafrices? It works for register. It's in config btw.

Dentafrice it still doesn't work.

rh4u
01-12-2007, 10:47 PM
then try mine ;0

Dentafrice,
01-12-2007, 10:49 PM
Because I don't like doing it that way, I think it is better to define your own.

If someone used firebug or any other program for that matter to make their own field for something..

Hitman
01-12-2007, 10:52 PM
Because I don't like doing it that way, I think it is better to define your own.

If someone used firebug or any other program for that matter to make their own field for something..But then for my protection of the $_POST's... I used clean($_POST['whatever']); but it broke something once... is that better (clean($_POST['whatever']);) though?

I tried yours rh4u! No luck. :(

Hitman
02-12-2007, 11:14 AM
I can't edit...

Anybody know what's wrong? I can insert manually in phpMyAdmin but not with this.

:s

Snex
02-12-2007, 12:37 PM
Try:

<?php
include 'config.php';
session_start();
$date = date("d/m/Y");

foreach($_POST as $key=>$val) {

$$key = clean($val);

}

if ($_SESSION['logged_user'] == true) {
$query = mysql_query("SELECT * FROM `users` WHERE `username` = '".$_SESSION['logged_user']."'");

$rows = mysql_fetch_array($query);

if ($rows['level'] == 5) {
if ($_POST['submit']) {
echo "News added!";
$by = $_SESSION['logged_user'];
$sql = mysql_query("INSERT INTO `news` (`title`, `shortcontent`, `content`, `by`, `date`) VALUES ('$title','$shortcontent','$content','$by','$date' )") or die('Could not insert data, Error: '. mysql_error());
}
if (!$_POST['submit']){
echo "<form action=\"\" method=\"POST\">
News title: <input type=\"text\" size=\"20\" name=\"title\">
</br>
</br>
Short content: <input type=\"text\" size=\"20\" name=\"shortcontent\">
</br>
</br>
Content: <input type=\"text\" size=\"20\" name=\"content\">
</br>
</br>
<input type=\"submit\" name=\"submit\" value=\"Add news!\">
</form>
";
}
} else {
echo "You're not meant to be here, shoo. <meta http-equiv=\"REFRESH\" content=\"1;url=./members.php\">";
}
} else {
echo "Not logged in... Redirecting... <meta http-equiv=\"REFRESH\" content=\"1;url=./login.php\">";
}
?>

Will also display any MySQL errors if there are any.

Hitman
02-12-2007, 02:41 PM
Thanks snex it works now!

+REP.

Thread closed.

Want to hide these adverts? Register an account for free!