Jump to content

Recommended Posts

Hey guys, I have made this page, which basically allows the user to choose a 'companion' and then set the name of it. Its supposed to then set up these details in one of the fields on my table however when I click 'submit' it just takes me to the page where you choose your 'companion' again and doesn't enter there details. Now heres the code:

 

<?
require("includes/connect.inc.php") ;
require("includes/navbar.inc.php") ;

	echo '<link rel=\'stylesheet\' href=\'includes/layoutstylesheet.css\' type=\'text/css\'><div id=\'content\'>' ;
$sql = "SELECT COUNT(*) FROM user WHERE username='".mysql_real_escape_string($_COOKIE['kurukouser'])."'";
$query = mysql_query($sql) or die(mysql_error());
$r = mysql_fetch_row($query);
if($r[0] == 1){

$username = htmlentities($_COOKIE['kurukouser'],ENT_QUOTES,"utf-8");
	echo '<center>' ;
$sql = "SELECT * FROM companion ";
$result = mysql_query($sql) ;
$companion = $_GET['companion'] ;
if(!isset($companion)) {
	echo'Hey '.$username.' welcome to companion rocks, here you can choose what you want you companion to be during your journeys, <br />of course this is optional but the benefits can be great!' ;
while($row=mysql_fetch_array($result)) {
$cname = $row['name'] ;
$cimage = $row['image'] ;
	echo'<br /><br />' ;
	echo'<img src="'.$cimage.'">' ;
	echo'<br />' ;
	echo $cname ;
	echo'<br />' ;
	echo'<a href="http://kurukolands.co.uk/companion.php?companion='.$cname.'">Take</a>' ;

}
}elseif(isset($companion)) {
$sql = "SELECT * FROM companion" ;
$result = mysql_query($sql) ;
while($row=mysql_fetch_array($result)) {
if($row['name'] == $companion) {
$cimage = $row['image'] ;
$cdescription = $row['description'] ;
	echo $companion ;
	echo'<br /><br /><img src="'.$cimage.'">' ;
	echo'<br /><br />'.$cdescription.'' ;
	echo'<form method="post" action="companion.php"><br />Name: <input type="text" name="name"><br /><input type="submit" name="submit" value="Submit">' ;
if(isset($_POST['Submit'])) {
$name = $_POST['name'] ;
$food = 0 ;
$energy = 0 ;
mysql_query("INSERT INTO usercompanion(username, name, food, energy) VALUES ('$username', '$name', '$food', '$energy')") ;
}
}
}
}
}else{

	echo'Error: You must be logged in to view this page.' ;

}
	echo'</div></center>' ;
?>

 

I basically want to know what im doing wrong if anyone could tell me, then I can try to fix it. Thanks.

 

Timecatcher.

Link to comment
https://forums.phpfreaks.com/topic/135949-solved-submit-button-help-needed/
Share on other sites

firstly dont use short start tags as it stops syntax highlighting.

 

change

 

 

mysql_query("INSERT INTO usercompanion(username, name, food, energy) VALUES ('$username', '$name', '$food', '$energy')") ;

 

to

 

mysql_query("INSERT INTO usercompanion(username, name, food, energy) VALUES ('$username', '$name', '$food', '$energy')") or die("Error with query. "mysql_error());

 

and change

$result = mysql_query($sql) ;

to

$result = mysql_query($sql) or die("Error with query2. "mysql_error());

Just a side note, I would also use indentation in my code. It makes it soo much easier to read and debug. As you have it there you have like 5 } in a row and no one can tell which goes with what unless they do a painful trace back.

 

If you are just using notepad, I would find a program that syntax highlights as well, such as notepad ++. It makes it a ton easier to code with.

Oh hehe yeah second person to mention indents xD didn't think about the }'s. Thanks. Oh and im using dreamweaver.

 

Thanks. TC. Still no avail with this whole insert info thing by the way.

 

Dreamweaver is great for designing pages, but I have never used it for PHP coding, but knowing how dreamweaver works I probably wouldn't recommend it. Dreamweaver is all about just getting it running and not caring if someone can actually read through stuff.

 

My 2 cents anyhow, or search if dreamweaver has a php plugin for proper indents etc.

Well I haven't really had any problems, I didn't really like moving from dreamweaver to notepad++ however and needed to change all the colours again xD. I think i'll stick with dreamweaver for now. Any ideas on my actual problem. :D

 

Timecatcher.

No, I do not like trying to cipher non-indented code. Sorry man, it requires too much of my brain power to go and indent it all then find the source of the issue etc, especially when it could easily just be a missing } etc which you have been found with proper indentation. I figure if you cannot take the 5 minutes to properly indent it for us, than yea I cannot take the 5 minutes to diagnose your problem.

 

At least that is my perspective on it.

 

EDIT:

I am not meaning to be rude, sorry just read it and it seemed rude. I am just trying to help you get more help out of these forms, cause I know there are others who think along the same lines.

No thats fine. I will repost the form indented, however im not sure exactly how to so I hope it looks ok! No it wasn't rude :P, I understand people arn't just here to help me hehe!

 

Thanks, Timecatcher.

 

EDIT:

 

Just realised I can't edit previous posts xD. Well heres the code again.

 

<?php
require("includes/connect.inc.php") ;
require("includes/navbar.inc.php") ;

	echo '<link rel=\'stylesheet\' href=\'includes/layoutstylesheet.css\' type=\'text/css\'><div id=\'content\'>' ;
$sql = "SELECT COUNT(*) FROM user WHERE username='".mysql_real_escape_string($_COOKIE['kurukouser'])."'";
$query = mysql_query($sql) or die(mysql_error());
$r = mysql_fetch_row($query);
if($r[0] == 1){

$username = htmlentities($_COOKIE['kurukouser'],ENT_QUOTES,"utf-8");
	echo '<center>' ;
$sql = "SELECT * FROM companion ";
$result = mysql_query($sql) ;
$companion = $_GET['companion'] ;
if(!isset($companion)) {
	echo'Hey '.$username.' welcome to companion rocks, here you can choose what you want you companion to be during your journeys, <br />of course this is optional but the benefits can be great!' ;
while($row=mysql_fetch_array($result)) {
$cname = $row['name'] ;
$cimage = $row['image'] ;
	echo'<br /><br />' ;
	echo'<img src="'.$cimage.'">' ;
	echo'<br />' ;
	echo $cname ;
	echo'<br />' ;
	echo'<a href="http://kurukolands.co.uk/companion.php?companion='.$cname.'">Take</a>' ;

	}
}elseif(isset($companion)) {
$sql = "SELECT * FROM companion" ;
$result = mysql_query($sql) ;
while($row=mysql_fetch_array($result)) {
if($row['name'] == $companion) {
$cimage = $row['image'] ;
$cdescription = $row['description'] ;
	echo $companion ;
	echo'<br /><br /><img src="'.$cimage.'">' ;
	echo'<br /><br />'.$cdescription.'' ;
	echo'<form method="post" action="companion.php"><br />Name: <input type="text" name="name"><br /><input type="submit" name="submit" value="Submit">' ;
if(isset($_POST['Submit'])) {
$name = $_POST['name'] ;
$food = 0 ;
$energy = 0 ;
mysql_query("INSERT INTO usercompanion(username, name, food, energy) VALUES ('$username', '$name', '$food', '$energy')") ;
				}
			}
		}
	}
}else{

	echo'Error: You must be logged in to view this page.' ;

}
	echo'</div></center>' ;
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.