Problem with php script.

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
spear1976
Forum Newbie
Posts: 10
Joined: Sat Jul 20, 2002 1:58 pm
Location: Norway

Problem with php script.

Post by spear1976 »

Hi,

Newbie warning!

I've created (and have recived help here before) a joke database with great help from books etc.
Hoewever i found that the way i used to do thing, was not very good.
I sendt all variables trough a link to a new page, that validated and added the information. Of course this got me into trouble when i added a long joke.

I have also tried to do everything trough a single page, but only recive an error message, but no details. Which is why i hope someone can look trough the code, and see how many things i have done wrong :)

Code: Select all

<?php


if ( isset( $f_tittel ) && isset( $f_gittav ) && isset( $f_epost ) && isset( $f_kategori) && isset( $f_innhold ) ) &#123;
	// User input!
		$error = "";
		$ret = add_to_database( $f_tittel, $f_gittav, $f_epost, $f_kategori, $f_innhold, $error );
		if ( ! $ret )
			print "Error: $error<br>";
		else
			print "Status: Takk skal du ha!";
		&#125; else &#123;
			write_form();
		&#125;
		
FUNCTION add_to_database( $f_tittel, $f_gittav, $f_epost, $f_kategori, $f_innhold, $error ) &#123;

$user = "Testuser";
$pass = "test";
$link = mysql_pconnect( "localhost", $user, $pass );

if ( ! $link ) &#123;
	$error = "Kunne ikke koble til mySQL server";
	return false;
	&#125;
	
	if ( ! mysql_select_db( $db, $link ) ) &#123;
		$error = mysql_error();
		return false;
		&#125;
	
	$query = "INSERT INTO vitser (tittel, kategori, gittav, epost, innhold) values ('$f_tittel','$f_kategori','$f_gittav','$f_epost','$f_innhold')";
	
		if ( ! mysql_query( $query, ¤link ) ) &#123;
			$error = mysql_error();
			return false;
			&#125;
			return true;
			&#125;


FUNCTION write_form() &#123;
$connection=mysql_connect("localhost","Testuser","test");

	
	#Se om det funket!
	if (!$connection) &#123;
		echo "Klarte ikke opprette forbindelse til mySQL serveren!";
		exit;
	&#125;
	
	# Hvilken database skal vi bruke?
	
	$db=mysql_select_db("vitser",$connection);
	
	#Se at man kan bytte database
	if (!$db) &#123;
		echo "Klarte ikke endring til databasen!";
		exit;
	&#125;

	$version = 'v1.0.0.3a' ;
	$software = 'VitseDB' ;
	$navbar = '	| <a href=/demo/>Hovedsiden</A> | <a href=leggtil.php>Ny Vits</A> | <a href=velg_kategori.php>Velg Kategori</A> | <a href=sisteX.php>Siste 5 vitser</A> |<br><br> ' ;
			 
	global $PHP_SELF;

		
$sql="SELECT kategori FROM kategori";

$mysql_result=mysql_query($sql,$connection);
$num_rows=mysql_num_rows($mysql_result);
	if ( $num_rows == 0 ) &#123;
		echo "$software - $version";
		echo "<br><br>";
		echo "$navbar";
		echo "Beklager, ingen informasjon tilgjengelig";
	&#125; else &#123;
	# Fant oppføringer
	echo "$software - $version";
	echo "<br><br>";
	echo "$navbar";
	echo "<FORM METHOD=POST>";
