Jump to content

nomis

Members
  • Posts

    38
  • Joined

  • Last visited

Everything posted by nomis

  1. okay... that was just an example. Instead of making the link "Edit" you could make it "view, and use the same information, rather than producing editable data, you could see information about that one user
  2. Getting information about all users and one user would require different code; if it were me I'd create a different page for viewing information about single users. Since you have this line echo "<a href=\"ninjacardone.php? Action=view&user=" . $Show['username'] . "\">" . $Show['username'] . "</a><br>"; why not edit it to something like this: echo '<a href="ninjacardoneedit.php?username='. $row['username]. '> Edit</a><br>' ; This way these variables will be passed to the URL, and you can grab them on the next page with a $_GET['username']
  3. Thanks... I apologize, I will provide more code (yes there are validation checks, which may be at the root of the problem). I tried your modifications, and got exactly the same problems I had earlier, so it could be my problem is with the validation checks, which are what bring up the form with pre-populated values. Here is a large chunk of what I had originally as person.php . There are more fields, but I'm testing on just variable. (I know I define my variables at the top, and it's not necessary, but it's just an old habit, and makes for fast reference to me... I don't think they'll cause a problem?) (Note that the table person also has an auto increment primary key, which is not mentioned below) <?php //database connection require_once ('connectvars.php'); $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); //form variables if (isset($_POST['submit'])) { $person_type = $_POST['person_type']; $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $middle_name = $_POST['middle_name']; $output_form = false; //if any required fields are missing produce error.. simple check once, rather than each if ((empty($person_type)) || (empty($first_name)) || (empty($last_name)) { echo '<h3>Missing Data</h3>'; echo 'You need to enter at least person type, first name, last name <br />'; $output_form = true; } //make sure NULL values actually get passed as NULL if (!empty($_POST['middle_name'])) { $middle_name = "'$middle_name'"; } else { $middle_name = 'NULL' ; } //If all required exist, report and execute insert query if ((!empty($person_type)) && (!empty($first_name)) && (!empty($last_name)) { // insert values into table $query = "INSERT into person (person_type, last_name, first_name, middle_name)" . "VALUES ('$person_type','$last_name','$first_name',$middle_name)"; $result = mysqli_query($dbc, $query) or die ('Error: '.mysqli_error($dbc)); //echo data entry results to the browser echo 'You entered: <p />';echo 'Person Type: ' . $person_type . '<br />'; echo 'First name: ' . $first_name . '<br />'; echo 'Last name: ' . $last_name . '<br />'; echo 'Middle name: ' . $middle_name . '<br />'; echo '<a href="person.php">Enter another person</a> | <a href="ppmanage.php>Manage Entries</a>'; } } else { $output_form = true; } if ($output_form) { ?> <table width=500 border=0 cellpadding=5> <tr> <td><label for "person_type">Person Type:</label> </td> <td><input type="text" id="person_type" name="person_type" value="<?php echo $person_type; ?>" /> </td> </tr> <tr> <td><label for "last_name">Last Name:</label> </td> <td><input type="text" id="last_name" name="last_name" value="<?php echo $last_name; ?>" /> </td> </tr> <tr> <td><label for "first_name">First Name:</label> </td> <td><input type="text" id="first_name" name="first_name" value="<?php echo $first_name; ?>" /> </td> </tr> <tr> <td><label for "middle_name">Middle Name:</label> </td> <td><input type="text" id="middle_name" name="middle_name" value="<?php echo $middle_name; ?>" /> </td> </tr> </table> <br /> <p> <input type="submit" value="Submit" name="submit" /> </p> </form> <?php } mysqli_close($dbc); ?> Here are the modifications I made with your suggestions with which I got identical results: <?php //database connection require_once ('connectvars.php'); $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); //form variables if (isset($_POST['submit'])) { $person_type = $_POST['person_type']; $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $middle_name = $_POST['middle_name']; $output_form = false; //Validate required variables //if any required fields are missing produce error if ((empty($person_type)) || (empty($first_name)) || (empty($last_name)) { echo '<h3>Missing Data</h3>'; echo 'You need to enter at least person type, first name, last name. <br />'; $output_form = true; } //make sure NULL values actually get passed as NULL if( empty( $_POST ) ) { $sql = "select middle_name from person where person_id='" . mysqli_real_escape_string( $_GET['person_id'] ) . "'"; $middle_name = ''; // Initialize value to empty string $q = mysqli_query( $sql ); if( $q ) { $row = mysqli_fetch_object( $q ); if( $row ) { $middle_name = $row->middle_name; // If we made it this far then the value was retrieved } } $middle_name = !strlen( $middle_name ) ? "" : htmlentities( $middle_name ); } else { //is there an entry with any length $middle_name = isset( $_POST['middle_name'] ) && strlen( trim( $_POST['middle_name'] ) ) ? "'" . mysql_real_escape_string( $_POST['middle_name'] ) . "'" : 'NULL'; } //If all required exist, report and execute insert query if ((!empty($person_type)) && (!empty($first_name)) && (!empty($last_name)) { //database insert $query = "INSERT into person (person_type, last_name, first_name, middle_name)" . "VALUES ('$person_type','$last_name','$first_name',$middle_name)"; $result = mysqli_query($dbc, $query) or die ('Error: '.mysqli_error($dbc)); //echo data entry results to the browser echo 'You entered: <p />';echo 'Person Type: ' . $person_type . '<br />'; echo 'First name: ' . $first_name . '<br />'; echo 'Last name: ' . $last_name . '<br />'; echo 'Middle name: ' . $middle_name . '<p />'; echo '<a href="person.php">Enter another person</a> | <a href="ppmanage.php>Manage Entries</a>'; } } //if there is no submission, show the form else { $output_form = true; } if ($output_form) { ?> <h3>Person Entry</h3> <form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>"> <table width=500 border=0 cellpadding=5> <tr> <td><label for "person_type">Person Type:</label> </td> <td><input type="text" id="person_type" name="person_type" value="<?php echo $person_type; ?>" /> </td> </tr> <tr> <td><label for "last_name">Last Name:</label> </td> <td><input type="text" id="last_name" name="last_name" value="<?php echo $last_name; ?>" /> </td> </tr> <tr> <td><label for "first_name">First Name:</label> </td> <td><input type="text" id="first_name" name="first_name" value="<?php echo $first_name; ?>" /> </td> </tr> <tr> <td><label for "middle_name">Middle Name:</label> </td> <td><input type="text" id="middle_name" name="middle_name" value="<?php echo $middle_name; ?>" /> </td> </tr> </table> <br /> <p> <input type="submit" value="Submit" name="submit" /> </p> </form> <?php } mysqli_close($dbc); ?> I think the problem may be in the verification loop? This works fine, but it does a number on the entries.
  4. okay, thanks. I also realized (subsequently) that none of the data is coming from the database; I'm only grabbing the Post variables that have been entered and echoing them to the form fields if there is a missing required field (not the null fields, but other fields; I just wanted everything entered to be echoed back to the form so the user wouldn't need to keep re-typing the same thing... the issue is dealing with the blank fields. I believe it has something to do with the way I'm echoing these variables. You use this: which is grabbing data from the database... is this the best way of ensuring the nulls remain null? I'd rather not have anything enter the tables until I'm certain that there are no nulls being entered. The way I have it works fine with the exception of a user resubmitting the erred form (it gets worse with each resubmit). I've figured out why; it appears I am renaming the null variables adding more and more quotes with each subsequent error with this bit of code if (!empty($_POST['middle_name'])) { $middle_name = "'$middle_name'"; }else{ $middle_name = 'NULL' ; } ... I'd rather have it not echo anything of there is nothing entered. Am I making this too difficult?
  5. wow, you did a lot of rewriting here... unfortunately, I'm still relatively new to this and am not able to clearly understand object-oriented models. I suppose if I spend some time with this I'll figure it out. (in the meantime, I did figure out the problem has to do with some of my loops... if they go through the first time it's okay, but on subsequent times, the way I have it working, I've ended up renaming the values of some of the variables, so they will continue changing with the echo statement inside the form fields). if you want I can show you the entire script (which is almost done; I'd hate to have to rewrite this as well as four other ones in a new way... ). Thanks; you've been particularly good in responding... I do appreciate it.
  6. yes, exactly. any ideas? I'd rather not have the word null not appear ever; just a blank space that gets translated as null on post.
  7. okay, that sortof works, but now, instead of 'NULL' appearing in the field, just NULL appears upon submit. A true null has been entered into the database, but as the form comes back with the world NULL, if it is submitted again, the text string NULL goes right back in the database. Is there a way to eliminate an entire word (in this case the word "NULL") using an else statement? something like this? if ($middle_name != null) { echo trim($middle_name,"'") ; } else echo trim($middle_name,"NULL"); note, tried that and it didn't work. funny how the easy ones end up never ending, eh?
  8. Thanks... I had actually already tried that but sponsor_type gets treated as part of the text string. I had a head-slap moment and figured out what I was doing wrong... I left out the result rows in the query, so of course that variable $st was just spitting back the contents of the query. Here's what I ended up doing which worked perfectly: $st = "Select sponsor_type from sponsor_type where sponsor_type_code = $sponsor_type_code' "; $resultst = mysqli_query ($dbc, $st) or die ('Error: '.mysqli_error($dbc)); $row = mysqli_fetch_array($resultst); echo 'Sponsor Type: ' . $row[0]. '<br />';
  9. Doh! of course. thanks again. This should work fine (except that because of my earlier solution, the results come back in single quotes... this shouldn't be a problem unless someone submits the form again without removing those quotes... is there an escape string that will work to remove those in the display?)
  10. Okay, this one is confusing me...it may be difficult, or I'm not sure how to find a solution I have a form which for a select, it grabs an id from a separate table, and then prints the id name as part of the <select> option. This is the code: <?php $r = mysqli_query($dbc, 'select * from sponsor_type order by sponsor_type'); while ($row = mysqli_fetch_array($r)) { echo "<option value =\"$row[0]\">$row[1] </option> \n"; } ?></select></td> </tr> <tr> This effectively produces the name of the sponsor_type, but grabs the primary key, which is sponsor_type_code. I want to produce a confirmation which states which type of sponsor was selected. Right now, I'm only able to grab the sponsor_type_code, and not the sponsor. This (of course), would be meaningless to the user. I have defined the variable for sponsor_type_code (as it essential for the next step... not getting into that here, that may be a different issue) $sponsor_type_code = $_POST['sponsor_type_code']; and am calling it with this: echo 'Sponsor Type Code: ' . $sponsor_type_code . '<br />'; This works fine, providing an answer of 'LO' (the code for "Lodging") in one instance, but the problem is that I want to be able to bring back sponsor_type (which is one column to the right of sponsor_type_code) and I'm not sure of the correct syntax... I've tried a few things. First of all I tried this: $st = "Select * from sponsor_type where 'sponsor_type_code' = '$sponsor_type_code'"; $resultst = mysqli_query ($dbc, $st) or die ('Error: '.mysqli_error($dbc)); echo 'Sponsor Type: ' . $st . '<br />'; which of course just brought back this result Sponsor Type: Select * from sponsor_type where 'sponsor_type_code' = 'LO' I can see that I am able to get some close data... is there a way for me to just bring back the second column from this query? I tried naming a second variable $sponsor_type, but of course, it's not really a post variable... do I link it to $st somehow? something like the below (which of course does not work... I know the syntax is wrong... $sponsor_type = $st.col[1]; thanks in advance for any ideas
  11. thanks, good advice. However, I've run into another minor problem. The results of my update into a form (the same form used to submit the data, so that more changes can be made if necessary), which echos any data entered back to the form. If there is a null, it prints "NULL" on the form, which is okay, but if a user submits the form again, it enters a text string of "NULL" back into the table. It works fine if the "NULL" is deleted, but this could cause problems if a user is not careful. This is how it looks <tr> <td><label for "middle_name">Middle Name:</label> </td> <td><input type="text" id="middle_name" name="middle_name" value="<?php echo $middle_name; ?>" /> </td> </tr> I tried putting an if statement into the form field like so: <tr> <td><label for "middle_name">Middle Name:</label> </td> <td><input type="text" id="middle_name" name="middle_name" value="<?php if ($middle_name == NULL) echo $middle_name; ?>" /> </td> </tr> however, though the data is successfully updated, returns a null value in all instances. I'd like to have it return the actual value if possible. Any suggestions? (yes, I know I left out the curly brackets) I feel like I'm stuck in an endless loop
  12. Okay, I feel dumb. I finally figured out what was wrong... if you remove the quotes from the query, you need to re-enter them if there actually is a value, or the sql query freaks out. Here's what I used: if (!empty($_POST['middle_name'])) $middle_name = "'$middle_name'"; else $middle_name = 'NULL' ;
  13. Hi, sorry if this is a dumb question, but I've been searching for help for this issue for a while in vain. I'm trying to get my form to pass a real null value when there is nothing entered, but to pass along the correct value if the data has been entered. For example, I have a field for "middle_name" for individuals that is not required. Here is a simplified version of a query from one of my update screens "UPDATE person SET person_type = '$person_type' , last_name ='$last_name' , first_name = '$first_name' , middle_name = '$middle_name' " ; the PHP form inputs a blank character instead of an actual null value into the middle_name field. If I remove the single quote marks from this field, as below: "UPDATE person SET person_type = '$person_type' , last_name ='$last_name' , first_name = '$first_name' , middle_name = $middle_name " ; it transmits nulls fine, but if I try to include an entry for this field (for instance "John") I get an error stating: Error: Unknown column 'John' in 'field list' I have tried quite a few things, but mostly all they do is enter a text string of "NULL" into the database, which of course is not the same thing as a null value. for instance this if (!empty($_POST['middle_name'])) $middle_name = $_POST['middle_name']; else $middle_name = "NULL"; just enters "NULL" into the database. If I try changing the variable to $middle_name = "NULL"; it just errs. Can someone help?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.