IT.COM

Looping Won't Work the Way I Need It to in PHP...Help

Spaceship Spaceship
Watch

Archangel

randypendleton.comTop Member
Impact
1,768
Here is my dilemma in a nutshell: I'm trying to empty multiple tables in the same database & post the content of them all on a web page. I've spent hours trying to do this to no avail. Given, I'm not much of a programmer so the answer/solution might be simple. But I can't figure it out. Look at my code (I modded these for obvious reasons):

First page:
PHP:
All items in this table:<p>
<form method=post action=page2.php>

<?
$username="xxx";
$password="xxx";
$database="xxx";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM donotdelete";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();



$i=0;
while ($i < $num) {
$x=$i+1;
$id=mysql_result($result,$i,"id");
$items=mysql_result($result,$i,"items");


echo "<INPUT TYPE=text NAME=tablenames[] value=" . $items . " SIZE=30><br>";



$i++;
}

echo "<p>Last record number in table:  <INPUT TYPE=text NAME=total value=" . $num . " SIZE=10><br>";

?>
<p>
<input type=submit>

And page2.php:

PHP:
<?

$tablenames = array();
$tablenames = $_POST['tablenames'];  


$total = $_POST['total'];  


$i=0;
while ($i < $total) {

$table=$tablename;

$username="xxx";
$password="xxx";
$database="xxx";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM $tablenames";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {

$number=mysql_result($result,$i,"number");
$date=mysql_result($result,$i,"date");
$name=mysql_result($result,$i,"name");
$email=mysql_result($result,$i,"email");


echo "Email address for number " . $x .":  <INPUT TYPE=text NAME=email[] value=" . $email . " SIZE=30><br>";
echo "Full name for number " . $x .":  <INPUT TYPE=text NAME=name[] value=" . $name . " SIZE=30><br>";




$i++;
}
$i++;
}

?>

Don't worry about "unused" vars, like $date. They'll be incorporated at a later date.

What is this script supposed to do? The first page enters table donotdelete and pulls out a list of names & the number of the highest ID in the table (using $num. It works for what I need). These are saved in var and passed on to the next page.

On the next page, I tried to use a loop to make a DB call for each table I have, using $tablenames to specify each table. The result was to have all the data from ALL tables named with $tablenames displayed on page2.php (I was then going to save the data via a hidden form element and pass it over to a page3.php). The script displays info for ONE table and will not loop.

