Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP, question about searching and updating a record
    primarykey
    data
    text
    <p>I have a question, I am new to PHP and I have been working on some exercises. The one I am currently working on is to create a simple form that will search a database (first name, last name). The returned results should then be populated into another form. This way, if I want to update the record, all I have to do is change the value of the populated form and hit Update. I have create the database no problem.</p> <p>The following is the code. (Please don't laugh, I'm very new...I am sure there are much more efficient ways of doing this, but I'm just playing around right now)</p> <p>Here is the form: </p> <pre><code>&lt;form action="" method="post"&gt; &lt;strong&gt;Search for name&lt;/strong&gt;&lt;br&gt; &lt;label for="fname"&gt;First Name&lt;/label&gt; &lt;input type="text" name="fname"&gt; &lt;label for="lname"&gt;Last Name&lt;/label&gt; &lt;input type="text" name="lname"&gt; &lt;input type="submit" name="submit" value="Search"&gt; &lt;/form&gt; </code></pre> <p>And here is the PHP: </p> <pre><code>if( isset( $_POST['submit'] ) ){ $first_name = $_POST['fname']; $last_name = $_POST['lname']; if ( $first_name == NULL || $last_name == NULL ) { echo "please enter search record"; } else { $query = "SELECT first_name, last_name FROM formdata WHERE first_name LIKE '%$first_name%' OR last_name LIKE '%$last_name%'"; $result = mysqli_query( $conn, $query ); $result_array = mysqli_fetch_row( $result ); $fname_value = $result_array[0]; $lname_value = $result_array[1]; echo " &lt;form action='' method='post'&gt;\n &lt;label for='fname_u'&gt;First Name&lt;/label&gt;\n &lt;input type='text' name='fname_u' value='$fname_value'&gt;\n &lt;label for='lname_u'&gt;Last Name&lt;/label&gt;\n &lt;input type='text' name='lname_u' value='$lname_value'&gt;\n &lt;input type='submit' name='update' value='Update'&gt;\n &lt;/form&gt;"; } } if( isset( $_POST['update'] ) ) { $first_name_u = ( $_POST['fname_u'] ); $last_name_u = ( $_POST['lname_u'] ); $query_update = "UPDATE formdata SET first_name = '$first_name_u', last_name = '$last_name_u' WHERE first_name = '$fname_value';"; echo $query_update; // this is just for testing } </code></pre> <p>This code seems to work and do what I want, all the way up to when I submit the updated information. I can't figure out how to carry over the value of the $fname_value variable to the if( isset( $_POST['update'] ) ) conditional. I am thinking I can't because they are two different POSTS? I really don't know...I just need to find a way to get value of the retrieved form data and use for the WHERE clause.</p> <p>Again, I'm very new and just getting my feet wet with this kind of stuff ... Any help would be great</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload