Jump to content

Recommended Posts

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"

[code=php:0]
<?

//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();
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/19406-insert-problem/
Share on other sites

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:

[code]$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);[/code]

Post the exact response this generates.
Link to comment
https://forums.phpfreaks.com/topic/19406-insert-problem/#findComment-84262
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.