Jump to content

[SOLVED] Some misunderstandings within my code?


Twister1004

Recommended Posts

Hey guys, I really like this forum, seems like it will be a lot of help to me, as a new coder.

 

However, I am getting an error within my script. Before I show you the script, I would like to tell you what I am trying to do.

 

Objective: I am trying to get the person registered as like a normal script, however, as well when they register, I am making a profile page for them as well. (Still coding it so it's not working.) So what I am trying to do is get the person registered, then get the information just entered to go into another table along with their account id.

 

My problem: While I do this, I am getting an "Incorrect integer value: '' for column 'accountid' at row 1"

accountid is a column within the table accounts. There is also another spot in the table profile for accountid as well.

 

Conclusion: So basically this should make more since. I'm trying to take the accountid from the account newly created row from the table accounts to the table profile.

 

Script:

<?php error_reporting(E_ALL);?>
<html>
<head>
<title>Abysmal Essence | Register - Complete</title>
</head>
<body>
<?php

$user = $_POST['user'];
$name = $_POST['name'];
$pass = $_POST['pass'];
$vpass = $_POST['vpass'];
$email = $_POST['email'];
$dob = $_POST['dob'];


include ("config.php");

$sel = 'SELECT * FROM `accounts` WHERE `username`="' . $_POST['user'] . '"';

//Andy edited here
$result = mysql_query($sel) or die(mysql_error());
$data = mysql_fetch_assoc($result);

if(mysql_num_rows($result) >0)
{
//echo "Username exitsts<br>";
}else{
//echo "Username Aviable<br>";
}




if ($user == "")
{
echo 'Please enter a username.';
exit();
}
elseif (mysql_num_rows(mysql_query($sel)) >= 1)
{
echo 'The username already exists. Please choose another name.<br>';
exit();
}
elseif ($pass == "")
{
echo 'You did not enter a password. Please go back and enter a password.<br>';
exit();
}
elseif ($vpass != $pass)
{
echo 'Your passwords do not match. Please re-enter your password.<br>';
exit();
}
else
{
$con = 'INSERT INTO `accounts` (username, name, password, email, dob) VALUES ("' .
	$user . '", "' . $name . '", "' . $pass . '", "' . $email . '", "' . $dob . '")';
$accountid = mysql_query($account['accountid']);
$account = "SELECT * FROM `accounts` WHERE `accountid` = '$accountid'";
$con2 = 'INSERT INTO `profile` (accountid, about, content, unknown) VALUES ( "'.$accountid.'","Edit me", "Edit me", "Edit me")';
mysql_query($con2) or die(mysql_error());
mysql_query($con) or die(mysql_error());
echo '<center>You have successfully created your account. You can now log in.</center>';
}
?>
<br/><br/>		Your account has been created.<br/> You will be e-mailed on your Login information. Please note: We have to manually send E-mails.<br/>If you are within the team for development, you will be sent an extra ID along with your login information.<br/> More info will be told when E-mails are received.<br/>

<input type='button' value='Home' onclick='parent.window.location.href = "./"' />
</body>
</html>

 

Now the script I added into this would be this section:

else
{
$con = 'INSERT INTO `accounts` (username, name, password, email, dob) VALUES ("' .
	$user . '", "' . $name . '", "' . $pass . '", "' . $email . '", "' . $dob . '")';
$accountid = mysql_query($account['accountid']);
$account = "SELECT * FROM `accounts` WHERE `accountid` = '$accountid'";
$con2 = 'INSERT INTO `profile` (accountid, about, content, unknown) VALUES ( "'.$accountid.'","Edit me", "Edit me", "Edit me")';
mysql_query($con2) or die(mysql_error());
mysql_query($con) or die(mysql_error());
echo '<center>You have successfully created your account. You can now log in.</center>';
}

 

Any advice or help?

 

Thank you very much for reading / posting.

Link to comment
Share on other sites

Now the script I added into this would be this section:

else
{
$con = 'INSERT INTO `accounts` (username, name, password, email, dob) VALUES ("' .
	$user . '", "' . $name . '", "' . $pass . '", "' . $email . '", "' . $dob . '")';
$accountid = mysql_query($account['accountid']);
$account = "SELECT * FROM `accounts` WHERE `accountid` = '$accountid'";
$con2 = 'INSERT INTO `profile` (accountid, about, content, unknown) VALUES ( "'.$accountid.'","Edit me", "Edit me", "Edit me")';
mysql_query($con2) or die(mysql_error());
mysql_query($con) or die(mysql_error());
echo '<center>You have successfully created your account. You can now log in.</center>';
}

 

Any advice or help?

 

Thank you very much for reading / posting.

Well there are a few problems with that code.

 

The first

$con = 'INSERT INTO `accounts` (username, name, password, email, dob) VALUES ("' .
$user . '", "' . $name . '", "' . $pass . '", "' . $email . '", "' . $dob . '")';
$accountid = mysql_query($account['accountid']);
$account = "SELECT * FROM `accounts` WHERE `accountid` = '$accountid'";

 

mysql_query returns a result resource and nothing else. result resources cannot be used within queries. In order to get the data returned from a query you'll need to use one of the following functions:

mysql_fetch_(array, assoc or row) or mysql_result

 

You should use the following code.

    // define the query for inserting a new account
    $sql = 'INSERT INTO `accounts` (username, name, password, email, dob) VALUES '.
           '("' . $user . '", "' . $name . '", "' . $pass . '", "' . $email . '", "' . $dob . '")';

    // create a new account
    $result = mysql_query($sql);

    // check that the query executed successfully
    if($result)
    {
        // now that a new account has been added we'll retrieve the accountid
        $accountid = mysql_insert_id(); // if you're not sure what this function does go to http://php.net/mysql_insert_id

        // now create a new profile
        $sql = 'INSERT INTO `profile` (accountid, about, content, unknown) VALUES ( "'.$accountid.'","Edit me", "Edit me", "Edit me")';
        $result = mysql_query($sql);

        if($result)
        {
           echo '<center>You have successfully created your account. You can now log in.</center>';
        }
        // unable to create  account profile, kill the script and display an error
        else
            die('Unable to create account profile!<br />MySQL Error: '.mysql_error());
    }
    // unable to create new account kill, the script and display an error
    else
        die('Unable to create a new account!<br />MySQL Error: '.mysql_error());

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.