|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dechim
Senior Member

Data înscrierii: 10/Mai/2005
Mesaje: 1172
Locație: Drobeta Turnu Severin

|
Iata un exemplu functional, cred eu, destul de didactic.
Toata treaba se face intr-un fisier cu extensia
php
Daca modifici datele conexiunii la MySQL ar trebui sa functioneze fara alte modificari.
code:
<?php
// verificam daca utilizatorul a facut o alegere
if (isset($_POST['submit']))
{
$server = 'localhost'; // serverul tau
$user = 'root'; // utilizatorul MySQL
$pass = ''; // parola MySQL
$db = 'test'; // baza de date
$tb = 'tabela'; // tabela in care vei cauta ID-ul
// conectare la serverul MySQL
$oConn = @mysql_connect($server, $user, $pass) or formDie("Eroare de conectare la serverul MySQL : ".mysql_error());
// creeaza baza de date daca nu exista
mysql_query("CREATE DATABASE IF NOT EXISTS ".$db.";",$oConn) or formDie("Eroare la creearea bazei de date : ".$db." - ".mysql_error());
// selecteaza baza de date
mysql_selectdb($db,$oConn) or formDie("Nu pot sa folosesc baza de date : <b>".$db."</b> - ".mysql_error());
// se creeaza tabela de test daca nu exista
$sql = "CREATE TABLE IF NOT EXISTS `".$tb."` (
`id` int(10) NOT NULL auto_increment,
`info` VARCHAR(30) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=ascii AUTO_INCREMENT=3 ;";
$result = mysql_query($sql,$oConn) or die('Nu pot sa creez tabela '.$tb.'('.mysql_error().')');
// daca tabela este goala o populam cu date de test
$result = mysql_query('SELECT count(id) as nr FROM `'.$tb.'`;',$oConn) or die('Nu pot sa citesc tabela <b>'.$tb.'</b>('.mysql_error().' )');
$row = mysql_fetch_assoc($result) or formDie("Ooops! Eroare neasteptata!");
$maxrec = (integer) $row['nr'];
if ($maxrec==0)
{
$sql = "INSERT INTO `".$tb."` (`id`, `info`) VALUES (1,'ID=1'),(2,'ID=2'),(3,'ID=3'),(4,'ID=4'),(5,'ID=5');";
$result = mysql_query($sql,$oConn) or die('Nu pot sa inserez datele in tabela <b>'.$tb.'</b>('.mysql_error().' )');
$maxrec = 5;
}
// cu id-ul selectat formam interogarea si retinem raspunsul in $mess pentru afisare
$id = (integer) $_POST['s1'];
$sql = "SELECT * FROM `".$tb."` WHERE id=".$id.";";
$result = mysql_query($sql,$oConn) or die('Nu pot sa citest datele din tabela <b>'.$tb.'</b>('.mysql_error().' )');
$mess = "In baza de date sunt ".$maxrec." inregistrari. Dvs. ati selectat :";
while ($row=mysql_fetch_assoc($result))
{
$mess .= $row['info']."<br />";
}
mysql_close($oConn);
}
else
{
$mess = "Selectati o optiune si apasati butonul";
}
?>
<html>
<head>
<script type="text/javascript">
function show()
{
var ss= document.getElementById('s1');
var len= ss.options.length;
var str="";
var j=0;
for(var i=0;i<len;i++){
if(ss.options[i].selected == true)
{
if(j == 0)
str= ss.options[i].innerHTML;
else
str += '|' + ss.options[i].value;
j++;
}
}
var echoDiv = document.getElementById('id_echo');
echoDiv.innerHTML = str;
}
</script>
</head>
<body>
<form name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
<select id="s1" name="s1" size=6 multiple onchange="show()" >
<option value="1" selected> test1 </option>
<option value="2"> test2 </option>
<option value="3"> test3 </option>
<option value="4"> test4 </option>
<option value="5"> test5 </option>
</select>
<div id="id_echo">1</div>
<input type="submit" name="submit" value=" OK " />
</form>
<h3><?php echo $mess; ?> </h3>
</body>
</html>
|
Sâm Iul 24, 2010 2:47 am |
|
|
|
|
PHPRomania Bot
Bot Member
Data înscrierii: 27/Dec/2007
Mesaje: 1
Locaţie: Server Google

|
|
Mie Dec 26, 2007 7:01 pm |
|
|
|
Reguli forum:
Nu puteți crea un subiect nou în acest forum Nu puteți răspunde în subiectele acestui forum Nu puteți modifica mesajele proprii din acest forum Nu puteți șterge mesajele proprii din acest forum Nu puteți vota în chestionarele din acest forum
|
|
|
|
|
< Subiectul anterior | Urmatorul subiect
>
|
|