i got alot of help in my other Question but my problem doesn't relate to said question anymore. I'm completely new to PDO but tried some stuff. I want to read from 2 different tables. It works on one side. On the other it throws me " Notice: Undefined index: BSTKUMSTVJ in C:\xampp\htdocs\test2.php on line 123 " and so on. I dont see where the problem is.. My currently used code :
<html>
<head>
<link rel="stylesheet" type="text/css" href="style2.css"/>
</head>
<body>
<?php include 'menu.php'; ?>
<br>
<form name="frmSearch" method="post" action="test2.php">
<table border="0">
<tr>
<th>Artikel :
<input name="var1" type="text" id="var1">
<input type="submit" value="Search"></th>
</tr>
</table>
</form>
<?php
$nameofdb = 'db';
$dbusername = 'root';
$dbpassword = '';
try {
$dbh = new PDO("mysql:dbname=$nameofdb;host=localhost", $dbusername, $dbpassword);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$var1 = str_replace(array('%','_'),'',$_POST['var1']);
if (!$var1)
{
exit('Invalid form value: '.$var1);
}
$query = 'SELECT
t1.`BSTKATNR`,
t1.`BSTLIEFBST`,
t1.`BSTLIEFTXT`,
t1.`BSTARTBES1`,
t1.`BSTANF`,
t1.`BSTSTLIO`,
t1.`BSTLIEFMIN`,
t1.`BSTMIND`,
t1.`BSTARTMASS`,
t1.`BSTKUMST`,
t1.`BSTKUMVK`,
t1.`BSTKUMER`
FROM `elebest` AS t1 WHERE
( t1.`BSTLIEFBST` LIKE :search ) OR
( t1.`BSTKATNR` LIKE :search ) OR
( t1.`BSTLIEFTXT` LIKE :search ) OR
( t1.`BSTARTBES1` LIKE :search)';
$secondquery = 'SELECT
t2.`BSTKATNR`,
t2.`BSTKUMSTVJ`,
t2.`BSTKUMVKVJ`,
t2.`BSTKUMERVJ`
FROM `vorjahr` AS t2 WHERE
( t2.`BSTKATNR` LIKE :search) OR
( t2.`BSTLIEFBST` LIKE :search) OR
( t2.`BSTLIEFTXT` LIKE :search)';
$stmt1 = $dbh->prepare($query);
$stmt2 = $dbh->prepare($secondquery);
$stmt1->bindValue(':search', '%' . $var1 . '%', PDO::PARAM_INT);
$stmt2->bindValue(':search', '%' . $var1 . '%', PDO::PARAM_INT);
$stmt1->execute();
$stmt2->execute();
print(" ");
$result1 = $stmt1->fetchAll();
$result2 = $stmt2->fetchAll();
foreach(array_merge($result1,$result2) as $row ) {
echo "<div id='uberschrift'>" ;
echo 'Artikelnummer : ' ;
echo $row["BSTKATNR"];
echo '<br>' ;
echo 'LF. Nr. : ' ;
echo $row["BSTLIEFBST"];
echo '<br>' ;
echo '<br>' ;
echo 'Beschreibung : ' ;
echo $row["BSTARTBES1"];
echo "</div>" ;
echo "<div id='links'>" ;
echo '<br>' ;
echo 'Bestand : ' ;
echo $row["BSTANF"];
echo '<br>' ;
echo 'Verfügbar : ' ;
echo $row["BSTSTLIO"];
echo '<br>' ;
echo '<br>' ;
echo 'Verkaufszahlen Aktuelles Jahr : ' ;
echo '<br>' ;
echo '<br>' ;
echo 'Stück : ' ;
echo $row["BSTKUMST"];
echo '<br>' ;
echo 'Umsatz : ' ;
echo $row["BSTKUMVK"]; echo ' €' ;
echo '<br>' ;
echo 'Ertrag : ' ;
echo $row["BSTKUMER"]; echo ' €' ;
echo '<br>' ;
echo '<br>' ;
echo "</div>" ;
echo $row["BSTKUMSTVJ"];
echo $row["BSTKUMVKVJ"]; echo ' €' ;
echo $row["BSTKUMERVJ"]; echo ' €' ;
}
?>
</body>
</html>
(Sorry, some text is in german)
Maybe it can be written easier, sadly I don't know how. Thank you for your help!
Looks like that
var_dump($row)
inside of your loop to see what it contains. – aynber 3 hours ago