Jump to content

[SOLVED] Somethign Wrong With My Code?


LostKID

Recommended Posts

Hi everyone im having a bit of a problem i cant seem to connect to my database.. well i cant seem to insert any information to be precise. im ok pulling information back from the database which leads me to believe there may be something wrong with my coding.. could you lend me some time to show me where i have gone wrong?

 

PHP Processing code after registering details.

<?php 
// CONNECT TO THE DATABASE
mysql_connect ("localhost", "xxx", "xxx") or die ('Error: ' . mysql_error());
mysql_select_db ("xxx");

// CALL IN VARIABLES
$fname = $_POST['fname'];
$lname = $_POST['lname'];

// THE QUERY
$query = "INSERT INTO user (fname,lname) VALUES ('$fname', '$lname')";

// UPDLOAD
$result = mysql_query($query) or die ('Error updating database');

if($results){
echo "Account createed Successfully, please login with your details.";
}

?>

 

The details are correct it just keeps giving me the error "'Error updating database".

The table exists in the database too.. its doing my head in.

Link to comment
Share on other sites

i'm not sure, but try this:

<?php 
// CONNECT TO THE DATABASE
mysql_connect ("localhost", "xxx", "xxx") or die ('Error: ' . mysql_error());
mysql_select_db ("xxx");

// CALL IN VARIABLES
$fname = $_POST['fname'];
$lname = $_POST['lname'];

// THE QUERY
$query = "INSERT INTO user ('fname' , 'lname') VALUES ($fname, $lname)";

// UPDLOAD
$result = mysql_query($query) or die ('Error updating database');

if($results){
    echo "Account createed Successfully, please login with your details.";
}

?>

Link to comment
Share on other sites

Are you sure that it isn't inserting into the database? - ie have you checked your tables directly?

 

You have a small error here:

if($results){
   echo "Account createed Successfully, please login with your details.";
}

You have defined the mysql_query as $result no $results so your message will not be shown.

 

Chris

Link to comment
Share on other sites

Hi again, i decided to reopen this thread up as not solved again instead of making another topic to fill up the forum.

 

i been adding the rest of my code. ie validation and what not and my code to see if the email address is already in use doesnt work. any ideas?

 

<?php 
// CONNECT TO THE DATABASE
mysql_connect ("localhost", "xxx", "xxx") or die ('Error: ' . mysql_error());
mysql_select_db ("xx");

// CALL IN VARIABLES
$username = $_POST['username'];
$password = $_POST['password'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$gamert = $_POST['gamert'];

// VALIDATE
if($fname == "" || $lname == ""){
echo("you didnt enter your full name");
exit();
}
if($password == ""){
echo("you didnt enter anything for your password, please try again");
exit();
}
if($email == ""){
echo("you didnt enter anything for your email, please try again");
exit();
}
if(!ereg("^.+@.+\\..+$", $email)){
echo("the email you entered was not valid, please try again");
exit();
}

// CHECK IF EMAIL EXISTS
$sql = "SELECT email FROM user WHERE email='$_POST[email]'";
$result = mysql_query($sql) or die('couldnt execute query'.mysql_error());
$num = mysql_num_rows($result);
if($num == 1){
echo("This email address is already in use, please use another email address");
exit();
}

// THE QUERY
$query = "INSERT INTO user (username,password,fname,lname,email,gamert) VALUES ('$username','$password','$fname','$lname','$email','$gamert')";

// UPDLOAD
$result2 = mysql_query($query) or die ('Error updating database'.mysql_error());

if($result2){
echo "Account createed Successfully, please login with your details. you will now be redirected..";
}

?>
<script type="text/javascript">
<!--
setTimeout('Redirect()',2000);
function Redirect()
{
location.href='index.php';
}
//-->
</script>

Link to comment
Share on other sites

You have this:

 

if($num == 1){

 

That is rather limiting as it only returns true if there is exactly one result, what if the email is in the db more than once?

 

You should use this

if($num <>0){

or

if($num >0){

 

Chris

 

ding ding we have a winner... damn cant believe that slipped by me lol i shouldnt cleaned out the database.. thats the reason it wasnt working at all. thanks your right ill use the more than expression. thanks! me so tired the little things get passed me. good job there are great people on this forum to lend a helping hand!

Link to comment
Share on other sites

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.