I am still working with redirection.

This works great, but is not exactly what I need:

<?php
ob_start();
?>

<h1>ob redirect test</h1>

<?php
$redirect_page = 'https://www.google.com';
$redirect = true;

if ($redirect == true) {
    header("Location:$redirect_page");
}

ob_end_flush();
?>

I have spent all day trying to figure out how to use this after a user submits a form BUT have it only activate then. Currently it merely loads Google upon run.

I have a lot of wild ideas such as if statements, setting variables to false, then resetting them to true. Make sense? No, it does not to me either.

I am winding up with a confused, heavy load of "spahgetti" code in my head.

I just need to submit a form and display the submitted user-data on the index.php page.

Thank you for your patience and any advice.

Matthew

Recommended Answers

All 37 Replies

Off the top of my head, change

if ($redirect == true) {
    header("Location:$redirect_page");
}

to

if ($redirect == true AND !empty($_POST)) {
    header("Location:$redirect_page");
}

The $_POST[] array gets populated with the form fields when someone submits a form. So we can say to only do a redirect if that array isn't empty, aka someone submitted a form. Of course we can then do more complicated things like redirect to one place if the form says ABC and redirect somewhere else if the form says XYZ.

commented: Thank you for your help. +7

Dani:

Thank you so much for your help. You're brilliant! I am learning so much from your site. It's wonderful.

I modified my code as you posted and it worked perfectly. In the morning I shall further modify the code to include my form. I will post my results here.

Kind regards,
Matthew

Glad that you got it working and that you find DaniWeb so useful :)
Good luck!

Dani,

Hi.

I modified the code. It works wonderful and is exactly what I needed.

One odd issue, though.

I submit data via form > Page redirects to index1.php

It should display a table with four cells, the top two labels, the bottom two, the data input by the User. It displays the table with top labels, but botttom data cells are collapsed. No data. (Note: Entered data does go into the DB. I always check)

Nothing in this code has changed as far as I know. It has always worked easily. Yet, now, I cannot get the data to dump into the two, bottom cells of the table (Please see attachment of the result)

...
$res = mysql_query("SHOW DATABASES");

while ($row = mysql_fetch_assoc($res)) {
    echo $row['Database'] . "\n";
}
mysql_select_db(372110, $con)
or die("Lost");
//Detect DB

$result = mysql_query("SELECT * FROM b8_14160309_simpleDB3 LIMIT 1");
echo "<table border='1'>
<tr>
<th>Employee Name</th>
<th>Employee Address</th>
</tr>";

while($row = mysql_fetch_array($result))

{
echo "<tr>";
echo "<td>" . $row['emp_name'] . "</td>";
echo "<td>" . $row['emp_address'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>

Thank you.

059f785b6b9708dc4439259e82a6bb1e

Hmm, looks right at a quick glance. Nothing staring me in the face. Are you definitely sure that it's the right table and the table is populated?

Dani:

Hi.

Yes, not sure what is going on - It all suddenly changed unbeknownst to me.

Yes, it is the right table. I only have two tables on the DB at this time.

Thank you for your help.

Try:

$result = mysql_query("SELECT * FROM b8_14160309_simpleDB3 LIMIT 1") or die ("Something is wrong here", mysql_error());

OR try:

if(mysql_num_rows($result)==0)
{
   echo "Theres something wrong here";
}

It is not working - This particular section of code was working fine just yesterday. I have no idea what happened. Maddening :/

$result = mysql_query("SELECT * FROM b8_14160309_simpleDB3 LIMIT 1");
echo "<table border='1'>
<tr>
<th>Employee Name</th>
<th>Employee Address</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['emp_name'] . "</td>";
echo "<td>" . $row['emp_address'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>

Did you try phorce's suggestion to print out the error message, if there is one?

Dani, yes I tried phorces suggestion.

I am not sure if I implemented it correctly (I had to modify the snippet); It may be a false-positive of sorts, the results, but it stated "Theres something wrong here".

So, I am not connecting, or..?... Something?

I am going to bed now and sleep it off until early morning where I can start fresh regarding this. I get rather obsessed and have worn myself out today concerning a block of code.

Thank you and good night.

Matthew

Its saying there are no records inside the table.so are there records in the table? Or the query returns NULL I would check the table. Try and screen print it to verify there are records in the table.

Peace

phorce:

Hi.

Yes, the table is very populated. About 7-pages of input data.

Thank you very much for all your help and sugestions!

Matthew

Can you post some more code? I.e. where do you make a connection to the database? If you post back within the next 15 mins, I'll take a look - Or just write one for you to test myself!

phorce:

Good Morning.

Sorry I missed you last night.

Here are the two files I'm working with (I believe the problem(s) lives in the 2nd example,index1.php):
1)Input
2)Redirected output

1)Input
ob_re_4000.php

<?php
ob_start();
?>

<html>
<body>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">Employee Name</td>
<td><input name="emp_name" type="text" id="emp_name"></td>
</tr>
<tr>
<td width="100">Employee Address</td>
<td><input name="emp_address" type="text" id="emp_address"></td>
</tr>
<tr>

<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="add" type="submit" id="add" value="Add Employee">
</td>
</tr>
</table>
</form>

