14 Replies - 1473 Views - Last Post: 10 December 2009 - 12:56 PM Rate Topic: -----

#1 Elbrus   User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 107
  • Joined: 22-July 08

Inserting into DB

Posted 09 December 2009 - 07:15 PM

I am self teaching my self php, in this I was trying to insert the data from the basic form I made and insert it into the db. When I try to run it I get a error it reads "Parse error: syntax error, unexpected $end in /home/a5148149/public_html/register.php on line 31"

This is my first try at doing a php script like this so expect alot of stupid errors on my part, but that is what learning is about right :)


 <?php

$mysql_host = "";
$mysql_database = "";
$mysql_user = "";
$mysql_password = "";

$name=$_POST['name'];
$username=$_POST['username'];
$pass=$_POST['pass'];
$email=$_POST['email'];


$cid = mysql_connect($mysql_host,$mysql_user,$mysql_password);
if (!$cid)
{

die('Error ' . mysql_error());

}

mysql_select_db($mysql_database, $cid);

mysql_query("INSERT INTO users (name, email, username, pass)
VALUES ('$name','$email','$username','$pass');



mysql_close($cid)

?>

This post has been edited by Elbrus: 10 December 2009 - 09:16 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Inserting into DB

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6747
  • View blog
  • Posts: 31,229
  • Joined: 10-May 07

Re: Inserting into DB

Posted 09 December 2009 - 07:35 PM

Quote

$name=$_POST['name'];
$username=$_POST['username'];
$pass=$_POST['pass'];
$email=$_POST['email'];



:crazy: Ewww. You don't do any error checking on the post variables!!! This could be why you don't know why it isn't working ;)

Although, your following line is missing the end quote. Replace with what I provided below.
mysql_query("INSERT INTO users (name, email, username, pass)
VALUES ('$name','$email','$username','$pass'");

Was This Post Helpful? 0
  • +
  • -

#3 trizz   User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 10-October 08

Re: Inserting into DB

Posted 10 December 2009 - 01:26 AM

You post always your DB user/pass on a forum?

Anyway, to quickly find the problem in your query, always add something like or die(mysql_error()); after you query (and not with an IF or something. Unnecessary code should be avoided.)

For example:

mysql_query("INSERT INTO users (name, email, username, pass)
VALUES ('$name','$email','$username','$pass'") or die(mysql_error());

This post has been edited by trizz: 10 December 2009 - 01:29 AM

Was This Post Helpful? 0
  • +
  • -

#4 Elbrus   User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 107
  • Joined: 22-July 08

Re: Inserting into DB

Posted 10 December 2009 - 08:16 AM

I have tried both suggestions, and to no avail, it is still not working, I am still getting the error that I posted in my first post. It says that there is a unexpected stop on line 31. Line 31 is the last line and only has
?>




on it. Dose any one else have a idea on what could be causing this. It may be my hosting provider? I use ooowebhosting.com

This post has been edited by Elbrus: 10 December 2009 - 08:18 AM

Was This Post Helpful? 0
  • +
  • -

#5 Elbrus   User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 107
  • Joined: 22-July 08

Re: Inserting into DB

Posted 10 December 2009 - 08:40 AM

View Postno2pencil, on 9 Dec, 2009 - 06:35 PM, said:

Quote

$name=$_POST['name'];
$username=$_POST['username'];
$pass=$_POST['pass'];
$email=$_POST['email'];



:crazy: Ewww. You don't do any error checking on the post variables!!! This could be why you don't know why it isn't working ;)



How would I do error checking on the post variables, I am still very new to php and not sure how.
Was This Post Helpful? 0
  • +
  • -

#6 danny_kay1710   User is offline

  • D.I.C Regular
  • member icon

Reputation: 29
  • View blog
  • Posts: 359
  • Joined: 27-April 08

Re: Inserting into DB

Posted 10 December 2009 - 08:42 AM

mysql_close($cid)
should be
mysql_close($cid);


Note the semi-colon; ;)

EDIT: Please remove your password from the Mysql connection string (in fact the entire block of $mysql vars would be better) Anyone who looks at this site(don't even have to be members) could just wipe that entire database given the information you have provided. UPDATE: Admin has done this for you I believe

This post has been edited by danny_kay1710: 10 December 2009 - 08:50 AM

Was This Post Helpful? 0
  • +
  • -

#7 Elbrus   User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 107
  • Joined: 22-July 08

Re: Inserting into DB

Posted 10 December 2009 - 09:16 AM

View Postdanny_kay1710, on 10 Dec, 2009 - 07:42 AM, said:

mysql_close($cid)
should be
mysql_close($cid);


Note the semi-colon; ;)

EDIT: Please remove your password from the Mysql connection string (in fact the entire block of $mysql vars would be better) Anyone who looks at this site(don't even have to be members) could just wipe that entire database given the information you have provided. UPDATE: Admin has done this for you I believe


Ahh the mysql information, I did not think about that, even though I understand, and will do this from now on. The database, and site is used for practicing and learning php, so no important data at all is in the db. Even with that said I will do this for now on thanks for the heads up.

** I have also removed all the information to the db, thanks agian :) **

I am going to ad the semi-colon now hopefully work. :)

This post has been edited by Elbrus: 10 December 2009 - 09:17 AM

Was This Post Helpful? 0
  • +
  • -

#8 Elbrus   User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 107
  • Joined: 22-July 08

Re: Inserting into DB

Posted 10 December 2009 - 09:27 AM

I have added the semi-colon, and still geting the error could this possibly be a problem from my hosting provider. Dose any one know a good free web hosting provider I can use to practice php. I do not have the computer power to run one on local host
Was This Post Helpful? 0
  • +
  • -

#9 nick1200   User is offline

  • Php Coder
  • member icon

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

Re: Inserting into DB

Posted 10 December 2009 - 10:52 AM

View PostElbrus, on 10 Dec, 2009 - 08:27 AM, said:

I have added the semi-colon, and still geting the error could this possibly be a problem from my hosting provider. Dose any one know a good free web hosting provider I can use to practice php. I do not have the computer power to run one on local host


000 web host

http://www.000webhost.com/

got to be the best around :)
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6248
  • View blog
  • Posts: 24,016
  • Joined: 23-August 08

Re: Inserting into DB

Posted 10 December 2009 - 11:09 AM

Post your entire current code (less the MySQL login parameters). This is not a hosting provider problem, it's a syntax error in your code.
Was This Post Helpful? 0
  • +
  • -

#11 Elbrus   User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 107
  • Joined: 22-July 08

Re: Inserting into DB

Posted 10 December 2009 - 11:12 AM

View Postnick1200, on 10 Dec, 2009 - 09:52 AM, said:

000 web host

http://www.000webhost.com/

got to be the best around :)



Ya that is the place I use, the code I have must just have a error that I can not find
Was This Post Helpful? 0
  • +
  • -

#12 Elbrus   User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 107
  • Joined: 22-July 08

Re: Inserting into DB

Posted 10 December 2009 - 11:18 AM

The php code
<?php



$name=$_POST['name'];
$username=$_POST['username'];
$pass=$_POST['pass'];
$email=$_POST['email'];


$cid = mysql_connect($mysql_host,$mysql_user,$mysql_password);
if (!$cid)
{

die('Error ' . mysql_error());

}

mysql_select_db($mysql_database, $cid);

mysql_query("INSERT INTO users (name, email, username, pass)
VALUES ('$name','$email','$username','$pass');



mysql_close($cid);

?>


html file ( I know it is not up to w3 standers, but this was only made of the fact of learning php nothing else)

   
	  <html>
<head>
	<title>Registure Online</title>
<style type="text/css">

#conta {
position:absolute;
top:0;
left:50%;
width: 600px;
margin: 0 0 0 -302px;

}

h1 {

text-align:center;

}

#header{
position:absolute;
top:0;
left:0;

background-color: blue;
heigh:250px;
width:100%;


}



