garteth Posted May 27, 2011 Share Posted May 27, 2011 Hi This may seem pretty standard to some of you but I am having a problem with tables. What I am trying to do is give the user 4 fields to choose from and depending upon which field they enter data into the php will query the mysql database and return all of the data related to that value and place it into a table. Seems fair enough however there could be multiple entries of that value and I want there to maybe be a loop that will check for the information and then when it is done complete the table. My code looks something like this: <html> <?php # Script 14.2 - course_form.php // This page allows the administrator to add a course (product). $con = mysql_connect("localhost","root","millennium"); if (!$con) { die('Could not connect: ' . mysql_error()); } if (isset($_POST['submitted'])) { // Check if the form has been submitted. // Validate the print_name, image, artist (existing or first_name, last_name, middle_name), size, price, and description. // Check for a course id. if (!empty($_POST['IpAddress'])) { $ip = ($_POST['IpAddress']); } else { $ip = FALSE; } // Check for a course id. if (!empty($_POST['Identifier'])) { $id = ($_POST['Identifier']); } else { $id = FALSE; } // Check for a course id. if (!empty($_POST['Destination'])) { $de = ($_POST['Destination']); } else { $de = FALSE; } // Check for a course id. if (!empty($_POST['Reply'])) { $re = ($_POST['Reply']); } else { $re = FALSE; } // Add the course to the database. mysql_select_db("accs", $con); $result = mysql_query("SELECT * FROM smsaudit WHERE smsreply='$re' || smsorigip='$ip' || smsidentifier='$id' || smsdest='$de'"); $tdcount = 1; $numtd = 4; // number of cells per row echo "<table border='1'>"; while($row = mysql_fetch_array($result)) { if ($tdcount == 1) echo "<tr>"; echo "<td>IP Address: $ip </td>"; // display as you like echo "<td>Identifier: $id </td>"; echo "<td>Destination: $de </td>"; echo "<td>Reply: $re </td>"; if ($tdcount == $numtd) { echo "</tr>"; $tdcount = 1; } else { $tdcount++; } // time to close up our table if ($tdcount!= 1) { while ($tdcount <= $numtd) { echo "<td> </td>"; $tdcount++; } echo "</tr>"; } echo "</table>"; } echo "<br />"; $result2 = mysql_query("SELECT totalsms FROM smsaudit ORDER BY totalsms DESC LIMIT 1"); while($row = mysql_fetch_array($result2)) { echo 'You have used ' . $row['totalsms'] .' sms messages'; } } else { // Display the form. ?> <form action="form1.php" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="524288"> <p> </p> <table width="449" border="0"> <td><h1>SMS Information</h1></td> <td> </td> <td> </td> <td> </td> </tr> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td width="160"><strong>IP Address</strong></td> <td width="95"> </td> <td width="167"><strong>Identifier</strong></td> </tr> <tr> <td><input name="IpAddress" type="text" id="IpAddress" /></td> <td> </td> <td><input name="Identifier" type="text" id="Identifier" /></td> <td width="9"> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td><strong>Destination</strong></td> <td> </td> <td><strong>Reply</strong></td> <td> </td> </tr> <tr> <td><input name="Destination" type="text" id="Destination" /></td> <td> </td> <td><input name="Reply" type="text" id="Reply" /></td> <td> </td> </tr> <tr> <td><input type="submit" name="Submit" id="Submit" value="Submit" /></td> <td> </td> <td> </td> <td> </td> </tr> </table> <input type="hidden" name="submitted" value="TRUE" /> </form> <?php } // End of main conditional. ?> </html> Can anyone please let me know of a way to achieve this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/237645-put-data-into-a-table/ Share on other sites More sharing options...
mikesta707 Posted May 27, 2011 Share Posted May 27, 2011 Ok, a couple of things. Firstly, when posting code, use code or php tags please. Secondly, don't just post your login information for your mysql tables. Most people here are nice, but someone could just come to the post, and have all your mysql tables and such. You need to give more information. What exactly is happening when you run that code? What do you expect to happen, and how is what is actually happening differ? Your validation is somewhat pointless. You check to see if things are empty, and if they are you set those variables to false, but you do the same thing whether those variables have a value of false, or an actual, meaningful value. I'm also not quite sure what exactly it is you want? If I am understanding correctly, you have 4 input boxes, and based on the values in those 4 boxes, you return information from the database that matches any of the 4 input boxes. Am I interpreting your post correctly? I also don't really understand why you add an empty row at the end of the table, but that really isn't a problem. Anyways, you code seems fine-ish to me, besides a few stylistic issues. What exactly is happening? Quote Link to comment https://forums.phpfreaks.com/topic/237645-put-data-into-a-table/#findComment-1221234 Share on other sites More sharing options...
garteth Posted May 31, 2011 Author Share Posted May 31, 2011 Hi The site is not live it is just a test site for my live one. Basically what I want is for someone to be able to search for information within the database based upon a chosen criteria e.g. I want to find someone using their IP Address so I put that into the IP Address field and click submit. So the php takes the value from the field and then queries the database and pulls out all of the information about that person and will display it into a table. What I dont want to have is a static table, I want it to create a new row each time an entry is pulled from the database and then close itself at the end and display on the webpage. I thought that this code would do the trick however it creates the first row but not the subsequent ones. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/237645-put-data-into-a-table/#findComment-1222786 Share on other sites More sharing options...
George Botley Posted May 31, 2011 Share Posted May 31, 2011 You need to make use of a while() loop when fetching data from a database in order to return multiple values. Quote Link to comment https://forums.phpfreaks.com/topic/237645-put-data-into-a-table/#findComment-1223208 Share on other sites More sharing options...
garteth Posted June 1, 2011 Author Share Posted June 1, 2011 Ive tried that but I do not know what I am doing wrong with the loop. Is there a simpler way for a user to be able to search a database and display the results then the way that I am doing it? Quote Link to comment https://forums.phpfreaks.com/topic/237645-put-data-into-a-table/#findComment-1223452 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.