Jump to content

INSERT problem


whoopass

Recommended Posts

OKay, I'll try to explain this as best I can.

 

I have a login system.  When a user registers and then logs in, he/she is shown a web page.  On this web page I have a form where I want the user to enter data.  What function or command would I use to ensure that the data entered coincides with the active user?  i.e.  I want my table to reflect the username and the data entered.  Such as username, data1, data2, data3.

 

I am able to insert the data but I don't know how to enter the active users username with the data.

 

Hope this makes sense.

 

Thanks for any help.

Link to comment
Share on other sites

Yes i do want to insert session values.  The value being the username of the active user and also insert the data he enters on the form.  So when the submit button is pressed, I want the username of the active user and the data on the same table.

 

Not sure how to insert the user with the data.

Link to comment
Share on other sites

Quick little script, to generalize the idea.

<?php

// do some kind of test to see if user is logged in
if($session->logged_in){

// test if the $_POST values have been set on the form
if(isset($_POST['textarea'])) {

// set your variables			
$text = $_POST['text'];
$username = $_SESSION['username'];

// if not set, make sure they know	
if(!isset($_POST['textarea'])) {
echo "Fill in the form please.";
}

// SQL query to insert the data
$sql="INSERT INTO 'table' VALUES (' ', '$text', '$username')";

// if cant connect, present error, or die();
if (!@mysql_query($sql)) {
echo 'Error adding query: ' . mysql_error();
}
}
}

?>

 

Look that over, and see if it helps your situation.

Link to comment
Share on other sites

Okay I was able to pass the active user to the table.  Thank you.

 

Do you know why I cannot overwrite the data using the INSERT function when entering new data?  The old data won't change.  If the user wants to change the data it doesn't work.

 

If I use UPDATE, only one row is created.  No new rows are made. 

 

Example.  Marty logs in, enters his data.  The database is updated.

 

Billy logs in, enters his data.  Only Billy's data is there.  Marty's data is overwritten with Billy's.

 

Using UPDATE 'table' SET variable = '$variable', etc etc.

 

Thanks again.

Link to comment
Share on other sites

Okay, I think I have it now.  I was using two different tables.  Instead, I merged my data table into my users table and by doing this I was able to update the users data using the UPDATE statement and WHERE username = $username.  Because the users must register first thereby setting their information before the data needed to be entered.

 

Thanks.

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.