form {

padding:10px;
margin:0;
position:absolute;
top:5em;
left:0;
background-color:#FFCC66;
}

#conta #links { position:absolute;
top:0em;
right:-11.3em;
background-color:red;
width:43%;
height:100%;

}



</style>
</head>
<body>
<div id="conta">
<div id="header">
<h1> Welcome to the Online registration page.</h1>
</div>

<form action="register.php" method="post" id="form">
<formset>
<legend>Please use the Form below to complete your regisutre information.</legend>

<p>
<label>Name: </label><input type="text" name="name" /><br /><br />
<label>Account Name: </label><input type="text" name="username" />
<br /><br />
<label>Password: </label><input type="password" name="pass" /><br /><br />
<label>Email: </label><input type="email" name="email" />
<br /><br /><input type="submit" value="Registure" /><input type="reset" />
</formset>
</p>
</form>
<div id="links">
<ul>
<li><a href="" >Home</a></li>
<li><a href="" >About Us</a></li>
<li><a href="" >Products</a></li>
</ul>
</div>
</div>
</body>
</html>

Was This Post Helpful? 0
  • +
  • -

#13 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6248
  • View blog
  • Posts: 24,016
  • Joined: 23-August 08

Re: Inserting into DB

Posted 10 December 2009 - 11:45 AM

mysql_query("INSERT INTO users (name, email, username, pass)
VALUES ('$name','$email','$username','$pass');



There are two things missing in that line. What are they?
Was This Post Helpful? 1
  • +
  • -

#14 Elbrus   User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 107
  • Joined: 22-July 08

Re: Inserting into DB

Posted 10 December 2009 - 12:45 PM

View PostJackOfAllTrades, on 10 Dec, 2009 - 10:45 AM, said:

mysql_query("INSERT INTO users (name, email, username, pass)
VALUES ('$name','$email','$username','$pass');



There are two things missing in that line. What are they?


I believe I see the mistakes correct me if I am wrong the correct code should be
mysql_query("INSERT INTO users (name, email, username, pass)
VALUES ('$name','$email','$username','$pass')");



It finally works! Thank you so much! Cant believe i did not catch that my self, but I guess that is what learning is all about. Thanks again!

This post has been edited by Elbrus: 10 December 2009 - 12:53 PM

Was This Post Helpful? 0
  • +
  • -

#15 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6248
  • View blog
  • Posts: 24,016
  • Joined: 23-August 08

Re: Inserting into DB

Posted 10 December 2009 - 12:56 PM

You got it, good job.

If you use a syntax-highlighting editor, that will help make those errors easier to spot.

Windows, I suggest Notepad++ or SciTE
Mac, TextWrangler is a good choice
Unix, vim or emacs are what I know
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1