Switch function, wont work?!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JohnDigweed
Forum Newbie
Posts: 18
Joined: Sun Mar 28, 2004 6:24 pm
Location: root/user/hell

Switch function, wont work?!

Post by JohnDigweed »

Ok, I am developing a News Poster script to practice my PHP skills.

The script contains 2 files. add.html, and news.php. Add.html is the HTML document with the form to fill out. News.PHP displays the news, and also has a switch function in there that stores the news in the MySQL database, and reads off it. Here I will post the script of each.

Add.html

Code: Select all

<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Title of News</title>
</head>

<body>

<form action="new.php?act=add" method="post">
        <p>Title of News :<input type="text" name="title" size="56"></p>
        <p>News : </p>
        <p><textarea rows="9" name="text" cols="60"></textarea></p>
        <p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>

</body>

</html>
I have no problem with that one. The problem I have is with the news.php

News.php

Code: Select all

<?
SWITCH($HTTP_GET_VARS&#1111;act]) &#123;
case "add":
$title = $_POST&#1111;'title'];
$text = $_POST&#1111;'text'];
mysql_connect('localhost','mySQLusername','mySQLpassword);
mysql_select_db('Databasename');
if(empty($title) || empty($text)) exit("You didnt completley fill out the form! Go back");
$title = addslashes($title); $text = addslashes($text);
mysql_query("INSERT INTO news (title,text) VALUES ('$title','$text')");
print "You have succesfully created a new story!";
break;
// Code above stores the new story in the databse

default:
mysql_connect('localhost','mySQLusername','mySQLpassword');
// logs into the mysql database
mysql_select_db('Databasename');
// name of the dedicated databse
$query = mysql_query('SELECT * FROM news');
// gets all information in the news table
while($news = mysql_fetch_array($query)) &#123;
echo "Title:" . $news&#1111;'title'] . "n";
echo $news&#1111;'text'] . "n";
// This displays news.php because it is default. This reads news out of the database.
&#125;
?>
When I go to news.php, all I see is a blank page. When I try to add news, all i see from news.php?act=add is a blank white page. Nothing is working.

Anyone have any ideas?

Thanks!
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

change

Code: Select all

SWITCH($HTTP_GET_VARS[act]) {
to

Code: Select all

switch($_GET['act']) {
Also:
To reduce code, if you're doing something in both cases (ie. connecting to the DB), do it before the switch statement.

[edit]What is "n" for?? did you mean "\n"? (line break)
JohnDigweed
Forum Newbie
Posts: 18
Joined: Sun Mar 28, 2004 6:24 pm
Location: root/user/hell

Post by JohnDigweed »

HI, I tested it out again, and it still doesnt work. Here is my new script that I updated :

News.php

Code: Select all

<?
switch($_GET&#1111;'act']) &#123;
case "add":
$title = $_POST&#1111;'title'];
$text = $_POST&#1111;'text'];
mysql_connect('localhost','sqluser','sqlpass');
mysql_select_db('sqldbname');
if(empty($title) || empty($text)) exit("You didnt completley fill out the form! Go back");
$title = addslashes($title); $text = addslashes($text);
mysql_query("INSERT INTO news (title,text) VALUES ('$title','$text')");
print "You have succesfully created a new story!";
break;
// Code above stores the new story in the databse

default:
mysql_connect('localhost','mySQLusername','mySQLpassword');
// logs into the mysql database
mysql_select_db('Databasename');
// name of the dedicated databse
$query = mysql_query('SELECT * FROM news');
// gets all information in the news table
while($news = mysql_fetch_array($query)) &#123;
echo "Title:" . $news&#1111;'title'] . "\n";
echo $news&#1111;'text'] . "\n";
// This displays news.php because it is default. This reads news out of the database.
&#125;
?>
It still displays a blank white page.

Thanks!
Last edited by JohnDigweed on Sun Mar 28, 2004 10:12 pm, edited 1 time in total.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

try changing those addslashes to stripslashes. al you may want to create a sepereate string for you SQL statement, then echo it out to make sure it looks correct. And also put some or die(mysql.error()) on the end of your queries.

Code: Select all

$title = stripslashes($title);
$text = stripslashes($text);
$sql = "INSERT INTO news (title,text) VALUES ('$title','$text')";
mysql_query($sql) or die(mysql.error());
And another thing, im almost 100% positive that you error reporting all OFF because you never close the Switch.
try adding another } after that last one.
JohnDigweed
Forum Newbie
Posts: 18
Joined: Sun Mar 28, 2004 6:24 pm
Location: root/user/hell

Post by JohnDigweed »

Thanks!

You got it! It finally works. You know, I posted this on 2 other popular forums, and no one was able to fix it. Congrats, you are a true expert :D
Post Reply