The_Thorn Posted January 16, 2014 Share Posted January 16, 2014 (edited) I have been struggling with this issue for some time; Reading, researching the possible problem(s). Still, I cannot produce the desired result: User enters data into form fields User submits data Page redirects User input needs to be displayed in a table on the resulting page It is number 4. that I'm having trouble with. No matter what I've tried, I cannot get the form data, input, to display on the redirected page. Here is the sloppy, redundant code: <?php session_start(); ini_set('display_errors', 'On'); error_reporting(E_ALL | E_STRICT); $con = mysql_connect("localhost","admin****","*Bridlepath*","test*"); 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['testTable1'] . "\n"; } mysql_select_db(test1, $con) or die("Lost"); echo "<table border='1'> <tr> <th>Employee Name</th> <th>Employee Address</th> </tr>"; $result = mysql_query("SELECT * FROM testTable1 LIMIT 1") or die (mysql_error()); while($row = mysql_fetch_array($result)) { echo $row['emp_name'] . " " . $row['emp_address']; echo "<br>"; } $result = mysql_query("SELECT * FROM testTable1 LIMIT 1") or die (mysql_error()); while($row = mysql_fetch_array($result)) { echo '<table><tr>'; echo '<td>'; echo '<Strong>Name:</strong><br/>'; echo $row; echo '</td>'; echo '</tr></table>'; } echo '<p>The session value for test is now: ' . $_SESSION[test] . '</p>'; mysql_close($con); ?> Thank you in advance for any advice or help. Edited January 16, 2014 by The_Thorn Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 16, 2014 Share Posted January 16, 2014 No matter what I've tried, I cannot get the form data, input, to display on the redirected page. What code are you using to redirect? Here is the sloppy, redundant code: What is wrong with it? What should it be doing? Quote Link to comment Share on other sites More sharing options...
The_Thorn Posted January 16, 2014 Author Share Posted January 16, 2014 What code are you using to redirect? What is wrong with it? What should it be doing? Form/Redirect Page: <?php session_start(); $_SESSION['test'] = $row; 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","admin1000","2Bridlepath2","test1"); if (!$con) { die('N0! ' . mysql_error()); } //Connect to DB $res = mysql_query("SHOW DATABASES"); while ($row = mysql_fetch_assoc($res)) { echo $row['Database'] . "\n"; } mysql_select_db(test1, $con) or die("Lost"); //Detect DB $sql="INSERT INTO testTable1 (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://redlinedown.com/revDisp4.php'; $redirect = true; if ($redirect == true AND !empty($_POST)) { header("Location:$redirect_page"); exit(); } ob_end_flush(); ?> When User submits form (This page, above), the page redirects and displays User input. Thank you. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 16, 2014 Share Posted January 16, 2014 (edited) in that case in revDisp4.php you need to get the newest record (which will be the last) SELECT * FROM testTable1 ORDER BY id DESC LIMIT 1 I assume you have an id column. or when you insert the record, get the records id and pass that to your script $sql="INSERT INTO testTable1 (emp_name, emp_address) VALUES ('$_POST[emp_name]','$_POST[emp_address]')"; if (!mysql_query($sql,$con)) { die('Error:' . mysql_error()); } $record_id = mysql_insert_id(); // get the id of the record that was inserted // pass the records id to the redirect page $redirect_page = 'http://redlinedown.com/revDisp4.php?id='.$record_id; Now in revDisp4.php you'd get the records id from $_GET['id']. example <?php session_start(); ini_set('display_errors', 'On'); error_reporting(E_ALL | E_STRICT); $con = mysql_connect("localhost","admin****","*Bridlepath*","test*"); if (!$con) { die('NEIN!' . mysql_error()); } //Connect to DB //(Possible offender below) mysql_select_db(test1, $con) or die("Lost"); // does the id exist? if(isset($_GET['id'])) { // get the record id $record_id = intval($_GET['id']); echo "<table border='1'> <tr> <th>Employee Name</th> <th>Employee Address</th> </tr>"; // fetch the record that matches the id $result = mysql_query("SELECT * FROM testTable1 WHERE id = $record_id") or die (mysql_error()); $row = mysql_fetch_array($result) echo '<table><tr>'; echo '<td>'; echo '<Strong>Name:</strong><br/>'; echo $row; echo '</td>'; echo '</tr></table>'; } mysql_close($con); ?> Edited January 16, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
The_Thorn Posted January 16, 2014 Author Share Posted January 16, 2014 (edited) Thank you, Ch0cu3r. But when I edited all the code via your suggestions, I received a blank page, where the data should be displayed, <?php session_start(); ini_set('display_errors', 'On'); error_reporting(E_ALL | E_STRICT); $con = mysql_connect("localhost","admin****","*Bridlepath*","test*"); if (!$con) { die('NEIN!' . mysql_error()); } //Connect to DB mysql_select_db(test1, $con) or die("Lost"); // does the id exist? if(isset($_GET['id'])) { // get the record id $record_id = intval($_GET['id']) or die("STOPPED!"); echo "<table border='1'> <tr> <th>Employee Name</th> <th>Employee Address</th> </tr>"; // fetch the record that matches the id $result = mysql_query("SELECT * FROM testTable1 WHERE id = $record_id") or die (mysql_error()); $row = mysql_fetch_array($result) echo '<table><tr>'; echo '<td>'; echo '<Strong>Name:</strong><br/>'; echo $row; echo '</td>'; echo '</tr></table>'; } mysql_close($con); ?> Please visit the following URL if you get the chance and give it a try: http://redlinedown.com/ob_res5.php Thank you so much! Edited January 16, 2014 by The_Thorn Quote Link to comment Share on other sites More sharing options...
The_Thorn Posted January 16, 2014 Author Share Posted January 16, 2014 Now I am getting the following when redirecting page: Notice: Use of undefined constant test1 - assumed 'test1' in /home/admin1000/public_html/revDisp5.php on line 14 Employee Name Employee Address Name: Notice: Array to string conversion in /home/admin1000/public_html/revDisp5.php on line 39 Array Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 16, 2014 Share Posted January 16, 2014 Notice: Array to string conversion in /home/admin1000/public_html/revDisp5.php on line 39Array echo $row; should be echo $row['emp_name']; Notice: Use of undefined constant test1 - assumed 'test1' in /home/admin1000/public_html/revDisp5.php on line 14 test1 needs to be wrapped in quotes on line 14 mysql_select_db('test1', $con) Quote Link to comment Share on other sites More sharing options...
The_Thorn Posted January 16, 2014 Author Share Posted January 16, 2014 Hi. Ok, did that. Thank you. Please look at and submit: http://redlinedown.com/ob_res5.php The result displays converted arrays (This is the closest I've come to getting anything to display). I do not understand why this is displaying all elements TWICE; I do not need the row ID displayed. Please comment if you get the chance. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 16, 2014 Share Posted January 16, 2014 (edited) You most probably are querying the database and echo'ing the data twice. Without seeing the code for revDisp5.php we cannot specifically tell you why. Edited January 16, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
The_Thorn Posted January 16, 2014 Author Share Posted January 16, 2014 You most probably are querying the database and echo'ing the data twice. Without seeing the code for revDisp5.php we cannot specifically tell you why. This is the current code for revDisp5.php: <?php session_start(); ini_set('display_errors', 'On'); error_reporting(E_ALL | E_STRICT); $con = mysql_connect("localhost","admin1000","2Bridlepath2","test1"); if (!$con) { die('NEIN!' . mysql_error()); } //Connect to DB mysql_select_db('test1', $con) or die("Lost"); // does the id exist? if(isset($_GET['id'])) { // get the record id $record_id = intval($_GET['id']) or die("STOPPED!"); echo "<table border='1'> <tr> <th>Employee Name</th> <th>Employee Address</th> </tr>"; // fetch the record that matches the id $result = mysql_query("SELECT * FROM testTable1 WHERE id = $record_id") or die (mysql_error()); $row = mysql_fetch_array($result); //test1 $arraystring = print_r($row, true); $arraystring = '<pre>'.print_r($row, true).'</pre>'; //test2 $comma_separated = implode(",", $row); echo $comma_separated; // echo '<table><tr>'; echo '<td>'; echo '<Strong>Name:</strong><br/>'; echo $comma_separated; echo '</td>'; echo '</tr></table>'; } mysql_close($con); ?> http://redlinedown.com/revDisp5.php?id=105 Thank you, Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Barand Posted January 16, 2014 Share Posted January 16, 2014 fetch_array() gets the data twice in the array (one with numeric indexes and also with field names as indexes). Use fetch_assoc() or fetch_row() to only get the data once, or use optional parameter with fetch_array() Quote Link to comment Share on other sites More sharing options...
The_Thorn Posted January 16, 2014 Author Share Posted January 16, 2014 fetch_array() gets the data twice in the array (one with numeric indexes and also with field names as indexes). Use fetch_assoc() or fetch_row() to only get the data once, or use optional parameter with fetch_array() Thank you, Sir! I learn so much from this fine site. Quote Link to comment 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.