<?php
$con = mysql_connect("localhost","*******0", "********1");
if (!$con)
  {
  die('NEIN! ' . mysql_error());
  }
//Connect to DB

$res = mysql_query("SHOW DATABASES");

while ($row = mysql_fetch_assoc($res)) {
    echo $row['Database'] . "\n";
}
mysql_select_db(372110, $con)
or die("Lost");
//Detect DB

$sql="INSERT INTO b8_*********_simpleDB* (emp_name, emp_address)
VALUES
('$_POST[emp_name]','$_POST[emp_address]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
//Add entry to DB
?>

<?php
$redirect_page = 'http://absinthe42.eu5.org/index1.php';
$redirect = true;

if ($redirect == true AND !empty($_POST)) {
    header("Location:$redirect_page");
}

ob_end_flush();
?>

2)Redirected output
index1.php

<?php

ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);

$con = mysql_connect("localhost","372***", "*********");
if (!$con)
  {
  die('NEIN!' . mysql_error());
  }
//Connect to DB
//(Possible offender below)
$res = mysql_query("SHOW DATABASES");

while ($row = mysql_fetch_assoc($res)) {
    echo $row['372110'] . "\n";
}
mysql_select_db(372***, $con)
or die("Lost");
//Detect DB

//START
$result = mysql_query("SELECT * FROM b8_*********_simpleDB3 LIMIT 1");
echo "<table border='1'>
<tr>
<th>Employee Name</th>
<th>Employee Address</th>

</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['emp_name'] . "</td>";
echo "<td>" . $row['emp_address'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>

Thank you in advance!
Matthew

Debugging.

I think I have isolated the problem lines, but have yet to solve the retrieved data-in-cell display issue.

I believe the issue lies within these two lines:

echo "<td>" . $row['emp_name'] . "JAH!" .  "</td>";
echo "<td>" . $row['emp_address'] . "</td>";

I will fix it, though.

Are emp_name and emp_address the name of the fields in the database?

Also, does it print out JAH! because, if it does, you know it's a db related issue.

Hi Dani.

To answer your two posts, above:

1) Yes, those are the names of the two fields in the DB.

2)I inserted JAH! as a test flag to see if it would populate the collapsed, empty cell, which upon run, it did print within the first, bottom-left cell.

e978c3bc06601d148271b0e49fb8e3b2

Related to this, can you please elaborate on why you think this may be specifically a DB-related issue.

I appreciate your help!

Matthew

Here is the full .php file:

<?php

ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);

$con = mysql_connect("localhost","372***", "********");
if (!$con)
  {
  die('NEIN!' . mysql_error());
  }
//Connect to DB
//(Possible offender below)
$res = mysql_query("SHOW DATABASES");

while ($row = mysql_fetch_assoc($res)) {
    echo $row['372***'] . "\n";
}
mysql_select_db(372***, $con)
or die("Lost");

echo "<table border='1'>
<tr>
<th>Employee Name</th>
<th>Employee Address</th>
</tr>";

$result = mysql_query("SELECT * FROM b8_14160309_simple*** LIMIT 1") or die (mysql_error());
while($row = mysql_fetch_array($result));
{
echo "YES";
echo "<tr>";
echo "<td>" . $row['emp_name'] . "JAH!" .  "</td>";
echo "<td>" . $row['emp_address'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>

Inside your table,

Are the rows emp_name / emp_address or are they named something else? Please print screen the table

Per request:

148268cdcec476d53652f2e4ca86571e

Thank you, phorce.
Matthew

5358d1b5d6d40073ff23c5e397cb5cf6

This post has no text-based content.

I can barely see that, have another one? I'm pretty sure it says 'emp_names' and not 'emp_name' am I right?

Strange:

When posting the image, a link is provide for a larger view; I will try again. One sec...

58ee5cd2ed83374cb5240bb136cea9d6

Please post, what the field names actually say?

It is:
"emp_name" and "emp_address"

Right, ok. mhm. This is really weird.

First off, attempt to do the following:

// After $result, comment the rest put this
$row = mysql_fetch_row($result);

var_dump($row[0]); // What does this give? 

Results:

It will not let me enter "code" directly here. I tried to C&P it into the <Code> formatting, but it erased my text (?) [Bug?]

Here is a screenshot. There should be a link to enlarge the view.

*You can have direct access to my DB if you'd like - Just PM me and I'll send credentials.

324941772da489700960ddb57e6944f0

Have not coded PHP in so long..

<?php

ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);

$con = mysql_connect("localhost","372***", "********");
if (!$con)
  {
  die('NEIN!' . mysql_error());
  }
//Connect to DB
//(Possible offender below)
$res = mysql_query("SHOW DATABASES");

while ($row = mysql_fetch_assoc($res)) {
    echo $row['372***'] . "\n";
}
mysql_select_db(372***, $con)
or die("Lost");

echo "<table border='1'>
<tr>
<th>Employee Name</th>
<th>Employee Address</th>
</tr>";

$result = mysql_query("SELECT * FROM b8_14160309_simple*** LIMIT 1") or die (mysql_error());

$row = mysql_fetch_array($result); // I think you commented out the $result
var_dump($row[0]);

?>

This should now not display any errors. Post back

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.