mjgdunne Posted April 23, 2008 Share Posted April 23, 2008 Hi, i have a basic search working and i want to add an edit button so that i can send the results from the search to another php script so that i can edit the details, is this possible, im new to php thanks. Quote Link to comment https://forums.phpfreaks.com/topic/102502-solved-send-results-from-one-php-script-to-another-script/ Share on other sites More sharing options...
947740 Posted April 23, 2008 Share Posted April 23, 2008 <?php session_start(); $_SESSION['varname'] = "variable contents";?> You have to start the session on every page you want to access the info. I would recommend doing a $varname = $_SESSION['varname'] on the pages you need to access the info. This makes it easier to use the data. Quote Link to comment https://forums.phpfreaks.com/topic/102502-solved-send-results-from-one-php-script-to-another-script/#findComment-524904 Share on other sites More sharing options...
mjgdunne Posted April 23, 2008 Author Share Posted April 23, 2008 Hi, i have the following code: if (isset($_POST['myq'])) { $myq = mysql_real_escape_string($_POST['myq']); $result = mysql_query("SELECT * FROM members WHERE Firstname LIKE '%$myq' OR Surname LIKE '%$myq'"); while ($row = mysql_fetch_assoc($result)) { echo "<p>You searched for: "" . $myq . ""</p>"; So would i put $result in like: $_SESSION['$results']= "results"; Quote Link to comment https://forums.phpfreaks.com/topic/102502-solved-send-results-from-one-php-script-to-another-script/#findComment-524977 Share on other sites More sharing options...
947740 Posted April 23, 2008 Share Posted April 23, 2008 Actually, you would do $_SESSSION['results'] = $results; That will store the query in the session variable. I may have confused on the last part of my message. When you need to access the info on another page, you do <?php // It is a good habit to start the session at the top of the page session_start(); $results = $_SESSION['results']; // Process the data ?> Quote Link to comment https://forums.phpfreaks.com/topic/102502-solved-send-results-from-one-php-script-to-another-script/#findComment-524985 Share on other sites More sharing options...
mjgdunne Posted April 23, 2008 Author Share Posted April 23, 2008 Thanks, does this mean i can access the results in the session on any php script, and would i just use session_start(); at the start of each script. Quote Link to comment https://forums.phpfreaks.com/topic/102502-solved-send-results-from-one-php-script-to-another-script/#findComment-524989 Share on other sites More sharing options...
947740 Posted April 23, 2008 Share Posted April 23, 2008 Exactly. The session can be used as long as the browser window is open, no matter what page you are on. Quote Link to comment https://forums.phpfreaks.com/topic/102502-solved-send-results-from-one-php-script-to-another-script/#findComment-524991 Share on other sites More sharing options...
mjgdunne Posted April 23, 2008 Author Share Posted April 23, 2008 If i use exit(); at the end if the script does that not exit the session, sorry these questions are prop very basic, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/102502-solved-send-results-from-one-php-script-to-another-script/#findComment-524994 Share on other sites More sharing options...
947740 Posted April 23, 2008 Share Posted April 23, 2008 That will not end the session. There are only 2 ways to end the session: session_destroy() and some other way I cannot remember. EDIT: The "other way" is session_unset(), which only clears the variables; it does not end the session. Quote Link to comment https://forums.phpfreaks.com/topic/102502-solved-send-results-from-one-php-script-to-another-script/#findComment-524995 Share on other sites More sharing options...
mjgdunne Posted April 23, 2008 Author Share Posted April 23, 2008 Thanks, but im getting an error: Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\search_emp.php:15) in C:\xampp\htdocs\search_emp.php on line 30 Any idea what it could be? Quote Link to comment https://forums.phpfreaks.com/topic/102502-solved-send-results-from-one-php-script-to-another-script/#findComment-524997 Share on other sites More sharing options...
947740 Posted April 23, 2008 Share Posted April 23, 2008 Move the session_start() to the very top of your script. Quote Link to comment https://forums.phpfreaks.com/topic/102502-solved-send-results-from-one-php-script-to-another-script/#findComment-524998 Share on other sites More sharing options...
mjgdunne Posted April 23, 2008 Author Share Posted April 23, 2008 Here is my code: <html> <head> <title>Car Rentals & Returns</title> <meta http-equiv="Content-Type" content="text/html" /> <link href="style2.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="wrapper3"> <img src="images/cars.jpg" width="996" height="100"></a> <TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=100%> <tr> <td align="center"> <H1>Search Results:</H1> <form action="edit_emp.php" method="post"> </td> </table> <TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=30%> <tr> <TD ALIGN=CENTER VALIGN=TOP WIDTH=50> </TD> <TD ALIGN=LEFT VALIGN=TOP WIDTH=83%> <TR><TD> <?php session_start(); $_SESSION['$result'] = "variable contents"; ini_set( 'display_errors', '1' ); error_reporting ( 2047 ); $host="localhost"; // Host name $username="root"; // Mysql username $password="root"; // Mysql password $db_name="test"; // Database name $tbl_name="members"; // Table name // Connect to server and select databse. mysql_connect("$host", "root", "root")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $myq=$_POST['myq']; if (isset($_POST['myq'])) { $myq = mysql_real_escape_string($_POST['myq']); $result = mysql_query("SELECT * FROM members WHERE Firstname LIKE '%$myq' OR Surname LIKE '%$myq'"); while ($row = mysql_fetch_assoc($result)) { echo "<p>You searched for: "" . $myq . ""</p>"; echo "<table border=\"1\" align=\"center\">"; echo "<tr><th>First Name</th>"; echo "<td>"; echo $row['Firstname']; echo "</td>"; echo "<tr><th>Surname</th>"; echo "<td>"; echo $row['Surname']; echo "</td>"; echo "<tr><th>Username</th>"; echo "<td>"; echo $row['username']; echo "</td>"; echo "<tr><th>Password</th>"; echo "<td>"; echo $row['password']; echo "</td>"; echo "<tr><th>Email</th>"; echo "<td>"; echo $row['email']; echo "</td>"; echo "<tr><th>Level</th>"; echo "<td>"; echo $row['level']; echo "</td>"; } } if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); } if (mysql_num_rows($result) == 0) { echo "No records found"; exit; } exit(); ?> <TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=100%> <tr> <input type="submit" value="Update Details"/> </form> <form method="post" action="login_success3.php"> <td align="center"><input type="submit" value="Home"/></form> </td> </tr> </table> </table> </td> </tr> </table> </BODY> </HTML> I am still getting the same error. Quote Link to comment https://forums.phpfreaks.com/topic/102502-solved-send-results-from-one-php-script-to-another-script/#findComment-525005 Share on other sites More sharing options...
947740 Posted April 23, 2008 Share Posted April 23, 2008 <?php //Added this session_start() session_start(); ?> <html> <head> <title>Car Rentals & Returns</title> <meta http-equiv="Content-Type" content="text/html" /> <link href="style2.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="wrapper3"> <img src="images/cars.jpg" width="996" height="100"></a> <TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=100%> <tr> <td align="center"> <H1>Search Results:</H1> <form action="edit_emp.php" method="post"> </td> </table> <TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=30%> <tr> <TD ALIGN=CENTER VALIGN=TOP WIDTH=50> </TD> <TD ALIGN=LEFT VALIGN=TOP WIDTH=83%> <TR><TD> <?php // Got rid of this session // Moved this session declaration ini_set( 'display_errors', '1' ); error_reporting ( 2047 ); $host="localhost"; // Host name $username="root"; // Mysql username $password="root"; // Mysql password $db_name="test"; // Database name $tbl_name="members"; // Table name // Connect to server and select databse. mysql_connect("$host", "root", "root")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $myq=$_POST['myq']; if (isset($_POST['myq'])) { $myq = mysql_real_escape_string($_POST['myq']); $result = mysql_query("SELECT * FROM members WHERE Firstname LIKE '%$myq' OR Surname LIKE '%$myq'"); // There was nothing to declare before $_SESSION['result'] = $result; while ($row = mysql_fetch_assoc($result)) { echo "<p>You searched for: "" . $myq . ""</p>"; echo "<table border=\"1\" align=\"center\">"; echo "<tr><th>First Name</th>"; echo "<td>"; echo $row['Firstname']; echo "</td>"; echo "<tr><th>Surname</th>"; echo "<td>"; echo $row['Surname']; echo "</td>"; echo "<tr><th>Username</th>"; echo "<td>"; echo $row['username']; echo "</td>"; echo "<tr><th>Password</th>"; echo "<td>"; echo $row['password']; echo "</td>"; echo "<tr><th>Email</th>"; echo "<td>"; echo $row['email']; echo "</td>"; echo "<tr><th>Level</th>"; echo "<td>"; echo $row['level']; echo "</td>"; } } if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); } if (mysql_num_rows($result) == 0) { echo "No records found"; exit; } exit(); ?> <TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=100%> <tr> <input type="submit" value="Update Details"/> </form> <form method="post" action="login_success3.php"> <td align="center"><input type="submit" value="Home"/></form> </td> </tr> </table> </table> </td> </tr> </table> </BODY> </HTML> Hope that works. Quote Link to comment https://forums.phpfreaks.com/topic/102502-solved-send-results-from-one-php-script-to-another-script/#findComment-525013 Share on other sites More sharing options...
mjgdunne Posted April 23, 2008 Author Share Posted April 23, 2008 Thanks, that got rid of the error, if i use the following displaying the results in input fields will it work: <?php session_start(); ?> <html> <head> <title>Car Rentals & Returns</title> <meta http-equiv="Content-Type" content="text/html" /> <link href="style2.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="wrapper3"> <img src="images/cars.jpg" width="996" height="100"></a> <TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=100%> <tr> <td align="center"> <H1>Search Results:</H1> <form action="edit_emp.php" method="post"> </td> </table> <TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=30%> <tr> <TD ALIGN=CENTER VALIGN=TOP WIDTH=50> </TD> <TD ALIGN=LEFT VALIGN=TOP WIDTH=83%> <TR><TD> <?php $_SESSION['result'] = "$result"; ini_set( 'display_errors', '1' ); error_reporting ( 2047 ); $host="localhost"; // Host name $username="root"; // Mysql username $password="root"; // Mysql password $db_name="test"; // Database name $tbl_name="members"; // Table name // Connect to server and select databse. mysql_connect("$host", "root", "root")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $myq=$_POST['myq']; while ($row = mysql_fetch_assoc($result)) { echo "<p>You searched for: "" . $myq . ""</p>"; echo "<table border=\"1\" align=\"center\">"; echo "<tr><th>First Name</th>"; echo "<td>"; echo '<input type="text" name="firstname" value="'. $row['Firstname'] .'" size="20" />'; echo "</td>"; echo "<tr><th>Surname</th>"; echo "<td>"; echo '<input type="text" name="surname" value="'. $row['Surname'] .'" size="20" />'; echo "</td>"; echo "<tr><th>Username</th>"; echo "<td>"; echo '<input type="text" name="username" value="'. $row['username'] .'" size="20" />'; echo "</td>"; echo "<tr><th>Password</th>"; echo "<td>"; echo '<input type="text" name="password" value="'. $row['password'] .'" size="20" />';; echo "</td>"; echo "<tr><th>Email</th>"; echo "<td>"; echo '<input type="text" name="email" value="'. $row['email'] .'" size="20" />'; echo "</td>"; echo "<tr><th>Level</th>"; echo "<td>"; echo '<input type="text" name="level" value="'. $row['level'] .'" size="20" />'; echo "</td>"; } } exit(); ?> <TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=100%> <tr> <input type="submit" value="Update Details"/> </form> <form method="post" action="login_success3.php"> <td align="center"><input type="submit" value="Home"/></form> </td> </tr> </table> </table> </td> </tr> </table> </BODY> </HTML> Quote Link to comment https://forums.phpfreaks.com/topic/102502-solved-send-results-from-one-php-script-to-another-script/#findComment-525021 Share on other sites More sharing options...
947740 Posted April 23, 2008 Share Posted April 23, 2008 I do not see why id would not. Did you try it and it did not work? Quote Link to comment https://forums.phpfreaks.com/topic/102502-solved-send-results-from-one-php-script-to-another-script/#findComment-525024 Share on other sites More sharing options...
mjgdunne Posted April 23, 2008 Author Share Posted April 23, 2008 Hi, my button to go to the update page has dissapeared, and is not showing up along with the home button? Quote Link to comment https://forums.phpfreaks.com/topic/102502-solved-send-results-from-one-php-script-to-another-script/#findComment-525035 Share on other sites More sharing options...
947740 Posted April 23, 2008 Share Posted April 23, 2008 If you use exit(), it will stop right there and not read anything else. Move the html code before the exit(). Quote Link to comment https://forums.phpfreaks.com/topic/102502-solved-send-results-from-one-php-script-to-another-script/#findComment-525042 Share on other sites More sharing options...
mjgdunne Posted April 23, 2008 Author Share Posted April 23, 2008 Thanks that fixed that, it is having a problem with the line: while ($row = mysql_fetch_assoc($result)) { Is there any other way i could just take $result so i can extract the name etc. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/102502-solved-send-results-from-one-php-script-to-another-script/#findComment-525056 Share on other sites More sharing options...
947740 Posted April 23, 2008 Share Posted April 23, 2008 If you have one row of data: $row = mysql_fetch_array($result); Then, reference the values by columname. echo "Name:".$row['name']; Quote Link to comment https://forums.phpfreaks.com/topic/102502-solved-send-results-from-one-php-script-to-another-script/#findComment-525060 Share on other sites More sharing options...
mjgdunne Posted April 23, 2008 Author Share Posted April 23, 2008 Thanks, i put that in, but two errors, any idea? Notice: Undefined variable: result in C:\xampp\htdocs\edit_emp.php on line 52 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\edit_emp.php on line 52 Quote Link to comment https://forums.phpfreaks.com/topic/102502-solved-send-results-from-one-php-script-to-another-script/#findComment-525062 Share on other sites More sharing options...
947740 Posted April 23, 2008 Share Posted April 23, 2008 Did you replace the while statement with the the $row = mysql_fetch_array($result)? Or did you just add it? (Do not forget to delete the } at the end) Quote Link to comment https://forums.phpfreaks.com/topic/102502-solved-send-results-from-one-php-script-to-another-script/#findComment-525066 Share on other sites More sharing options...
mjgdunne Posted April 23, 2008 Author Share Posted April 23, 2008 I just replaced it, should i leave it in? Here is the code: <?php session_start(); ?> <html> <head> <title>Car Rentals & Returns</title> <meta http-equiv="Content-Type" content="text/html" /> <link href="style2.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="wrapper3"> <img src="images/cars.jpg" width="996" height="100"></a> <TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=100%> <tr> <td align="center"> <H1>Search Results:</H1> <form action="edit_emp.php" method="post"> </td> </table> <TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=30%> <tr> <TD ALIGN=CENTER VALIGN=TOP WIDTH=50> </TD> <TD ALIGN=LEFT VALIGN=TOP WIDTH=83%> <TR><TD> <?php $_SESSION['result'] = "$result"; ini_set( 'display_errors', '1' ); error_reporting ( 2047 ); $host="localhost"; // Host name $username="root"; // Mysql username $password="root"; // Mysql password $db_name="test"; // Database name $tbl_name="members"; // Table name // Connect to server and select databse. mysql_connect("$host", "root", "root")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $row = mysql_fetch_array($result); echo "<table border=\"1\" align=\"center\">"; echo "<tr><th>First Name</th>"; echo "<td>"; echo '<input type="text" name="firstname" value="'. $row['Firstname'] .'" size="20" />'; echo "</td>"; echo "<tr><th>Surname</th>"; echo "<td>"; echo '<input type="text" name="surname" value="'. $row['Surname'] .'" size="20" />'; echo "</td>"; echo "<tr><th>Username</th>"; echo "<td>"; echo '<input type="text" name="username" value="'. $row['username'] .'" size="20" />'; echo "</td>"; echo "<tr><th>Password</th>"; echo "<td>"; echo '<input type="text" name="password" value="'. $row['password'] .'" size="20" />';; echo "</td>"; echo "<tr><th>Email</th>"; echo "<td>"; echo '<input type="text" name="email" value="'. $row['email'] .'" size="20" />'; echo "</td>"; echo "<tr><th>Level</th>"; echo "<td>"; echo '<input type="text" name="level" value="'. $row['level'] .'" size="20" />'; echo "</td>"; exit(); ?> <TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=100%> <tr> <input type="submit" value="Update Details"/> </form> <form method="post" action="login_success3.php"> <td align="center"><input type="submit" value="Home"/></form> </td> </tr> </table> </table> </td> </tr> </table> </BODY> </HTML> Quote Link to comment https://forums.phpfreaks.com/topic/102502-solved-send-results-from-one-php-script-to-another-script/#findComment-525070 Share on other sites More sharing options...
947740 Posted April 23, 2008 Share Posted April 23, 2008 Uh oh...I just realized you do not have a query. You have to query the database before you can use information. Quote Link to comment https://forums.phpfreaks.com/topic/102502-solved-send-results-from-one-php-script-to-another-script/#findComment-525074 Share on other sites More sharing options...
mjgdunne Posted April 23, 2008 Author Share Posted April 23, 2008 I used if (isset($_POST['myq'])) { $myq = mysql_real_escape_string($_POST['myq']); $result = mysql_query("SELECT * FROM members WHERE Firstname LIKE '%$myq' OR Surname LIKE '%$myq'"); This is the edit results page, i need to get the results from results, do i still need a query? Quote Link to comment https://forums.phpfreaks.com/topic/102502-solved-send-results-from-one-php-script-to-another-script/#findComment-525076 Share on other sites More sharing options...
947740 Posted April 23, 2008 Share Posted April 23, 2008 Whoa, I was all confused there for a minute. You have to use the previously established $_SESSION['variable'] to access the information. Use $row = mysql_fetch_array($_SESSION['result']). It should work, but I am not sure. Quote Link to comment https://forums.phpfreaks.com/topic/102502-solved-send-results-from-one-php-script-to-another-script/#findComment-525082 Share on other sites More sharing options...
mjgdunne Posted April 23, 2008 Author Share Posted April 23, 2008 Thanks for all your help so far, only one error left now, Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\edit_emp.php on line 51 Quote Link to comment https://forums.phpfreaks.com/topic/102502-solved-send-results-from-one-php-script-to-another-script/#findComment-525111 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.