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..
session_idin the database ? You can corelate the info with the user id.