du9826 2013-08-29 10:24
浏览 85

PHP插入两个不同的表

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..

  • 写回答

1条回答 默认 最新

  • dow56114 2013-08-29 12:32
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染