karatekid36 Posted June 6, 2007 Share Posted June 6, 2007 I have a form that is being used to "update" user info and I need the information to be pulled from the users table. In order to get the variables into the form, I am echoing it in and using an mysql array to place them into the form. at the t-shirt size part of the form the code is as follows <p>T Shirt Size: <label> <input type="radio" name="tshirt" value="XXL" /> XXL</label> <label> <input type="radio" name="tshirt" value="XL" /> XL</label> <label> <input type="radio" name="tshirt" value="L" /> Large</label> <label> <input type="radio" name="tshirt" value="M" /> Medium</label> <label> <input type="radio" name="tshirt" value="S" /> Small</label> </p> In the input tag, i would love to place and if statement that look something like this.. if ($row[8] == "XXL") { echo 'checked="checked"; } else { echo = ""; }; So basically, i would put this in every t shirt size and if the tshirt size in the database matched the radio button, it would be selected. Hence inserting the previous data into the echoed form. I am having trouble writing the code that would allow this to happen. The whole form is listed below for those who need it. This is still very very very underconstruction <?php $page_title = 'Edit a User'; if (!isset($_SESSION['admin'])) { include ('./includes/header_admin.html'); } else { include ('./includes/header.html'); }; // Check for a valid user ID, through GET or POST. if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { // Accessed through view_users.php $id = $_GET['id']; } elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) { // Form has been submitted. $id = $_POST['id']; } else { // No valid ID, kill the script. echo '<h1 id="mainhead">Page Error</h1> <p class="error">This page has been accessed in error.</p><p><br /><br /></p>'; include ('./includes/footer.html'); exit(); } require_once ('./includes/mysql_connect.php'); // Connect to the db. // Check if the form has been submitted. if (isset($_POST['submitted'])) { $errors = array(); // Initialize error array. // Check for a first name. if (empty($_POST['first_name'])) { $errors[] = 'You forgot to enter your first name.'; } else { $fn = escape_data($_POST['first_name']); } // Check for a last name. if (empty($_POST['last_name'])) { $errors[] = 'You forgot to enter your last name.'; } else { $ln = escape_data($_POST['last_name']); } // Check for an email address. if (empty($_POST['email'])) { $errors[] = 'You forgot to enter your email address.'; } else { $e = escape_data($_POST['email']); } // Check for a cellphone. if (empty($_POST['cell'])) { $errors[] = 'You forgot to enter your cellphone number.'; } else { $cp = escape_data($_POST['cell']); } // Check for a screen name. if (empty($_POST['sn'])) { $errors[] = 'You forgot to enter your screen name.'; } else { $sn = escape_data($_POST['sn']); } // Check for an birthday month. if (empty($_POST['b_month'])) { $errors[] = 'You forgot to enter your birthday month.'; } else { $bm = escape_data($_POST['b_month']); } // Check for an hometown address. if (empty($_POST['home_town'])) { $errors[] = 'You forgot to enter your hometown.'; } else { $ht = escape_data($_POST['home_town']); } // Check for a state if (empty($_POST['state'])) { $errors[] = 'You forgot to enter your state.'; } else { $st = escape_data($_POST['state']); } // Check for an tshirt size. if (empty($_POST['tshirt'])) { $errors[] = 'You forgot to enter your tshirt size.'; } else { $ts = escape_data($_POST['tshirt']); } // Check for an grad year. if (empty($_POST['grad_year'])) { $errors[] = 'You forgot to enter your graduation year.'; } else { $gy = escape_data($_POST['grad_year']); } // Check for an pledge semester. if (empty($_POST['pledge_semester'])) { $errors[] = 'You forgot to enter your pledge semester.'; } else { $ps = escape_data($_POST['pledge_semester']); } // Check for an pledge year. if (empty($_POST['pledge_year'])) { $errors[] = 'You forgot to enter your pledge year.'; } else { $py = escape_data($_POST['pledge_year']); } // Check for an pledge class. if (empty($_POST['pledge_class'])) { $errors[] = 'You forgot to enter your pledge class.'; } else { $pc = escape_data($_POST['pledge_class']); } if (empty($errors)) { // If everything's OK. // Test for unique email address. $query = "SELECT user_id FROM brothers WHERE email='$e' AND user_id != $id"; $result = mysql_query($query); if (mysql_num_rows($result) == 0) { // Make the query. $query = "UPDATE brothers SET first_name='$fn', last_name='$ln', email='$e' WHERE user_id=$id"; $result = @mysql_query ($query); // Run the query. if (mysql_affected_rows() == 1) { // If it ran OK. // Print a message. echo '<h1 id="mainhead">Edit a User</h1> <p>The user has been edited.</p><p><br /><br /></p>'; } else { // If it did not run OK. echo '<h1 id="mainhead">System Error</h1> <p class="error">The user could not be edited due to a system error. We apologize for any inconvenience.</p>'; // Public message. echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query . '</p>'; // Debugging message. include ('./includes/footer.html'); exit(); } } else { // Already registered. echo '<h1 id="mainhead">Error!</h1> <p class="error">The email address has already been registered.</p>'; } } else { // Report the errors. echo '<h1 id="mainhead">Error!</h1> <p class="error">The following error(s) occurred:<br />'; foreach ($errors as $msg) { // Print each error. echo " - $msg<br />\n"; } echo '</p><p>Please try again.</p><p><br /></p>'; } // End of if (empty($errors)) IF. } // End of submit conditional. // Always show the form. // Retrieve the user's information. $query = "SELECT first_name, last_name, email, cell, sn, birthday, home_town, state, tshirt, grad_year, pledge_semester, pledge_year, pledge_class FROM brothers WHERE user_id=$id"; $result = @mysql_query ($query); // Run the query. if (mysql_num_rows($result) == 1) { // Valid user ID, show the form. // Get the user's information. $row = mysql_fetch_array ($result, MYSQL_NUM); // Create the form. echo '<h2>Edit a User</h2> <form action="edit_user.php" method="post"> <p>First Name: <input type="text" name="first_name" size="15" maxlength="15" value="' . $row[0] . '" /></p> <p>Last Name: <input type="text" name="last_name" size="15" maxlength="30" value="' . $row[1] . '" /></p> <p>Email Address: <input type="text" name="email" size="20" maxlength="40" value="' . $row[2] . '" /> </p> <p>Cellphone: <input type="text" name="cell" size="11" maxlength="10" value="' . $row[3] . '" /><b>Must have no spaces or characters between the numbers</b> </p> <p>Screen Name: <input type="text" name="sn" size="20" maxlength="40" value="' . $row[4] . '" /> </p> <p>Birthday: <input type="text" name="birthday" size="11" maxlength="40" value="' . $row[5] . '" /><b>Must be in this format YYYY-MM-DD</b></p> <p>Hometown: <input type="text" name="home_town" size="20" maxlength="40" value="' . $row[6] . '" /> </p> <p>State: <input type="text" name="state" size="20" maxlength="40" value="' . $row[7] . '" /> </p> <p>T Shirt Size: <label> <input type="radio" name="tshirt" value="XXL" /> XXL</label> <label> <input type="radio" name="tshirt" value="XL" /> XL</label> <label> <input type="radio" name="tshirt" value="L" /> Large</label> <label> <input type="radio" name="tshirt" value="M" /> Medium</label> <label> <input type="radio" name="tshirt" value="S" /> Small</label> </p> <p><input type="submit" name="submit" value="Submit" /></p> <input type="hidden" name="submitted" value="TRUE" /> <input type="hidden" name="id" value="' . $id . '" /> </form>'; } else { // Not a valid user ID. echo '<h1 id="mainhead">Page Error</h1> <p class="error">This page has been accessed in error.</p><p><br /><br /></p>'; } mysql_close(); // Close the database connection. include ('./includes/footer.html'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/54400-if-statement-construction-within-an-echo-statement/ Share on other sites More sharing options...
trq Posted June 6, 2007 Share Posted June 6, 2007 <input type="radio" name="tshirt" value="XXL"<?php echo (isset($row[8]) && $row[8] == 'XXL') ? " selected='selected'" : "" ?> /> Quote Link to comment https://forums.phpfreaks.com/topic/54400-if-statement-construction-within-an-echo-statement/#findComment-268996 Share on other sites More sharing options...
karatekid36 Posted June 6, 2007 Author Share Posted June 6, 2007 i inserted the code into the file only changing the XXL to XL because I have no records with XXL as of yet. When I replaced those and entered the code into my file, the following error happens.. Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/carrigan/public_html/php_attend/edit_user.php on line 195 This is not an uncommon error I know, but I can not get it to go away. Please remember that this is html that is being echoed in not jsut straight html. How can I fix this so that the radio button is representing the value in the db that is stored for that user? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/54400-if-statement-construction-within-an-echo-statement/#findComment-269322 Share on other sites More sharing options...
trq Posted June 6, 2007 Share Posted June 6, 2007 Post your current code. Quote Link to comment https://forums.phpfreaks.com/topic/54400-if-statement-construction-within-an-echo-statement/#findComment-269324 Share on other sites More sharing options...
karatekid36 Posted June 6, 2007 Author Share Posted June 6, 2007 In my first post, I put the edit user file. That file is without the new code. Here is the new code. <?php $page_title = 'Edit a User'; include ('./includes/header.html'); // Check for a valid user ID, through GET or POST. if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { // Accessed through view_users.php $id = $_GET['id']; } elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) { // Form has been submitted. $id = $_POST['id']; } else { // No valid ID, kill the script. echo '<h1 id="mainhead">Page Error</h1> <p class="error">This page has been accessed in error.</p><p><br /><br /></p>'; include ('./includes/footer.html'); exit(); } require_once ('./includes/mysql_connect.php'); // Connect to the db. // Check if the form has been submitted. if (isset($_POST['submitted'])) { $errors = array(); // Initialize error array. // Check for a first name. if (empty($_POST['first_name'])) { $errors[] = 'You forgot to enter your first name.'; } else { $fn = escape_data($_POST['first_name']); } // Check for a last name. if (empty($_POST['last_name'])) { $errors[] = 'You forgot to enter your last name.'; } else { $ln = escape_data($_POST['last_name']); } // Check for an email address. if (empty($_POST['email'])) { $errors[] = 'You forgot to enter your email address.'; } else { $e = escape_data($_POST['email']); } // Check for a cellphone. if (empty($_POST['cell'])) { $errors[] = 'You forgot to enter your cellphone number.'; } else { $cp = escape_data($_POST['cell']); } // Check for a screen name. if (empty($_POST['sn'])) { $errors[] = 'You forgot to enter your screen name.'; } else { $sn = escape_data($_POST['sn']); } // Check for an birthday month. if (empty($_POST['b_month'])) { $errors[] = 'You forgot to enter your birthday month.'; } else { $bm = escape_data($_POST['b_month']); } // Check for an hometown address. if (empty($_POST['home_town'])) { $errors[] = 'You forgot to enter your hometown.'; } else { $ht = escape_data($_POST['home_town']); } // Check for a state if (empty($_POST['state'])) { $errors[] = 'You forgot to enter your state.'; } else { $st = escape_data($_POST['state']); } // Check for an tshirt size. if (empty($_POST['tshirt'])) { $errors[] = 'You forgot to enter your tshirt size.'; } else { $ts = escape_data($_POST['tshirt']); } // Check for an grad year. if (empty($_POST['grad_year'])) { $errors[] = 'You forgot to enter your graduation year.'; } else { $gy = escape_data($_POST['grad_year']); } // Check for an pledge semester. if (empty($_POST['pledge_semester'])) { $errors[] = 'You forgot to enter your pledge semester.'; } else { $ps = escape_data($_POST['pledge_semester']); } // Check for an pledge year. if (empty($_POST['pledge_year'])) { $errors[] = 'You forgot to enter your pledge year.'; } else { $py = escape_data($_POST['pledge_year']); } // Check for an pledge class. if (empty($_POST['pledge_class'])) { $errors[] = 'You forgot to enter your pledge class.'; } else { $pc = escape_data($_POST['pledge_class']); } if (empty($errors)) { // If everything's OK. // Test for unique email address. $query = "SELECT user_id FROM brothers WHERE email='$e' AND user_id != $id"; $result = mysql_query($query); if (mysql_num_rows($result) == 0) { // Make the query. $query = "UPDATE brothers SET first_name='$fn', last_name='$ln', email='$e' WHERE user_id=$id"; $result = @mysql_query ($query); // Run the query. if (mysql_affected_rows() == 1) { // If it ran OK. // Print a message. echo '<h1 id="mainhead">Edit a User</h1> <p>The user has been edited.</p><p><br /><br /></p>'; } else { // If it did not run OK. echo '<h1 id="mainhead">System Error</h1> <p class="error">The user could not be edited due to a system error. We apologize for any inconvenience.</p>'; // Public message. echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query . '</p>'; // Debugging message. include ('./includes/footer.html'); exit(); } } else { // Already registered. echo '<h1 id="mainhead">Error!</h1> <p class="error">The email address has already been registered.</p>'; } } else { // Report the errors. echo '<h1 id="mainhead">Error!</h1> <p class="error">The following error(s) occurred:<br />'; foreach ($errors as $msg) { // Print each error. echo " - $msg<br />\n"; } echo '</p><p>Please try again.</p><p><br /></p>'; } // End of if (empty($errors)) IF. } // End of submit conditional. // Always show the form. // Retrieve the user's information. $query = "SELECT first_name, last_name, email, cell, sn, birthday, home_town, state, tshirt, grad_year, pledge_semester, pledge_year, pledge_class FROM brothers WHERE user_id=$id"; $result = @mysql_query ($query); // Run the query. if (mysql_num_rows($result) == 1) { // Valid user ID, show the form. // Get the user's information. $row = mysql_fetch_array ($result, MYSQL_NUM); // Create the form. echo '<h2>Edit a User</h2> <form action="edit_user.php" method="post"> <p>First Name: <input type="text" name="first_name" size="15" maxlength="15" value="' . $row[0] . '" /></p> <p>Last Name: <input type="text" name="last_name" size="15" maxlength="30" value="' . $row[1] . '" /></p> <p>Email Address: <input type="text" name="email" size="20" maxlength="40" value="' . $row[2] . '" /> </p> <p>Cellphone: <input type="text" name="cell" size="11" maxlength="10" value="' . $row[3] . '" /><b>Must have no spaces or characters between the numbers</b> </p> <p>Screen Name: <input type="text" name="sn" size="20" maxlength="40" value="' . $row[4] . '" /> </p> <p>Birthday: <input type="text" name="birthday" size="11" maxlength="40" value="' . $row[5] . '" /><b>Must be in this format YYYY-MM-DD</b></p> <p>Hometown: <input type="text" name="home_town" size="20" maxlength="40" value="' . $row[6] . '" /> </p> <p>State: <input type="text" name="state" size="20" maxlength="40" value="' . $row[7] . '" /> </p> <p>T Shirt Size: <label> <input type="radio" name="tshirt" value="XXL" /> XXL</label> <label> <input type="radio" name="tshirt" value="XL"<?php echo (isset($row[8]) && $row[8] == 'XL') ? " selected='selected'" : "" ?> /> <label> <input type="radio" name="tshirt" value="L" /> Large</label> <label> <input type="radio" name="tshirt" value="M" /> Medium</label> <label> <input type="radio" name="tshirt" value="S" /> Small</label> </p> <p><input type="submit" name="submit" value="Submit" /></p> <input type="hidden" name="submitted" value="TRUE" /> <input type="hidden" name="id" value="' . $id . '" /> </form>'; } else { // Not a valid user ID. echo '<h1 id="mainhead">Page Error</h1> <p class="error">This page has been accessed in error.</p><p><br /><br /></p>'; } mysql_close(); // Close the database connection. include ('./includes/footer.html'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/54400-if-statement-construction-within-an-echo-statement/#findComment-269337 Share on other sites More sharing options...
thefortrees Posted June 6, 2007 Share Posted June 6, 2007 $query = "SELECT tshirt FROM brothers WHERE user_id = '$id';"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); <input type="radio" name="tshirt" value="XXL" <?php if ($row['tshirt'] == 'XXL') echo 'checked';?> /> Also, if you want to condense your code (the portion that checks to see if all the fields are filled out), you can do something like this: foreach($_POST as $value){ if ( empty($value) ){ $msg = 'Please fill out all required fields.'; return $msg; } } Quote Link to comment https://forums.phpfreaks.com/topic/54400-if-statement-construction-within-an-echo-statement/#findComment-269338 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.