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 it's not necesary, cuz u know how it looks//
Now i have the following code, wich 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 look's 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 informations into stiri2 database table..
I must get session_id from users database table, and update users and stiri2 database tables with inserted informations..
I will create a litlle map:
User it's logged in, and his session_id is for example, 328234917238914 .
Now he want to insert some new informations, wich will be stored in stiri2 database table.
Every user will add their informations, so i must do something to not overlap the inserted informations..
What i need to know, it's how to make insert for curent user...
I hope to understand something, and if someone can help me i'll remain grateful. Thank you..
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.