Could anyone offer a solution? :(
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
untested...

PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title></title>
</head>

<body>

<p>
	All items in this table:<br />
	<form method="post" action="page2.php">
<?php

$username = 'xxx';
$password = 'xxx';
$database = 'xxx';

$db = @mysql_connect('localhost', $username, $password) or die('Unable to connect to the database:<br />' . mysql_error());
@mysql_select_db($database, $db) or die('Unable to select database:<br />' . mysql_error());

$result = mysql_query("
	SELECT items
	FROM donotdelete
", $db);

if (mysql_num_rows($result) > 0)
{
	while ($row = mysql_fetch_assoc($result))
	{
		echo "<input type=\"text\" name=\"tablenames[]\" value=\"$row[items]\" size=\"30\" /><br />\n";
	}
	echo '<input type="submit" name="submit" value="Submit" />';
}
mysql_free_result($result);

?>
	</form>
</p>

</body>
</html>

Page 2
PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title></title>
</head>

<body>

<?php

$username = 'xxx';
$password = 'xxx';
$database = 'xxx';

$db = @mysql_connect('localhost', $username, $password) or die('Unable to connect to the database:<br />' . mysql_error());
@mysql_select_db($database, $db) or die('Unable to select database:<br />' . mysql_error());

$tablenames = array_map('trim', (array)$_POST['tablenames']);

if (count($tablenames) > 0)
{
	foreach ($tablenames AS $table)
	{
		$result = mysql_query("
			SELECT *
			FROM $table
		", $db);

		if (mysql_num_rows($result) > 0)
		{
			while ($row = mysql_fetch_assoc($result))
			{
				$number = $row['number'];
				$date = $row['date'];
				$name = $row['name'];
				$email = $row['email'];

				echo "<p>\n\tEmail address for $number:  <input type=\"text\" name=\"email[]\" value=\"$email\" size=\"30\" /><br />\n\t";
				echo "Full name for $number:  <input type=\"text\" name=\"name[]\" value=\"$name\" size=\"30\" /><br />\n\t</p>\n";
			}
		}
	}
}

?>

</body>
</html>
 
1
•••
Change

Code:
echo "<INPUT TYPE=text NAME=tablenames[] value=" . $items . " SIZE=30><br>";
to
Code:
echo "<INPUT TYPE=text NAME=tablenames$i value=" . $items . " SIZE=30><br>";

And page2.php: Romove

Code:
$tablenames = array();
$tablenames = $_POST['tablenames'];

Add This in your while loop
Code:
$tablenames="tablenames$i";
$tableToselect = $_POST[$tablenames] # Not Sure if it should be $_POST[$tablenames] or $_POST['$tablenames']

And Change

Code:
$query="SELECT * FROM $tablenames";
to
Code:
$query="SELECT * FROM $tableToselect ";

I hope this helps not real good with PHP
 
Last edited:
0
•••
I ran the first part, saved it as page1.php. It returns this error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/randylee/public_html/script/page1.php on line 27

Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /home/randylee/public_html/script/page1.php on line 35

untested...

PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title></title>
</head>

<body>

<p>
	All items in this table:<br />
	<form method="post" action="page2.php">
<?php

$username = 'xxx';
$password = 'xxx';
$database = 'xxx';

$db = @mysql_connect('localhost', $username, $password) or die('Unable to connect to the database:<br />' . mysql_error());
@mysql_select_db($database, $db) or die('Unable to select database:<br />' . mysql_error());

$result = mysql_query("
	SELECT items
	FROM donotdelete
", $db);

if (mysql_num_rows($result) > 0)
{
	while ($row = mysql_fetch_assoc($result))
	{
		echo "<input type=\"text\" name=\"tablenames[]\" value=\"$row[items]\" size=\"30\" /><br />\n";
	}
	echo '<input type="submit" name="submit" value="Submit" />';
}
mysql_free_result($result);

?>
	</form>
</p>

</body>
</html>

Page 2
PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title></title>
</head>

<body>

<?php

$username = 'xxx';
$password = 'xxx';
$database = 'xxx';

$db = @mysql_connect('localhost', $username, $password) or die('Unable to connect to the database:<br />' . mysql_error());
@mysql_select_db($database, $db) or die('Unable to select database:<br />' . mysql_error());

$tablenames = array_map('trim', (array)$_POST['tablenames']);

if (count($tablenames) > 0)
{
	foreach ($tablenames AS $table)
	{
		$result = mysql_query("
			SELECT *
			FROM $table
		", $db);

		if (mysql_num_rows($result) > 0)
		{
			while ($row = mysql_fetch_assoc($result))
			{
				$number = $row['number'];
				$date = $row['date'];
				$name = $row['name'];
				$email = $row['email'];

				echo "<p>\n\tEmail address for $number:  <input type=\"text\" name=\"email[]\" value=\"$email\" size=\"30\" /><br />\n\t";
				echo "Full name for $number:  <input type=\"text\" name=\"name[]\" value=\"$name\" size=\"30\" /><br />\n\t</p>\n";
			}
		}
	}
}

?>

</body>
</html>


---------- Post added at 05:59 PM ---------- Previous post was at 05:52 PM ----------

I tried this, too. It still only performs the task on the first table...


Change

Code:
echo "<INPUT TYPE=text NAME=tablenames[] value=" . $items . " SIZE=30><br>";
to
Code:
echo "<INPUT TYPE=text NAME=tablenames$i value=" . $items . " SIZE=30><br>";

And page2.php: Romove

Code:
$tablenames = array();
$tablenames = $_POST['tablenames'];

Add This in your while loop
Code:
$tablenames="tablenames$i";
$tableToselect = $_POST[$tablenames] # Not Sure if it should be $_POST[$tablenames] or $_POST['$tablenames']

And Change

Code:
$query="SELECT * FROM $tablenames";
to
Code:
$query="SELECT * FROM $tableToselect ";

I hope this helps not real good with PHP
 
0
•••
That error usually occurs due to an SQL syntax problem, which causes the result to be invalid. This means you have the odd comma or 2 out of place or you have used a column name that doesn't exist (usually).

If you are 100% sure the SQL statement is fine you need to debug, or just print out the mysql error on your development environment in order to see what the actual problem is.
 
0
•••
after each mysql_query write

PHP:
echo mysql_error();

and post what it says.
 
0
•••
I had to mod this a bit but it works perfectly Repped + Thanks :)

untested...

PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title></title>
</head>

<body>

<p>
	All items in this table:<br />
	<form method="post" action="page2.php">
<?php

$username = 'xxx';
$password = 'xxx';
$database = 'xxx';

$db = @mysql_connect('localhost', $username, $password) or die('Unable to connect to the database:<br />' . mysql_error());
@mysql_select_db($database, $db) or die('Unable to select database:<br />' . mysql_error());

$result = mysql_query("
	SELECT items
	FROM donotdelete
", $db);

if (mysql_num_rows($result) > 0)
{
	while ($row = mysql_fetch_assoc($result))
	{
		echo "<input type=\"text\" name=\"tablenames[]\" value=\"$row[items]\" size=\"30\" /><br />\n";
	}
	echo '<input type="submit" name="submit" value="Submit" />';
}
mysql_free_result($result);

?>
	</form>
</p>

</body>
</html>

Page 2
PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title></title>
</head>

<body>

<?php

$username = 'xxx';
$password = 'xxx';
$database = 'xxx';

$db = @mysql_connect('localhost', $username, $password) or die('Unable to connect to the database:<br />' . mysql_error());
@mysql_select_db($database, $db) or die('Unable to select database:<br />' . mysql_error());

$tablenames = array_map('trim', (array)$_POST['tablenames']);

if (count($tablenames) > 0)
{
	foreach ($tablenames AS $table)
	{
		$result = mysql_query("
			SELECT *
			FROM $table
		", $db);

		if (mysql_num_rows($result) > 0)
		{
			while ($row = mysql_fetch_assoc($result))
			{
				$number = $row['number'];
				$date = $row['date'];
				$name = $row['name'];
				$email = $row['email'];

				echo "<p>\n\tEmail address for $number:  <input type=\"text\" name=\"email[]\" value=\"$email\" size=\"30\" /><br />\n\t";
				echo "Full name for $number:  <input type=\"text\" name=\"name[]\" value=\"$name\" size=\"30\" /><br />\n\t</p>\n";
			}
		}
	}
}

?>

</body>
</html>
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back