foreach invalid argument supplied and mysql fetch array issue - php

i have this code which i use to print some fields from the database.
My problem is that i get this error about foreach invalid argument supplied and a mysql fetch array problem.
The code is this:
foreach( $checked1 as $key => $value){
echo "<th> $value </th>";
}
echo "</tr></thead>";
while($row = mysql_fetch_array($result)){
Where $checked1 is an array
$checked1 = $_POST['checkbox'];
What's the problem here?
The whole code:
<?php
echo "<div id='table-3'>";
if(isset($_POST['Submit'])) {
echo "<pre>";
$checked1 = $_POST['checkbox'];
$checked = implode(',', $_POST['checkbox']);
}
$con = mysql_connect('localhost','user','passwd');
mysql_query("SET NAMES UTF8");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db_name", $con);
$result = mysql_query("SELECT $checked FROM hostess");
echo "<table >";
echo "<thead><tr>";
if(is_array($_POST['checkbox'])){
foreach( $checked1 as $key => $value){
echo "<th> $value </th>";
}
echo "</tr></thead>";
} else {
echo "Checkbox is not an array.";
}
while($row = mysql_fetch_array($result)){
echo "<tr>";
foreach($checked1 as $key => $value){
if($value == 'photo'){
echo "<td> <img src=foto/photo1/".$row[$value] . "></td>";
} else if($value == 'photo2'){
echo "<td><img src=foto/photo2/".$row[$value] . "></td></td>";
}
else if( $value == 'photo2' && $value == 'photo'){
echo "<td> <img src=foto/photo1/".$row[$value] . "></td>";
echo "<td><img src=foto/photo2/".$row[$value] . "></td></td>";
}
else{
echo "<td>" . $row[$value] . "</td>";
}
}echo "</tr>";
}
echo "</table>";

Either $_POST['checkbox'] is not defined (no checkbox checked while sending form) or is not an array (input name does not contain [] in the end);
You should always check if variable contain what you want before performing any operation on it, ex:
if(is_array($checked1)){
foreach( ... ){
}
}
$result = mysql_query(' ... ');
if(!$result){
die(mysql_error());
}

What is your $_POST['checkbox'] called in your HTML form? It should be something like:
<input type="checkbox" name="checkbox[]" value="1" />
Check to make sure your checkbox values are coming through as arrays. Do the following:
if(is_array($_POST['checkbox'])){
// contiue with foreach...
} else {
echo "Checkbox is not an array.";
}

Related

showing results obtained after $_POST in a new page

I have been trying to work this out for a while.
I have created a form with a dropdown box that gets results from a database. from this i then $_POST that from to another page. From that second page i wish to get the ID number and then get the records and display them on screen.
I will then put them in a table to organise the results better.
can anyone help me in achieving this.
Here is the code for the form (which works and sends the $PlantID)
$sql = "SELECT DISTINCT * FROM PLANTS";
$result = mysqli_query($mysqli,$sql)or die(mysqli_error());
//********************* Botannical name drop down box
echo "<form name='selection' id='selection' action='profile.php' method='post'>";
echo "<select name='flower'>";
while($row = mysqli_fetch_array($result)) {
$plantid = $row['FlowerID'];
$plantname = $row['Botannical_Name'];
$plantcommon = $row['Common_Name'];
/* $plantheight = $row['Height'];
$plantav = $row['AV'];
$plantcolours = $row['Colours'];
$plantflowering = $row['Flower_Time'];
$plantspecial = $row['Special_Conditions'];
$plantfrost = $row['Frost_Hardy'];
$plantaspect = $row['Aspect'];
$plantspeed = $row['Growth_Speed'];*/
echo "<option value=".$plantid.">".$plantname." -> AKA -> ".$plantcommon."</option>";
}
echo "</select>";
echo "<br />";
//********************* End of form
echo "<input type='submit' name='submit' value='Submit'/>";
echo "</form>";
I have created this page to get the ID and display that ID on screen. AS you can tell i have probably doubled up on ways to try work this out.
$sql = "SELECT * FROM PLANTS";
$result = mysqli_query($mysqli,$sql)or die(mysqli_error());
if(isset($_POST['submit'])){
$selected_val = $_POST['Botannical_Name']; // Storing Selected Value In Variable
echo "You have selected :" .$selected_val; // Displaying Selected Value
}
echo "<br />";
echo "well:".$_POST["Botannical_Name"]."<br/>";
echo "now:".$plantquery."<br />";
echo $_POST;
echo "<table>";
foreach ($_POST as $key => $value) {
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
echo "</table>";
Any help would be greatly appreciated.
you should use following to get the selected value,
$selected_val = $_POST['flower'];
if(isset($_POST['submit'])){
$selected_val = $_POST['flower']; // Storing Selected Value In Variable
echo "You have selected :" .$selected_val; // Displaying Selected Value
$sql = "SELECT * FROM PLANTS WHERE FlowerID='.$selected_val.'";
$result = mysqli_query($mysqli,$sql)or die(mysqli_error());
while ($row=mysqli_fetch_assoc($result))
{
echo $row['Botannical_Name'];
}
}
echo "<br />";
print_r($_POST);
if(!empty(_POST)) {
echo "<table>";
foreach ($_POST as $key => $value) {
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
echo "</table>";
}

Decrypt data from MySQL database

I have the below code to show all data from a MySQL database in a HTMl database:
<?php
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>";
$i = 0;
while($row = $result->fetch_assoc())
{
if ($i == 0) {
$i++;
echo "<tr>";
foreach ($row as $key => $value) {
echo "<th>" . $key . "</th>";
}
echo "</tr>";
}
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
This code works fine and the data is displayed in the table correctly, my problem is that most of the data in the DB is encrypted (simply)!
For example, here is how someones first name is stored 5cGs+mhdNN/SnHdqEbMk6zlKRu8uW7pR1zRjQHV9DGQ=
Is there a way to decrypt the data before displaying it in the table?
I usually decrpyt the date in the following way:
$name_firstD = simple_decrypt($name_first , "secured");
You need to have an array of columns which are encrypted.
<?php
$result = mysqli_query($con,"SELECT * FROM Persons");
$encrypted_columns = array('password','code', 'first_name');
echo "<table border='1'>";
$i = 0;
while($row = $result->fetch_assoc())
{
if ($i == 0) {
$i++;
echo "<tr>";
foreach ($row as $key => $value) {
echo "<th>" . $key . "</th>";
}
echo "</tr>";
}
echo "<tr>";
foreach ($row as $key => $value) {
echo "<td>" . (in_array($key,$encrypted_columns))? simple_decrypt($value , "secured") : $value . "</td>";
}
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
put the name of your encrypted column inside $encrypted_columns array.

Database query info to dropdown menu

I am taking info from a database query and adding it to a dropdown menu form (that is placed inside a table). The query is in a separate function that is called from within the form. It adds the info from the database to the correct location on the table, but it is not in the dropdown menu. I used variables $a, $b, and $c to test my syntax, and it works fine with those variables. Is it an issue with the function call? Any ideas?
Here is the code:
<?php
function fill_dropdown(){
include("../secure/database.php");
$conn = pg_connect(HOST." ".DBNAME." ".USERNAME." ".PASSWORD)
or die('Could not connect: ' . pg_last_error()); //error if could not connect to database
$query = "SELECT country_code, name FROM lab5.country ORDER BY name ASC";
$result = pg_query($query) or die("Unable to execute: " . pg_last_error($conn));
$numRow = 0;
//results are good so output them to HTML
//echo "test<br />";
while ($line1 = pg_fetch_array($result, null, PGSQL_ASSOC)){
$counter = 0;
//echo "test<br />";
foreach ($line1 as $col_value){ // then add all data for attributes in succeeding columns
if($counter == 0){
$code[$numRow] = $col_value;//array($numRow => $col_value);
echo "\t\t<input type=\"hidden\" name=\"code\" value=\"$code[$numRow]\" />";
//echo $code[$numRow] . "<br />";
}
elseif($counter == 1){
$country_name[$numRow] = $col_value;
echo "<option value=$country_name[$numRow]>$country_name[$numRow]</option>";
//echo $country_name[$numRow] . "<br />";
}
$counter++;
}
$numRow++;
}
//echo "end test<br />";
}
echo "<table border = \"1\">";
echo "<form method=\"POST\" action=\"exec.php\">"; //save and cancel buttons
for($i=1; $i<5; $i++) //building initial table
{
echo "\t<tr>\n";
echo "\t\t<td>";
if($i == 1)
echo "Name";
elseif($i == 2)
echo "Country Code";
elseif($i == 3)
echo "District";
else
echo "Population";
echo "</td>\n";
echo "<td>\n";
if($i == 1){
echo "<input type=\"text\" name=\"name\">";
}
elseif($i == 2){
echo "<select name=\"country_code\">"; //dropdown box
$c = 0; //these are just to show that this way works
$a = "IOT";
$b = "test2";
$numRow = 1;
echo "<option value=\"IOT\">British Indian Ocean Territory</option>";
echo "<option value=$a>$b</option>";
//echo "<option value=" . $country_name[$numRow] . ">" . $country_name[$numRow] . "</option>";
fill_dropdown();
//echo "<option value=\"Brunei\">Brunei</option>";
echo "</select>";
}
elseif($i == 3){
echo "<input type=\"text\" name=\"district\">";
}
else{
echo "<input type=\"text\" name=\"population\">";
}
}
echo "</td>";
echo "</tr>";
echo "</table>";
echo "\t\t<input type=\"submit\" value=\"Save\" name=\"save\" />";
echo "<input type=\"button\" value=\"Cancel\" onclick=\"top.location.href='" . $_SERVER['HTTP_REFERER'] . "';\" />\n";
echo "</form>";
?>
It would seem you can replace
while ($line1 = pg_fetch_array($result, null, PGSQL_ASSOC)){
$counter = 0;
//echo "test<br />";
foreach ($line1 as $col_value){ // then add all data for attributes in succeeding columns
if($counter == 0){
$code[$numRow] = $col_value;//array($numRow => $col_value);
echo "\t\t<input type=\"hidden\" name=\"code\" value=\"$code[$numRow]\" />";
//echo $code[$numRow] . "<br />";
}
elseif($counter == 1){
$country_name[$numRow] = $col_value;
echo "<option value=$country_name[$numRow]>$country_name[$numRow]</option>";
//echo $country_name[$numRow] . "<br />";
}
$counter++;
}
$numRow++;
}
with
while ($row = pg_fetch_assoc($result)) {
// why do you want this line at all?
echo "\t\t<input type=\"hidden\" name=\"code\" value=\"$row[country_code]\"/>";
echo "<option value=\"$row[name]\">$row[name]</option>";
}
The only thing I can see that is wrong, is not having quotes around the option's value attribute. I don't understand what you are expecting to achieve by interleaving hidden inputs with options, though. Judging by your $a, $b, $c template, maybe what you actually want is:
while ($row = pg_fetch_assoc($result)) {
echo "<option value=\"$row[country_code]\">$row[name]</option>";
}

PHP - Adding a separator in a Loop

I asked this question yesterday but no one replied after I tried to get further help. I've been trying to figure out the problem in the code I was given in my previous post but it's not giving me the results I want. Right now what it does is it writes a separator after each row but what I really want is if the name of the manager from the previous row is different then write a separator and keep going on with loop.
while ($row = mysqli_fetch_array($results)) {
$team = $row[0]['Manager'];
foreach($row as $rows){
if($rows['Manager'] !== $team){
echo "<tr><td colspan=\"3\">Separator</td></tr>";
}
echo "<tr>";
echo "<td>".$row['Manager']."</td>";
echo "<td>".$row['Employee_ID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "</tr>";
}
}
You're mixing $row and $rows, you would be okay doing this:
$team = "";
while ($row = mysqli_fetch_array($results)) {
if($team && ($row['Manager'] != $team)){ // skip first separator
echo "<tr><td colspan=\"3\">Separator</td></tr>";
}
$team = $row['Manager'];
echo "<tr>";
echo "<td>".$row['Manager']."</td>";
echo "<td>".$row['Employee_ID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "</tr>";
}
Am I missing something, or can you simply display the data, then assign it to the variable further in the loop?
while ($row = mysqli_fetch_array($results)) {
$team = $row[0]['Manager'];
foreach($row as $rows){
if($row['Manager'] !== $team){
echo "<tr><td colspan=\"3\">".$team."</td></tr>";
}
echo "<tr>";
echo "<td>".$row['Manager']."</td>";
echo "<td>".$row['Employee_ID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "</tr>";
$team = $row[0]['Manager'];
}
}
What is $rowS['Manager'] ? $rowS is unknown. use $row['Manager'] like:
if($row['Manager'] !== $team){
echo "<tr><td colspan=\"3\">Separator</td></tr>";
}
while ($row = mysqli_fetch_array($results)) {
$team = $row[0]['Manager'];
foreach($row as $rows){
if($rows['Manager'] !== $team) {
$team = $row['Manager'];
echo "<tr><td colspan=\"3\">Separator</td></tr>";
}
echo "<tr>";
echo "<td>".$row['Manager']."</td>";
echo "<td>".$row['Employee_ID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "</tr>";
}
}
Just move the value allocation $team = $rows['Manager']; in the if condition.
while ($row = mysqli_fetch_array($results)) {
$team = "";
foreach($row as $rows){
if($rows['Manager'] != $team){
echo "<tr><td colspan=\"3\">Separator</td></tr>";
$team = $rows['Manager'];
}
echo "<tr>";
echo "<td>".$rows['Manager']."</td>";
echo "<td>".$rows['Employee_ID']."</td>";
echo "<td>".$rows['Name']."</td>";
echo "</tr>";
}
}

Get field names from database result

Here I want to fetch the field names from the table name which is stored in the variable $table. And want to make a table headers of the field names. What's wrong with this approach I tried:
<?php
$sql=mysql_query("show fields from $table");
if(mysql_num_rows($sql))
while($res = mysql_fetch_object($sql))
{
?>
<th><?php echo $res->field; ?></th>
<?
}
else
{
echo "No data to display";
}
?>
$printTHs = true;
while($res = mysql_fetch_assoc($sql))
{
if ($printTHs)
{
printTableHeader($res);
$printTHs= false;
}
echo "<tr>";
foreach($res as $val)
{
echo "<td>" . $val . "</td>";
}
echo "</tr>";
}
function printTableHeader($res)
{
echo "<tr>";
foreach($res as $col => $val)
{
echo "<th>" . $col . "</th>";
}
echo "</tr>";
}
The query has to be
$sql=mysql_query("show columns from $table");
Use mysql_fetch_assoc() and then get the keys using array_keys(). This will return an array with all the keys.
Use foreach() to get your table headers
Not the optimal solution, but would do it:
$results = mysql_query('SELECT * FROM ' . $table);
if (mysql_num_rows($results))
{
echo '<table>';
$first = TRUE;
while ($row = mysql_fetch_assoc($results))
{
if ($first = TRUE)
{
echo '<tr>';
foreach ($row as $k => $v)
echo '<th>' . $k . '</th>';
echo '</tr>';
$first = FALSE;
}
echo '<tr>';
foreach ($row as $column)
echo '<td>' . $column . '</td>';
echo '</tr>';
}
echo '</table>';
}

Categories