Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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&uuml;gbar : ' ;
echo $row["BSTSTLIO"];
echo '<br>' ;
echo '<br>' ;
echo 'Verkaufszahlen Aktuelles Jahr : ' ;
echo '<br>' ;
echo '<br>' ;
echo 'St&uuml;ck : ' ;
echo $row["BSTKUMST"];
echo '<br>' ;
echo 'Umsatz : ' ;
echo $row["BSTKUMVK"];  echo ' &euro;' ;
echo '<br>' ;
echo 'Ertrag : ' ;
echo $row["BSTKUMER"];  echo ' &euro;' ;
echo '<br>' ;
echo '<br>' ;   
echo "</div>" ;

echo $row["BSTKUMSTVJ"];
echo $row["BSTKUMVKVJ"];    echo ' &euro;' ;
echo $row["BSTKUMERVJ"];    echo ' &euro;' ;

}

?>

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

share|improve this question
    
That field doesn't exist for some reason. var_dump($row) inside of your loop to see what it contains. – aynber 3 hours ago
    
I tried to figure it out by looking at the dumps already. It throws out all the data it should when I test all different functions.. – Exelion 3 hours ago
    
Obviously, in your first query there is no such field selected. How on the earth you expect it to appear in the combined array? – Your Common Sense 3 hours ago
    
But, how do I select it? I thought it worked like that.. I need to read both tables at the same time.. – Exelion 3 hours ago
    
Like WHAT? a field from one table would mysteriously appear in the array selected from another table? And what value it should have? – Your Common Sense 3 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.