Hi, I'm struggling to get a relatively simple PHP/MySQL script to work. Basically, I've got two tables populated with entries from a database.

However I'm getting errors on the second table mysql_num_rows(): supplied argument is not a valid MySQL result resource and mysql_fetch_array(): supplied argument is not a valid MySQL result resource Now, I get that this means that haven't been defined, but I should have defined them in a superglobal from the result of a form as follows:

$myfirstname=$_POST['myfirstname'];
$mysecondname=$_POST['mysecondname'];
session_register("myfirstname");
session_register("mysecondname");

Table code:

<div id="news_all"  style="float:left">
<h2>News from the team...</h2>
<?php
//DB login
require_once('mysql_login.php');
//$latest = mysql_query("SELECT * FROM messages where tm =  (SELECT MAX(tm) FROM messages)");
$query = "SELECT * FROM messages where first_name = 'Milton' and second_name= 'Players'";
$all_msgs = mysql_query($query);
$num = mysql_num_rows($all_msgs);
mysql_close();
echo "<table border='1' width=400px>";
echo "<tr> <th>Date Sent</th> <th>Message</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $all_msgs )) {
	// Print out the contents of each row into a table
	echo "<tr><td>"; 
	echo $row['tm'];
	echo "</td><td>"; 
	echo $row['message'];
	echo "</td></tr>"; 
} 

echo "</table>";

//dummy text
//include("team_news.txt");

?>
</div>
<div id="news_latest" style="float:left">
<h2>News for you...</h2>
<?php
//DB login
//require_once('mysql_login.php');
$query = "SELECT * FROM messages where first_name = '$myfirstname' and second_name = '$mysecondname'";
$yr_msgs = mysql_query($query);
$num = mysql_num_rows($yr_msgs);
echo "<hr />".$num."<hr />";
mysql_close();
echo "<table border='1' width=400px>";
echo "<tr> <th>Date Sent</th> <th>Message</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $yr_msgs )) {
	// Print out the contents of each row into a table
	echo "<tr><td>"; 
	echo $rowb['tm'];
	echo "</td><td>"; 
	echo $rowb['message'];
	echo "</td></tr>"; 
} 

echo "</table>";
//dummy text
//include("your_news.txt");
?>
</div>

change the $rowb to $row.

and delete line number 10.

or move line 10 all way to the bottom

Thank-you all: your advice has taken me much further. I don't know why I missed that mysql_close() - it has to connect to the database again!

Anyways...

I'm aiming to take the information from a form by assigning each field to a variable, and writing this into superglobals (myfirstname, mysecondname), as shown in the second code block

Using these, the application then accesses the database table to pull the rows which match first and second name (sorry not to post the code, I'm at work!).

They seem to be registered, and I've included session_start() at the start...

Any ideas?

The easiest way that I can think of would be like this:

<?
session_start();
//check for post
if(count($_POST) > 0 && $_POST['btnSubmit'] == "Submit")
{
	//specify the allowed form elements that may post to this page
	$allowedposts = array("myfirstname", "mysecondname");

	//name your form elements the same as the name you would like to give your session variables
	foreach($_POST as $key=>$value)
	{
		//check the post variable against the $allowedposts array for authentication
		//don't worry about $_POST['btnSubmit'] as it will not be in your array
		if(in_array($key, $allowedposts))
		{
			$_SESSION[$key] = $value;
		}
	}
	
	print_r($_SESSION); //for development purposes, show the session vars and values
	
	/* now you have your posted form elements assigned to session variables like so
		$_SESSION['myfirstname'] = "text in field";
		$_SESSION['mysecondname'] = "text in field";
		
		and then you connect to db here, whatever input validation you want to do (HIGHLY RECOMMENDED)
		and run your insert sql query.
	*/
}
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form name="frmNames" action="<? echo $_SERVER['REQUEST_URI']; ?>" method="post">
	<input type="text" name="myfirstname" /><br />
    <input type="text" name="mysecondname" /><br />
    <input type="submit" name="btnSubmit" value="Submit" />
</form>
</body>
</html>
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.