Jump to content


Photo

Broken Code?


  • Please log in to reply
3 replies to this topic

#1 jvo

jvo
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 12 April 2006 - 07:19 PM

I am trying to implement a website using PHP and so far it's going rather well. Today I ran upon a bit of a hiccup. I've written some code folloing a tutorial that allows people to enter information to a database and it worked fine for a little while. But now it's broken. On the user end, it looks like it works, but when I actually browse the contents of the database, the information has not been added. I'm relatively new to the PHP/MySQL platform but as I'm not so familiar with the syntax yet, I could very well be over looking something quite simple. Below is the code from the two pages I have.

SIGNUP.HTML
<html>
<head> <title>.:: New User ::. </title></head>

<body>

<font face="arial" pt size=1>

<form action="insert.php" method="post">

Username: <input type="text" name="user"><br>
Password: <input type="password" name="password"><br>
Confirm Password: <input type="password" name="password"><br>
Email: <input type="text" name="email"><br>

First Name: <input type="text" name="first"><br>
Last Name: <input type="text" name="last"><br>
Primary Contact: <input type="text" name="phone"><br>
Age: <input type="text" name="age"><br>
Gender: <input type="radio" name="gender" value="M">Male
<input type="radio" name="gender" value="F">Female<br>

<input type="Submit">
</form>

</body>
</html>



INSERT.PHP

<?php
include("library/dblogin.php");

$user=$_POST['user'];
$password=$_POST['password'];
$email=$_POST['email'];

$first=$_POST['first'];
$last=$_POST['last'];
$phone=$_POST['phone'];
$age=$_POST['age'];
$gender=$_POST['gender'];

$query = "INSERT INTO users VALUES
('','$user','$password','$email','$first','$last','$phone','$age','$gender','0')";
mysql_query($query);


mysql_close();

?>

<html>
<head>
<title> .:: New User ::. </title>
</head>

<body>
<font face="arial" pt size="1">
<p align="center">
Your account has been successfully created.<br>
Thank you for registering. <br>
</p>
</body>
</html>

So there it is. Is there any reason why this code shouldn't work? Any insight would be helpful.

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 12 April 2006 - 07:26 PM

If nothing is being added to your Database then there is a likely chance you have an error with your Query. To find out change this:
mysql_query($query);
to:
mysql_query($query) or die("Error: " . mysql_error());
Run you code again this time if there is a problem with your query it'll stop the script and show an error message. if it does return an error, post the full error message here and we'll try to help.

#3 jvo

jvo
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 12 April 2006 - 07:34 PM

[!--quoteo(post=364165:date=Apr 12 2006, 03:26 PM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Apr 12 2006, 03:26 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
If nothing is being added to your Database then there is a likely chance you have an error with your Query. To find out change this:
mysql_query($query);
to:
mysql_query($query) or die("Error: " . mysql_error());
Run you code again this time if there is a problem with your query it'll stop the script and show an error message. if it does return an error, post the full error message here and we'll try to help.
[/quote]


Hey, thanks so much for the fast reply. I did exactly what you said and here's what I get

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 ''users' VALUES ('','johndoe','0000','john@doe.com','John','D

I'm rechecking my code now, but it still looks fine. Any advise?

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 12 April 2006 - 07:43 PM

Add echoing the $query in the "or die" clause:
<?php
mysql_query($query) or die("Error in query: $query<br>" . mysql_error());
?>

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users