getting a error with my code...

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 890 Views - Last Post: 30 August 2009 - 10:28 AM Rate Topic: -----

#1 nick1200  Icon User is offline

  • Php Coder
  • member icon

Reputation: -18
  • View blog
  • Posts: 922
  • Joined: 21-March 09

getting a error with my code...

Post icon  Posted 30 August 2009 - 07:06 AM

My forum


	<div id="mid_section">
		<div class="mid_section_inner">
		
		  <div class="mid_box">
			  <div class="mid_box_title"></div>
					</div>
			
			<div class="mid_box">
				<div class="mid_box_title">Want to make a team?</div>
						
						<form>
				<label></label>
				<br/>
				<br/>
				<label>What do you want your Team To Be Called ? </label>
				<form action="insert.php" method="post">
Team Name: 
  <input type="text" name="teamname" />
<input type="submit" />
</form>

</body>
</html> <br/>
				  </p>
						</form>	
			
				<div style="clear:both;"></div>
			</div>
		</div>				
	</div>
	




insert.php
<?php	
	include(' ../include/session.php')

	
?>

<?php
$con = mysql_connect("localhost","shadow","mypass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("pokemon", $con);

$sql="INSERT INTO team (username, team_name)
VALUES
('$_POST[username]','$_POST[teamname])";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
?> 





im getting this error

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '')' at line 3

Is This A Good Question/Topic? 0
  • +

Replies To: getting a error with my code...

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,533
  • Joined: 08-August 08

Re: getting a error with my code...

Posted 30 August 2009 - 07:16 AM

$sql="INSERT INTO team (username, team_name)
VALUES
('$_POST[username]','$_POST[teamname])";


is missing a ' near the end:
$sql="INSERT INTO team (username, team_name)
VALUES
('$_POST[username]','$_POST[teamname]')";


Was This Post Helpful? 0
  • +
  • -

#3 nick1200  Icon User is offline

  • Php Coder
  • member icon

Reputation: -18
  • View blog
  • Posts: 922
  • Joined: 21-March 09

Re: getting a error with my code...

Posted 30 August 2009 - 07:22 AM

View PostCTphpnwb, on 30 Aug, 2009 - 06:16 AM, said:

$sql="INSERT INTO team (username, team_name)
VALUES
('$_POST[username]','$_POST[teamname])";


is missing a ' near the end:
$sql="INSERT INTO team (username, team_name)
VALUES
('$_POST[username]','$_POST[teamname]')";




Now it goes to the page and a clikc the button to add but it adds nothing to the db it does but it dont add what i put if that makes sence it makes a new collum / line ) but nothing in them

This post has been edited by nick1200: 30 August 2009 - 07:25 AM

Was This Post Helpful? 0
  • +
  • -

#4 sfurnival  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 52
  • Joined: 27-August 09

Re: getting a error with my code...

Posted 30 August 2009 - 07:39 AM

Couple things you should get into the habit of doing.

<input type="text" name="teamname" />



Any time your using a form tag, set both the id & name like so:

<input type="text" id="teamname" name="teamname" />



Secondly you don't have a form field for username yet, either add a field for it or give it a default value somewhere near the beginning of insert.php

Lastly, try to get in the habit of not putting your variables directly into your query. Avoid this:

$sql="INSERT INTO team (username, team_name) VALUES ('$_POST[username]','$_POST[teamname]')";



There are a few reason not to do this, but not really something i want to get into here. Change your query to:

$sql="INSERT INTO team (username, team_name) VALUES ('" . $_POST['username'] . "','" . $_POST['teamname'] . "')";


Was This Post Helpful? 0
  • +
  • -

#5 nick1200  Icon User is offline

  • Php Coder
  • member icon

Reputation: -18
  • View blog
  • Posts: 922
  • Joined: 21-March 09

Re: getting a error with my code...

Posted 30 August 2009 - 07:46 AM

View Postsfurnival, on 30 Aug, 2009 - 06:39 AM, said:

Couple things you should get into the habit of doing.

<input type="text" name="teamname" />



Any time your using a form tag, set both the id & name like so:

<input type="text" id="teamname" name="teamname" />



Secondly you don't have a form field for username yet, either add a field for it or give it a default value somewhere near the beginning of insert.php

Lastly, try to get in the habit of not putting your variables directly into your query. Avoid this:

$sql="INSERT INTO team (username, team_name) VALUES ('$_POST[username]','$_POST[teamname]')";



There are a few reason not to do this, but not really something i want to get into here. Change your query to:

$sql="INSERT INTO team (username, team_name) VALUES ('" . $_POST['username'] . "','" . $_POST['teamname'] . "')";





it adds the username but not what ever the user puts into the box even tho we cant see the box now
Heres my two new pages i noticed there was a error with the sessions was diffrent on each page so heres my 2 new pages


<?php	
	include("insert.php"); 
		include('include/session.php'); 
	if (!$session->logged_in) {
		header("Location: login.php");
	}



	// all the stuff at the top of the page
	require('sections/new_pagehead.php');

	// all the stuff on the left column of the page
		include_once('sections/new_leftbar.php');
?>

	<div id="mid_section">
		<div class="mid_section_inner">
		
		  <div class="mid_box">
			  <div class="mid_box_title"></div>
					</div>
			
			<div class="mid_box">
				<div class="mid_box_title">Want to make a team?</div>
						
						<form>
				<label></label>
				<br/>
				<br/>
				<label>What do you want your Team To Be Called ? </label>
				<form action="insert.php" method="post">
Team Name: 
<input type="text" id="teamname" name="teamname" />
<input type="submit" />
</form>

</body>
</html> <br/>
				  </p>
						</form>	
			
				<div style="clear:both;"></div>
			</div>
		</div>				
	</div>
	
	
<?php
	// all the stuff on the right column of the page
		include_once('sections/new_rightbar.php');
		
	// close everything down with a footer
	require('sections/footer.php');
?>



insert.php
<?php	
	include('include/session.php'); 

	
?>

<?php
$con = mysql_connect("localhost","shadow","mypass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("pokemon", $con);

$sql = "INSERT INTO team (`username`, `team_name`) VALUES ('" . $_SESSION['username'] . "', '" . $_POST['teamname']. "')";



if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
?> 



i am getting 1 record added
when the page loads

no forum or nothing ....

This post has been edited by nick1200: 30 August 2009 - 08:03 AM

Was This Post Helpful? 0
  • +
  • -

#6 sfurnival  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 52
  • Joined: 27-August 09

Re: getting a error with my code...

Posted 30 August 2009 - 08:07 AM

This here:

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";



should probably be:

if (!mysql_query($sql,$con))  {
	die('Error: ' . mysql_error());
} else {
	echo "1 record added";
}



That way it will only echo if the sql command was executed properly. (thats not to say what you wanted to happen has happened, only that mysql liked the command)

You might want to temporarily try echoing the values to the page before trying to insert them in the database just to double check that they do have values:

echo 'Team Name: ' . $_POST['teamname'] . '<br />';
echo 'User Name: ' . $_SESSION['username'];



Put that code in right before you run the query to make sure they have the values your expecting.
Was This Post Helpful? 0
  • +
  • -

#7 arshem  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-March 09

Re: getting a error with my code...

Posted 30 August 2009 - 08:10 AM

The reason you're not getting anything except the echo, is because thats' all you have, to go back to your forum, you'd need to link back to your forum, I.E.

Header("Location: YOURFORUMURL.com");exit;

at the end of the document
Was This Post Helpful? 0
  • +
  • -

#8 nick1200  Icon User is offline

  • Php Coder
  • member icon

Reputation: -18
  • View blog
  • Posts: 922
  • Joined: 21-March 09

Re: getting a error with my code...

Posted 30 August 2009 - 08:12 AM

View Postsfurnival, on 30 Aug, 2009 - 07:07 AM, said:

This here:

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";



should probably be:

if (!mysql_query($sql,$con))  {
	die('Error: ' . mysql_error());
} else {
	echo "1 record added";
}



That way it will only echo if the sql command was executed properly. (thats not to say what you wanted to happen has happened, only that mysql liked the command)

You might want to temporarily try echoing the values to the page before trying to insert them in the database just to double check that they do have values:

echo 'Team Name: ' . $_POST['teamname'] . '<br />';
echo 'User Name: ' . $_SESSION['username'];



Put that code in right before you run the query to make sure they have the values your expecting.




there is still no forum there i have uploaded a screeney to tinny pic

http://tinypic.com/v...pic=2yodgth&s=3

thats when the page is loaded
there is no button or anything which should be there
Was This Post Helpful? 0
  • +
  • -

#9 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 150
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: getting a error with my code...

Posted 30 August 2009 - 08:31 AM

this ..

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";



is perfectly fine ... because you will not see the echo if there is a error because of the die statement ...

this ...

echo 'Team Name: ' . $_POST['teamname'] . '<br />';
echo 'User Name: ' . $_SESSION['username'];



would be a good idea for developing because it will show you exactly what is set to the post variable and the session username

@sfurnival im curious as to why you think a ID is necessary for a input element when the post array works based on the name attribute and nothing else .... the only reason to set a ID is if your using some javascript and for ease of access have the id available

past that the reason you don't see anything else is cause the page doesnt have anything else ... if your wanting to go back to the page you were just at like someone said use a header

header("Location: forum.php");



be aware that has tobe called BEFORE anything is outputted to the browser so your echo command becomes pretty much useless ...

This post has been edited by RPGonzo: 30 August 2009 - 08:34 AM

Was This Post Helpful? 0
  • +
  • -

#10 nick1200  Icon User is offline

  • Php Coder
  • member icon

Reputation: -18
  • View blog
  • Posts: 922
  • Joined: 21-March 09

Re: getting a error with my code...

Posted 30 August 2009 - 08:35 AM

View PostRPGonzo, on 30 Aug, 2009 - 07:31 AM, said:

this ..

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";



is perfectly fine ... because you will not see the echo if there is a error because of the die statement ...

this ...

echo 'Team Name: ' . $_POST['teamname'] . '<br />';
echo 'User Name: ' . $_SESSION['username'];



would be a good idea for developing because it will show you exactly what is set to the post variable and the session username

@sfurnival im curious as to why you think a ID is necessary for a input element when the post array works based on the name attribute and nothing else .... the only reason to set a ID is if your using some javascript and for ease of access have the id available

past that the reason you don't see anything else is cause the page doesnt have anything else ... if your wanting to go back to the page you were just at like someone said use a header

header("Location: forum.php");



be aware that has tobe called BEFORE anything is outputted to the browser so your echo command becomes pretty much useless ...




but i am going to the forum the maketeam.php which has stuff on
when i go to it it dont show the page ( which i showed in the screen shoot )
Was This Post Helpful? 0
  • +
  • -

#11 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 150
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: getting a error with my code...

Posted 30 August 2009 - 08:38 AM

Your form is landing you on insert.php .... so thats where the data has to be that you want to see ... if you want to have that information submitted on the same page that insert code needs to go before anything else and before running it put it in a conditional statement so it ONLY runs if the post variable you are looking for is set ....
Was This Post Helpful? 0
  • +
  • -

#12 sfurnival  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 52
  • Joined: 27-August 09

Re: getting a error with my code...

Posted 30 August 2009 - 08:40 AM

RPGonzo has provided the solution to your forum not showing, I was only fixing your INSERT problem.

And yes your right RPgonzo, i skimmed and thought he was echoing the error and not dieing with error.
Was This Post Helpful? 0
  • +
  • -

#13 nick1200  Icon User is offline

  • Php Coder
  • member icon

Reputation: -18
  • View blog
  • Posts: 922
  • Joined: 21-March 09

Re: getting a error with my code...

Posted 30 August 2009 - 08:42 AM

View PostRPGonzo, on 30 Aug, 2009 - 07:38 AM, said:

Your form is landing you on insert.php .... so thats where the data has to be that you want to see ... if you want to have that information submitted on the same page that insert code needs to go before anything else and before running it put it in a conditional statement so it ONLY runs if the post variable you are looking for is set ....



so i would need to make the forum on the insert.php page :blink: ?

all i wanna do is have a simple forum with 1 box on the user enters a team name into the box then it fills in team_name in the db and stores there username has well

and the date would be copyed from the forum to the insert.php my sql statment
Was This Post Helpful? 0
  • +
  • -

#14 sfurnival  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 52
  • Joined: 27-August 09

Re: getting a error with my code...

Posted 30 August 2009 - 08:45 AM

header("Location: forum.php");



Add that to the bottom of insert.php, after the row is inserted, it's been said a few times already...
Was This Post Helpful? 0
  • +
  • -

#15 nick1200  Icon User is offline

  • Php Coder
  • member icon

Reputation: -18
  • View blog
  • Posts: 922
  • Joined: 21-March 09

Re: getting a error with my code...

Posted 30 August 2009 - 08:49 AM

View Postsfurnival, on 30 Aug, 2009 - 07:45 AM, said:

header("Location: forum.php");



Add that to the bottom of insert.php, after the row is inserted, it's been said a few times already...



now im getting a white blank page


<?php	
	include('include/session.php'); 

	
?>

<?php
$con = mysql_connect("localhost","shadow","bonnie");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("pokemon", $con);

$sql = "INSERT INTO team (`username`, `team_name`) VALUES ('" . $_SESSION['username'] . "', '" . $_POST['teamname']. "')";


if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";


mysql_close($con)
header("Location: maketeam.php");
?> 



put it after its added the row
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2