crmamx Posted February 6, 2011 Share Posted February 6, 2011 select_display.php // I am selecting an ID value from the db, result can only be 0 or 1 if (mysql_num_rows($result) == 0) { print '<big style="color: red;"><span style="font-weight: bold;"> ERROR MESSAGE:</span></big> Update ID number not found'; // I don't want my form to be displayed, just the error message } if (mysql_num_rows($result) == 1) { // Here I want to jump to html <form action="insert.php" method="post"> // I don't want to echo the whole form from within php // Now it displays on either compare because I am displaying it after ?> } Quote Link to comment https://forums.phpfreaks.com/topic/226891-update-program-flow-is-killing-me/ Share on other sites More sharing options...
Pikachu2000 Posted February 6, 2011 Share Posted February 6, 2011 I usually use logic along these lines when I expect exactly one record from the database, storing errors in an array along the way. The you can check if the array is empty to decide how to proceed. $errors = array(); // initialize an empty array to hold error messages if( mysql_num_rows($result) ) !== 1 ) { $errors[] = '<big style="color: red;"><span style="font-weight: bold;">ERROR MESSAGE:</span></big> Update ID number not found'; } // form display logic if( empty($errors) ) { // include your form here. } Quote Link to comment https://forums.phpfreaks.com/topic/226891-update-program-flow-is-killing-me/#findComment-1170696 Share on other sites More sharing options...
crmamx Posted February 6, 2011 Author Share Posted February 6, 2011 I like your code better, but until then: // form display logic if( empty($errors) ) { // include your form here. } I still don't understand the include your form here. I new this wouldn't work, which I interpret to be what you suggested. if (mysql_num_rows($result) == 1) { mysql_query("Select FROM airplanes WHERE id='$id'"); <form action="insert.php" method="post"> AMA #: <input name="ama" type="text" value="<? echo $ama; ?>"> </form> } And sure enough it didn't so I know I am missing something. Let's say I delete the value=, still I am executing php and don't understand how to get the form to display inside the if the statement. Quote Link to comment https://forums.phpfreaks.com/topic/226891-update-program-flow-is-killing-me/#findComment-1170740 Share on other sites More sharing options...
Pikachu2000 Posted February 6, 2011 Share Posted February 6, 2011 You can drop out of php and enter the form in straight HTML, or you can contain the form in a separate file and include it. It's a little difficult to tell exactly what result you're after based on the relatively small amount of code in your post . . . Quote Link to comment https://forums.phpfreaks.com/topic/226891-update-program-flow-is-killing-me/#findComment-1170746 Share on other sites More sharing options...
crmamx Posted February 6, 2011 Author Share Posted February 6, 2011 Ok, here is all the code. <?php // change record in database // Connect to database ============================================ include("connect_db.php"); // retrieve form data from form2.html =================================== $id = $_POST['id']; // Send query =================================================== $query = "SELECT * FROM airplanes WHERE id='$id'"; if (!mysql_query($query)){ die('Error :' .mysql_error()); } $result = mysql_query ($query); $num = mysql_num_rows($result); // Check to see if record exists or not ==================================== if (mysql_num_rows($result) == 1) { mysql_query("Select FROM airplanes WHERE id='$id'"); print 'Record successfully retreived'; } // Record not in db =============================================== if (mysql_num_rows($result) == 0) { print '<big style="color: red;"><span style="font-weight: bold;"> ERROR MESSAGE:</span></big> Change ID number not found'; echo "<br />"; echo "<br />"; print 'Use the BACK button on your browser to try again'; echo "<br />"; } ?> <h3>Change this record</h3> <form action="insert.php" method="post"> AMA #: <input name="ama" type="text" value="<?php echo $ama; ?>"><br> Model Name: <input name="model_name" type="text" value="<?php echo model_name; ?>"><br> Model Mfg: <input name="model_mfg" type="text" value="<?php echo $model_mfg; ?>"><br> Wingspan: <input name="wingspan" type="text" value="<?php echo $wingspan; ?>"><br> Engine Mfg & Size: <input name="engine" type="text" value="<?php echo $engine; ?>"><br> Sound Reading: <input name="decibels" type="text" value="<?php echo $decibels; ?>"> Leave blank if you don't know it.<br> <input value="Send" type="submit"> </form> </body> </html> This actually works, or my version does. Did a lot of deleting of test stuff like echos. I actually have 2 problems but was trying to solve them one at a time. 1. The form will display whether I find a record or not. I only want it to display if I find a record. 2. When I find a record the form does not display any values. I appreciate your patience. Quote Link to comment https://forums.phpfreaks.com/topic/226891-update-program-flow-is-killing-me/#findComment-1170759 Share on other sites More sharing options...
jcbones Posted February 6, 2011 Share Posted February 6, 2011 So tell the PHP to only send the form, if the database returns a record. if (mysql_num_rows($result) == 0) { print '<big style="color: red;"><span style="font-weight: bold;"> ERROR MESSAGE:</span></big> Change ID number not found'; echo "<br />"; echo "<br />"; print 'Use the BACK button on your browser to try again'; echo "<br />"; } else { ?> <h3>Change this record</h3> <form action="insert.php" method="post"> AMA #: <input name="ama" type="text" value="<?php echo $ama; ?>"><br> Model Name: <input name="model_name" type="text" value="<?php echo model_name; ?>"><br> Model Mfg: <input name="model_mfg" type="text" value="<?php echo $model_mfg; ?>"><br> Wingspan: <input name="wingspan" type="text" value="<?php echo $wingspan; ?>"><br> Engine Mfg & Size: <input name="engine" type="text" value="<?php echo $engine; ?>"><br> Sound Reading: <input name="decibels" type="text" value="<?php echo $decibels; ?>"> Leave blank if you don't know it.<br> <input value="Send" type="submit"> </form> <?php } ?> </body> </html> Of course I would extract the query from the result resource before I called the values. Quote Link to comment https://forums.phpfreaks.com/topic/226891-update-program-flow-is-killing-me/#findComment-1170784 Share on other sites More sharing options...
crmamx Posted February 6, 2011 Author Share Posted February 6, 2011 I don't understand the code, but it fixed the first problem. The form only displays if select finds a record. What does php and the right curly bracket do here? </form> <?php } ?> </body> Now for the second problem. It is obvious that php is not passing my form the variables I need. I know how to pass them from a form to php but how do I pass them from php to a form. Simply...how do I make the data show up on the form? Quote Link to comment https://forums.phpfreaks.com/topic/226891-update-program-flow-is-killing-me/#findComment-1170817 Share on other sites More sharing options...
Pikachu2000 Posted February 6, 2011 Share Posted February 6, 2011 As for the the curly brace seemingly in the middle of nowhere, it's perfectly fine in php to 'step out' of php and send output to the browser. That curly brace closes the preceding } else {. These two code chunks will produce identical output: <?php if( 1 = 1 ) { // this conditional will always evaluate to TRUE, and is just for illustration echo 'This is line 1<br>'; echo 'This is line 2<br>'; echo 'This is line 3<br>'; echo 'This is line 4<br>'; } Same as: <?php if( 1 = 1 ) { // this conditional will always evaluate to TRUE, and is just for illustration ?> This is line 1<br> This is line 2<br> This is line 3<br> This is line 4<br> <?php } ?> Now, as to why you aren't getting any values echoed into your form, you need to fetch the values from the query result resource before you can display them using mysql_fetch_assoc, mysql_fetch_row, etc. Quote Link to comment https://forums.phpfreaks.com/topic/226891-update-program-flow-is-killing-me/#findComment-1170822 Share on other sites More sharing options...
crmamx Posted February 7, 2011 Author Share Posted February 7, 2011 Damn, this stuff is killing me! I finally got it but only after hours of struggle. It isn't the ].,} stuff that seems to get me. I just don't seem to understand the flow logic of php. Is this the way it works? ($row = mysql_fetch_array( $result )); // $row is initially 1 and the result row name/s and value/s are stored in $result // so I would have row1,name1,name1-value,name2,name2-value, ect // the program continues and drops to the end (somewhere), and if I call for an echo // of row1 by name1 it will output name1-value // and if I think it might find more than one record then I simply add the while statement Now to go back and clean it all up. Changing the db will be duck soup. Thanks again guys. Quote Link to comment https://forums.phpfreaks.com/topic/226891-update-program-flow-is-killing-me/#findComment-1170858 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.