#	$f_tittel=strip_it($f_tittel);
#	$f_gittav=strip_it($f_gittav);
#	$f_epost=strip_it($epost);
#	$f_innhold=strip_it($f_innhold);
#    $f_innhold = nl2br($row&#1111;"innhold"]);
	
	echo "Tittel på vitsen<br> <INPUT TYPE="TEXT" NAME="f_tittel" VALUE="$f_tittel"><br>";
	echo "Ditt navn<br>  <INPUT TYPE="TEXT" NAME="f_gittav" VALUE="$f_gittav"><br>";
	echo "Din E-Post<br>  <INPUT TYPE="TEXT" NAME="f_epost" VALUE="$f_epost"><br>";	
	echo "Vits:<br>   <textarea name="f_innhold" cols="50" rows="10" WRAP>$f_innhold</textarea><br>";	
	
	# Velg kategori
	echo "Velg kategori<br>";
	echo "<SELECT NAME="f_kategori">";
	
	# FINN INFORMASJON OM KATEGORIER
	while ($row=mysql_fetch_array($mysql_result))
		&#123;
			$f_kategori=$row&#1111;"kategori"];
			# Vis resultat
				echo "<OPTION>$f_kategori";
			&#125;
			echo "</select>";
			&#125; # END ELSE
			echo "<br><br>";
			echo "<INPUT TYPE="SUBMIT" VALUE="OK">";
			echo "</FORM>";
			
			echo "<FORM METHOD=GET ACTION="$PHP_SELF">";
			echo "<INPUT TYPE="SUBMIT" VALUE="Visk ut!">";
			mysql_close($connection);
			
		&#125;
		
?>
User avatar
hex
Forum Commoner
Posts: 92
Joined: Sat Apr 20, 2002 3:20 am
Location: UK

Post by hex »

Aaaaggghhhh!! It's all foreign!!

P.S. Post more descriptive subjects please.
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

c'mon hex, "problem with php script" is pretty descriptive, I don't know what you're talking about.

Anyway, spear, on trying out your script, I got no errors, except that (obviously) there were no results returned from the db. What's the problem?
mgordon
Forum Newbie
Posts: 4
Joined: Wed Jul 24, 2002 9:25 am

Look out for globals

Post by mgordon »

I'm not sure that I really understand what it is your trying to do. I suggest also that you start using the $_GET/$_POST variables so that you won't have problems when you upgrade to PHP 4.2 This means that instead of checking the $f_tittel you check the $_POST[$f_tittel]

Where is your form action? Something should happend when you do this, right?

Whats with this:
global $PHP_SELF;
I can't see you using it.

Comments in PHP are ususally writte using the // commentline or /* comment */ syntax, please use this and not # if you want people to read your code.

Also try to work with your indentation. If you are going to as people to read your code then please present it nicely :!: Try also to use english and not your local language since most people don't speak any of the nordic languages.
Ex. of proper indentation

Code: Select all

if()
&#123;
    if()
    &#123;
         if()
         &#123;
              code
          &#125;else&#123;
              code
          &#125;
     &#125;

     // Comment
     code
&#125;
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

just curious, which nordic language is this? anybody know?
mgordon
Forum Newbie
Posts: 4
Joined: Wed Jul 24, 2002 9:25 am

Language

Post by mgordon »

Not Swedish, that's for sure. I think it's Norwegian.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

A search on "Kunne ikke koble til mySQL server" leads me to believe it may be Norwegian...

Mac
User avatar
spear1976
Forum Newbie
Posts: 10
Joined: Sat Jul 20, 2002 1:58 pm
Location: Norway

Thanks again

Post by spear1976 »

Hi,
twigletmac wrote:A search on "Kunne ikke koble til mySQL server" leads me to believe it may be Norwegian...
It was a good thing i started the thread with "newbie warning" :-)
First off, the language is Norwegian.

And yes, there is a *lot* of strange things found in it. I'm just learning php/mySQL, and have created my first script. Which is composed of many bits and pices found on "howto" pages around the world.
Some thing does not need to be there, other things are missing :(

Anyway i figured out my problem (or at least one other way of getting it to work the way i want to).I think the mai problem was that i had mixed way, way to many bits of code, so i stripped it down, and started from ground zero.

I've recived many helpfull tips here, both from reading other messages, and from reading the replies to this one. I thank you all for helping, and promise that if i write again, i will give better descriptions of my problems :)

Anyway, thanks for helping and newbie getting started with scripting.
And for replying / trying to help, even though my code is not good enough...

Best regards,
Spear (Norway)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Spear, I think the main problem was that because all the comments and variable names were in Norwegian so it was difficult to read the code and easily follow what you were trying to achieve. I appreciate that English is not the first language of most people but it is the language in general use on this board. It does make it easier to help if scripts are translated so that variable names and comments are in English.

Mac
Post Reply