0

I need to know if I can insert some data, into two database tables..

I have the following code, wich verify if user is logged on:

<?php
// Start the session (pretty important!)
session_start();

// Establish a link to the database
$dbLink = mysql_connect('localhost', 'DBUSER', 'PW');
if (!$dbLink) die('Can\'t establish a connection to the database: ' . mysql_error());

$dbSelected = mysql_select_db('DBNAME', $dbLink);
if (!$dbSelected) die ('We\'re connected, but can\'t use the table: ' . mysql_error());

$isUserLoggedIn = false;  
$query      = 'SELECT * FROM users WHERE session_id = "' . session_id() . '" LIMIT 1';  
$userResult     = mysql_query($query);  
if(mysql_num_rows($userResult) == 1){  
$_SESSION['user'] = mysql_fetch_assoc($userResult);  
$isUserLoggedIn = true;  
}else{  
if(basename($_SERVER['PHP_SELF']) != 'index.php'){  
    header('Location: index.php');  
    exit;  
}  
}  
?>

Database look's like:

Database table : users

---------------------------------------------------------------
| id | name | email | password | registered_date | session_id |
---------------------------------------------------------------

The HTML form is not necessary, because you know how it looks.

Now I have the following code, which insert some data to database table. Its different database table from upper code.

That its the php code:

<?php
include ('adresa-site.php');
if(isset($_POST['add']))
{
$dbhost = 'localhost';
$dbuser = 'DBUSER';
$dbpass = 'PW';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Eroare de conexiune: ' . mysql_error());
}

if(! get_magic_quotes_gpc() )
{
$link = $web_site.''.$_POST["stire"].'?idstire='.$_POST["idstire"];
$poza = addslashes ($_POST['poza']);
$nume = addslashes ($_POST['nume']);
$prenume = addslashes ($_POST['prenume']);
$varsta = addslashes ($_POST['varsta']);
$localitatea = addslashes ($_POST['localitatea']);
$numep = addslashes ($_POST['numep']);
$idstire = addslashes ($_POST['idstire']);
$stire = addslashes ($_POST['stire']);
$data = gmDate('Y-m-d H:i:s');
}
else
{
$link = $web_site."".$_POST['stire']."?idstire=".$_POST['idstire'];
$poza = htmlentities($_POST['poza'], ENT_QUOTES | ENT_HTML5);
$nume = htmlentities($_POST['nume'], ENT_QUOTES | ENT_HTML5);
$prenume = htmlentities($_POST['prenume'], ENT_QUOTES | ENT_HTML5);
$varsta = $_POST["varsta"];
$localitatea = htmlentities($_POST['localitatea'], ENT_QUOTES | ENT_HTML5);
$numep = htmlentities($_POST['numep'], ENT_QUOTES | ENT_HTML5);
$idstire = htmlentities($_POST['idstire'], ENT_QUOTES | ENT_HTML5);
$stire = $_POST["stire"];
$data = gmDate('Y-m-d H:i:s');
}


$sql = "INSERT INTO stiri2".
   "(link, poza, nume, prenume, varsta, localitatea, numep, idstire, stire, accesari,         data) ".
               "VALUES('$link','$poza','$nume','$prenume','$varsta','$localitatea','$numep','$idstire','$stire','$accesari', NOW())";
mysql_select_db('DBNAME');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Nu s-au putut adauga datele: ' . mysql_error());
}
echo "

<p class='text-center'>Copiaza si trimite link-ul de mai jos, prietenului tau</p>

<textarea rows='2' cols='55'>http://site.ro/" . $_POST['stire'] . "?idstire="     . $_POST['idstire'] . "</textarea>

<br />
<p class='text-center'>Acceseaza <a target='_blank'href='http://site.ro/" .     $_POST['stire'] . "?idstire=" . $_POST['idstire'] . "'>ACEST</a> link pentru     previzualizare</p>

";
mysql_close($conn);
}
?>

Database looks like:

Database table : stiri2

--------------------------------------------------------------------------------------------------------
| id | link | poza | nume | prenume | varsta | localitatea | numep | idstire | stire | data | accesari |
--------------------------------------------------------------------------------------------------------

Now user from users database table, want to insert new information into stiri2 database table.

I must get session_id from users database table, and update users and stiri2 database tables with inserted information.

I will create a little map:

User it's logged in, and his session_id is for example, 328234917238914.

Now he want to insert some new information, which will be stored in stiri2 database table.

Every user will add their information, so I must do something to not overlap the inserted information.

What I need to know, it's how to make insert for current user..

5
  • Why do you need to save the session_id in the database ? You can corelate the info with the user id. Commented Aug 29, 2013 at 10:31
  • it's not necesay session_id , but i need to know how to make insert for every user ! Commented Aug 29, 2013 at 10:34
  • nobody can help me with that problem? It's so complicated? :( .. for me is, but i think not for you guys ! Commented Aug 29, 2013 at 11:08
  • It's not complicated, but you need to read stackoverflow help pages and whathaveyoutried.com. After that, try and solve the issue yourself. After that, post a question that is more concise and generalist. Well, if you do that, you probably will find your answers before posting the question Commented Aug 29, 2013 at 12:17
  • Ok .. i will try to solve myself. i've hope if someone can help me, cuz im newbie on php. Commented Aug 29, 2013 at 12:26

1 Answer 1

1

What are you looking for is probably something like this: mysql_insert_id http://php.net/manual/de/function.mysql-insert-id.php

What you do is the following: A user registrates, you get that id and us this as your userID.

You can store this userid in the session.

If your user logs in, you will look up this id and use it in your other queries.

Sign up to request clarification or add additional context in comments.

4 Comments

Ty for your comment. i will read entire page from php.net . Plus 1
i've found something interesting .. <strong>Insert into stiri2 (Select * from users where id='CURENT ID')</strong> ..It is the corect one?
@MuzicaVeche you should retrieve your UserID while checking the credentials of your user
i've try that: $sql = "INSERT INTO stiri". "(link, poza, nume, prenume, varsta, localitatea, idstire, stire, accesari, data) ". "VALUES('$link','$poza','$nume','$prenume','$varsta','$localitatea','$idstire','$stire','$accesari', NOW())". "SELECT * FROM users WHERE id='".$_SESSION['id']."' LIMIT 1 "; , but not working.. Have you another solution?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.