ho due tabelle:
codice:
mysql> desc utenti;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| nome | varchar(20) | NO | MUL | NULL | |
| password | varchar(20) | NO | | NULL | |
| email | varchar(20) | NO | | NULL | |
+----------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> desc articoli;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(20) | NO | | NULL | |
| sectionid | varchar(10) | NO | | NULL | |
| testo | text | NO | | NULL | |
| data | datetime | NO | | NULL | |
| datamod | datetime | YES | | NULL | |
| utente | varchar(20) | NO | MUL | NULL | |
+-----------+-------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
ho creato una foreign key: articoli.utente è collegato a utenti.nome.
solo che quando vado a inserire gli articoli in php devo inserire anche il nome utente a mano.
come faccio a fargli inserire automaticamente il nome dell'utente connesso??
codice php:
Codice PHP:
<?php
if (isset($_POST['inserisci'])) {
include 'config.php';
$link = mysql_connect($host, $user, $password) or die("Non è possibile connettersi al server
");
$conn = mysql_select_db($db, $link) or die("Non è possibile connettersi al db
");
$title = mysql_real_escape_string($_POST['title']);
$sectionid = mysql_real_escape_string($_POST['sectionid']);
$testo = mysql_real_escape_string($_POST['testo']);
$insert = "INSERT INTO articoli (title, sectionid, testo, data, datamod) VALUES ('" . $title . "', '" . $sectionid . "', '" . $testo . "', sysdate(), sysdate())";
if ($_POST['title'] == NULL || $_POST['sectionid'] == NULL || $_POST['testo'] == NULL) {
echo "dati mancanti";
} else {
mysql_query($insert) or die(mysql_error($link));
header("Location: index.php");
}
}
?>