Hi
I am hoping someone can give me some advice.

I have a user_registration table with the field 'ID' as the user ID field.

I have another file (property_details) which is for users to insert their own records.

What I need to do is to automatically assign their ID from the user_registration table to the field in the property_details file. This is so each record can be associated with the correct owner.

Each page will already show who has logged on via

. $_SESSION ['username'] .

I have a feeling I need to do something like:

// Connect to database
$dbc=mysqli_connect ()
 // Grab the user-entered log-in data
      $user_username = mysqli_real_escape_string($dbc, trim($_POST['username']));
// Grab the Registered ID
$query=Select ID from user_registration where $username = $user_username

What I am stuck on is:
How to match the registered username and bring back the associated ID
How to automatically stick the returning registered ID from user_registration into my property_details.ID field. (<input name="ID" type="text" id="ID" size="32" />)

Any help would be really appreciate.:-)

Many thanks in advance

So this is the actual lines I'm following now:

// Connect to the database
    $dbc = mysqli_connect("localhost", "xxxxxxx", "xxxxxxxxx", "xxxxxxx");
	$data = "SELECT ID FROM user_registration WHERE username = $_SESSION['username']";
	$query = mysql_query($data) or die("Couldn't execute query. ". mysql_error());

Then on my actual form field I have the following:

value="<?php echo $data[ID]?>"

I am getting an error which I know is to do with the $_SESSION[username] - but this is where I'm not sure how to match the username in the user_registration table with the name of the user who is logged in and bring back their ID....

You can't include array variables in double-quote strings like that, this has to be done either of two ways:

// Connect to the database
    $dbc = mysqli_connect("localhost", "xxxxxxx", "xxxxxxxxx", "xxxxxxx");
	$data = "SELECT ID FROM user_registration WHERE username = '" .$_SESSION['username']. "'";
	$query = mysql_query($data) or die("Couldn't execute query. ". mysql_error());
// Connect to the database
    $dbc = mysqli_connect("localhost", "xxxxxxx", "xxxxxxxxx", "xxxxxxx");
	$data = "SELECT ID FROM user_registration WHERE username = '{$_SESSION['username']}'";
	$query = mysql_query($data) or die("Couldn't execute query. ". mysql_error());

And don't forget to escape them first of course.

Hi Thanks for the help. I'm not getting any syntax error now, but all it is bringing back into my field is the capital S

My actual field in user_registration is set out as follows:
ID bigint(20) UNSIGNED ZEROFILL No None AUTO_INCREMENT

Due to how I've set up my password reset with activation key etc the ID that gets entered looks like this:
00000000000000000001

So I would expect a 1 to be inserted into my propety_record.ID field and not S

Do you have any ideas why this may be happening?

Many thanks for the help, it is really appreciated.

Maybe it is because the table field is in integer, but is trying to convert the data into a text field in the form?

value="<?php echo $dataID]?>"

Maybe I need to somehow convert the ID to a string? But I wouldn't know where to do that?

Hi
Here is a modified version of what I'm trying to do:

<?php 
  // Start the session
  require_once('startsession.php');
  
  require_once('Connections/dreamsin_localhost.php');  

// Connect to the database    
	$dbc = mysqli_connect("localhost", "xxxxxxx", "xxxxxxx", "xxxxxxxx");
	
	$data = "SELECT ID FROM user_registration WHERE username = '" .$_SESSION['username']. "' AND ID = (int)ID ";
	
	$id = (int) $row['ID']; 	
	
	

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

Result Posted: <?php echo gettype('ID'); ?>
Result Posted: <?php echo $data[$ID]?>

</body>
</html>

The results I currently get are:

Result Posted: string Result Posted: S

The result I'm looking for is a number ....

I feel sure it is the convert back to integer that I need to do, but I dont'know where or how to set my ID.....

Many thanks liz

The query as I posted should be fine on itself, but in the code in your last post no query ever gets executed.

All you are doing right now is making a string $data which contains the query you want to send, and setting $id to the int-value of an undefined variable ($row doesn't exist in your code) which comes down to $id = 0.
So when echo-ing $data[$id] you should expect $data[0] to be echo'd which is the first character of the string $data which is an S.

Try understanding what this page reads under 2. Procedural style. It's about how to execute query's.

Yes thanks! :-) I realised I had left the query out. Even with the

query = mysql_query($data) or die("Couldn't execute query. ". mysql_error

I still return an S in $data(ID)

The only time I get the appropriate ID back is if I echo

<?php echo '(' . $_SESSION['ID'] . ')'?>

I just dont'know if that is the right way?

Again you are trying to get the value you want from the variable $data which does not contain the result of the query.

But

$query = mysql_query($data) or die("Couldn't execute query. ". mysql_error());
$row = mysql_fetch_array($query);
echo $row['ID'];

should work.

What this does is:
mysql_query: execute the given query on the database; returns a MySQL resource variable from which you can extract the actual result of the query.

mysql_fetch_array: read the next row (or false if there aren't any left) from the query-result given as an argument. This has to be a MySQL resource like the above function returns.
This returns an array containing the fields of the result for one row and since you'll never get more than one row you only have to call it once.

Now the $row should contain what you are looking for.

Yes your right! Thanks

This is what I've done:

mysql_select_db($database_dreamsin_localhost, $dreamsin_localhost);
$query_rs_id = "SELECT ID FROM user_registration  WHERE username = '" .$_SESSION['username']. "' ";
$rs_id = mysql_query($query_rs_id, $dreamsin_localhost) or die(mysql_error());
$row_rs_id = mysql_fetch_assoc($rs_id);
$totalRows_rs_id = mysql_num_rows($rs_id);

And then in my field called :

value="<?php echo $row_rs_id['ID']; ?>"

Many thanks for the help!

Liz

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.