Jump to content

PHP and MySQL connection wont work! :(


pappakaka

Recommended Posts

Ok, i really need help with my registration page for my website. I've been working on this for 2 days now and i can't seem to get it to work propertly. This problem is that whenever i'm trying to register myself as a new user at my localhost website i get "Could Not Process Form" error that i've put into the code myself. I beleive this has something to do with the connection to my MySQL but i've put in "membership" as the database "root" as the username and then my password. If anyone has the time to read through my coding, i have marked the line where the error message is placed in the code.. yeah see for your self. Really need help with this!

 

<?php

class Register
{
  private $username;
  private $firstname;
  private $lastname;
  private $password;
  private $passmd5;
  private $email;
  private $gender;
  private $birthday;

  private $errors;
  private $token;

  public function __construct()
  {
    $this->errors = array();

    $this->username  = $this->filter($_POST['username']);
$this->firstname = $this->filter($_POST['first_name']);
$this->lastname  = $this->filter($_POST['last_name']);
    $this->password  = $this->filter($_POST['password']);
    $this->email     = $this->filter($_POST['email']);
$this->gender    = $this->filter($_POST['gender']);
$this->birthday  = $this->filter($_POST['birth_day']);
    $this->token     = $_POST['token'];

    $this->passmd5   = md5($this->password);
  }

  public function process()
  {
    if($this->valid_token() && $this->valid_data())
         $this->register();

    return count($this->errors)? 0 : 1;
  }

  public function filter($var)
  {
    return preg_replace('/[^a-zA-Z0-9@.]/','',$var);
  }

  public function register()
  {
   mysql_connect("localhost","root","") or die(mysql_error());
   mysql_select_db("membership") or die (mysql_error());

   mysql_query("INSERT INTO users(username,password) VALUES ('{$this->username}','{$this->passmd5}')");

   if(mysql_affected_rows()< 1)
     $this->errors[] = 'Could Not Process Form';    <------------------ HERE IS THE ERROR MESSAGE I'VE PUT IN THE CODE TO SPIT OUT IF SOMETHING GOES WRONG
  }

  public function user_exists()
  {
    mysql_connect("localhost","root","") or die(mysql_error());
    mysql_select_db("membership") or die (mysql_error());

    $data = mysql_query("SELECT username FROM users WHERE username = '{$this->username}'");

    return mysql_num_rows($data) > 0 ? 1 : 0;
  }

  public function show_errors()
  {
    echo "<h3>Errors</h3>";

    foreach($this->errors as $key=>$value)
      echo $value."<br>";
  }

  public function valid_data()
  {
    if($this->user_exists())
      $this->errors[] = 'Username Already Taken';
    if(empty($this->username))
      $this->errors[] = 'Invalid Username';
if(empty($this->firstname))
      $this->errors[] = 'Invalid First Name';
if(empty($this->lastname))
      $this->errors[] = 'Invalid Last Name';
    if(empty($this->password))
      $this->errors[] = 'Invalid Password';
    if(empty($this->email) || !preg_match('/^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}$/',$this->email))
      $this->errors[] = 'Invalid Email';
if(empty($this->gender))
      $this->errors[] = 'Invalid Gender';
if(empty($this->birthday))
      $this->errors[] = 'Invalid Birthday';

  return count($this->errors)? 0 : 1;
  }


  public function valid_token()
  {
   if(!isset($_SESSION['token']) || $this->token != $_SESSION['token'])
     $this->errors[] = 'Invalid Submission';

   return count($this->errors)? 0 : 1;
  }
}

?>

Link to comment
Share on other sites

i suggest that you echo the query and see if it works via phpmyadmin, and also check to see whether it runs in php successfully with mysql_error();

 

$sql = "INSERT INTO users(username,password) VALUES ('{$this->username}','{$this->passmd5}')";
echo "sql: $sql <br />";
mysql_query($sql) or die(mysql_error());

 

Link to comment
Share on other sites

Ok i tried what you said and used this code:

  public function register()
  {
   mysql_connect("localhost","root","") or die(mysql_error());
   mysql_select_db("membership") or die (mysql_error());

   $sql = "INSERT INTO users(username,password) VALUES ('{$this->username}','{$this->passmd5}')";
echo "sql: $sql <br />";
mysql_query($sql) or die(mysql_error());

   if(mysql_affected_rows()< 1)
     $this->errors[] = 'Could Not Process Form';
  }

 

But then i get this message:

 

sql: INSERT INTO users(username,password) VALUES ('test','098f6bcd4621d373cade4e832627b4f6')

Field 'first_name' doesn't have a default value

 

What does it mean by "Field 'first_name' doesn't have a default value"? do i have to change it?

 

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.