Jump to content


Photo

INSERT problem


  • Please log in to reply
4 replies to this topic

#1 anthonydamasco

anthonydamasco
  • Members
  • PipPipPip
  • Advanced Member
  • 92 posts

Posted 01 September 2006 - 08:11 PM

I'm having trouble inserting information into my database, could someone have a look at my code and help me debug it?

i get "unable to insert into database"

<?

//connect info was edited out

//select the database
$db = mysql_select_db("accu") or die( "Unable to select database");
$username="";
$email="";
$firstname="";
$lastname="";


$username = $_POST['username'];
$email = $_POST['email_address'];
$firstname = $_POST['first_name'];
$lastname = $_POST['last_name'];
$companyname = $_POST['comp_name'];
$phone = $_POST['phone'];


/* Do some error checking on the form posted fields */

if((!$firstname) || (!$lastname) || (!$email) || (!$username)){
    echo 'You did not submit the following required information! <br />';
    if(!$firstname){
        echo "First Name is a required field. Please enter it below.<br />";
    }
    if(!$lastname){
        echo "Last Name is a required field. Please enter it below.<br />";
    }
    if(!$email){
        echo "Email Address is a required field. Please enter it below.<br />";
    }
    if(!$username){
        echo "Desired Username is a required field. Please enter it below.<br />";
    }
       exit(); // if the error checking has failed, we'll exit the script!
} 


function makeRandomPassword() {
  $salt = "abchefghjkmnpqrstuvwxyz0123456789";
  srand((double)microtime()*1000000); 
      $i = 0;
      while ($i <= 7) {
            $num = rand() % 33;
            $tmp = substr($salt, $num, 1);
            $pass = $num . $tmp;
            $i++;
      }
      return $pass;
}

$random_password = makeRandomPassword();

$db_password = md5($random_password); 

/* checking and ensure that the user's email address or username
 does not exist in the database */
 
 $sql_email_check = mysql_query("SELECT email FROM staff WHERE email='$email'");
 $sql_username_check = mysql_query("SELECT username FROM staff WHERE username='$username'");
 
 $email_check = mysql_num_rows($sql_email_check);
 $username_check = mysql_num_rows($sql_username_check);
 
 if(($email_check > 0) || ($username_check > 0)){
     echo "Please fix the following errors: <br />";
     if($email_check > 0){
         echo "<strong>Your email address has already been used by another member 
         in our database. Please submit a different Email address!<br />";
         unset($email_address);
     }
     if($username_check > 0){
         echo "The username you have selected has already been used by another member 
         in our database. Please choose a different Username!<br />";
         unset($username);
     }
     exit();  // exit the script so that we do not create this account!
 } 

//insert the values
$sql = "INSERT INTO staff VALUES (NULL, '$firstname', '$random_password', '$lastname', '$username', '$companyname', '$email', 'SYSDATE()')";
mysql_query($sql) or die ( "Unable to insert into database");


if(!$sql){
    echo 'There has been an error creating your account. Please contact the webmaster.';
} else {
    $userid = mysql_insert_id();
    // Let's mail the user!
    $subject = "Your Membership at AccuStaffing!";
    $message = "Dear $firstname $lastname,
    Thank you for registering at our website, http://www.accustaffing.com!
    
    You are two steps away from logging in and accessing our exclusive members area.
    
    To activate your membership, please click here: 
    http://www2.accustaffing.com/activate.php?id=$userid&code=$db_password
    
    Once you activate your memebership, you will be able to login with the following 
    information:
    Username: $username
    Password: $random_password
    
    Thanks!
    The Webmaster
    
    This is an automated response, please do not reply!";
    
    mail($email, $subject, $message, 
        "From: Accustaffing Webmaster<anthony.damasco@accustaffing.com>\n
        X-Mailer: PHP/" . phpversion());
    echo 'Your membership information has been mailed to your email address!
     Please check it and follow the directions!';
	 include 'thankyou.html';
}


mysql_close();
?>


#2 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 01 September 2006 - 08:24 PM

what is $en() ?
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#3 anthonydamasco

anthonydamasco
  • Members
  • PipPipPip
  • Advanced Member
  • 92 posts

Posted 01 September 2006 - 08:30 PM

where do u see that?

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 01 September 2006 - 08:52 PM

Change the error messaging so it generates a useful message for debugging. What you have at present is like an error message that says "This failed because it failed". Try this instead:

$sql = "INSERT INTO staff VALUES (NULL, '$firstname', '$random_password', '$lastname', '$username', '$companyname', '$email', 'SYSDATE()')";
mysql_query($sql) or die ( "Error: ". mysql_error(). " with query ". $sql);

Post the exact response this generates.
Legend has it that reading the manual never killed anyone.
My site

#5 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 01 September 2006 - 09:04 PM

try to change NULL to '' (empty string)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users