Jump to content

Selection in while loop


CloudBreaker
Go to solution Solved by CloudBreaker,

Recommended Posts

I can see and select these values from the data base, however I can't make that value stick.  Once I hit submit, nothing is really set is it?  I would just like to echo my chosen selection.

 

thanks,

 

CB

<?php
//Open Connection
          $conn = mysqli_connect("localhost","root","","hsa_project_hub");
//Gather Information
      $sql = "SELECT * FROM user_project WHERE project_id=215016";
      $result = mysqli_query($conn,$sql) or die (mysql_error());
?>    
<form action="selectionTest.php" method="post">
        <p>Final Answer by:
        <select name="first_name" >
            <option value="" >--Select--</option>
            <!-- Loop For Fetch Result -->        
            <?php while($row = mysqli_fetch_array($result) ) : ?>
             <option value="first_name:" ><?php echo($row['first_name']) , ' ', ($row['last_name']);?></option>
            <?php endwhile; ?> 
            <!-- End Loop for Fetch Result -->
    </select>
    </p>
    <input type="submit" value="Complete"/>
</form>
Link to comment
Share on other sites

In selectionTest.php, add the following to the top of the page.

 



echo('<pre>'.print_r($_POST,1).'</pre>');


 

You see your input values?  Note that project_id is likely not shown, so you will need to add it as a hidden input in your form.

 

Now you need to save them in the database.  

 



$stmt=$conn->pdo('INSERT INTO user_project(first_name) VALUES(?) WHERE id=?');
$stmt->execute(array($_POST['first_name'],$_POST[' project_id']));

Link to comment
Share on other sites

You have to provide a unique value for your options otherwise whatever is clicked will return the same value "first name:". When a select tag is setup properly the $_POST array will return something as $_POST['first_name'] which will match one of the values you specify in one of the option tags. No matter which item is selected currently you will get back "first name:" every time.

  • Like 1
Link to comment
Share on other sites

You have to provide a unique value for your options otherwise whatever is clicked will return the same value "first name:". When a select tag is setup properly the $_POST array will return something as $_POST['first_name'] which will match one of the values you specify in one of the option tags. No matter which item is selected currently you will get back "first name:" every time.

 

...and that's exactly what's happening.  Below is the joined table I'm pulling from.  Chuck, Jack and Bob are all showing up properly in the drop down per the project_id they belong to, however I'm confused when you say to provide a "unique value" for my options.  Do you mean the "id's" of the names?    I'm confused because the names are already there, and I just having trouble with the methodology of getting the chosen name to stick, because this after this small script is successfully executed it will be redirected to the rfi list where the user with the final answer will be listed as show in the screen capture link.   This entire little project is made up for my learning purposes and I keep running into these little road blocks that are driving me nuts.  Thanks for all your help guys.

 

 

https://drive.google.com/file/d/0B06KJO0YEuzxc0xMOVRPSlBtSGM/view?usp=sharing

 

https://drive.google.com/file/d/0B06KJO0YEuzxUk9KaFlaZGZYWW8/view?usp=sharing

Edited by CloudBreaker
Link to comment
Share on other sites

What ginerjm is referring to is this (highlighted in red)

<option value="first_name:" ><?php echo($row['first_name']) , ' ', ($row['last_name']);?></option>

 

This is given all the names in your select menu the same value when the form is submitted. If you want the persons name to be submitted then remove the value attribute from the option tag.

Link to comment
Share on other sites

What ginerjm is referring to is this (highlighted in red)

<option value="first_name:" ><?php echo($row['first_name']) , ' ', ($row['last_name']);?></option>

 

This is given all the names in your select menu the same value when the form is submitted. If you want the persons name to be submitted then remove the value attribute from the option tag.

 

The selected name now shows up up in the array.   All that needs to be done now is to take the selection from the array and insert it into the rfi's table "answered_by" row.   I'm having no luck on line 27.

<?php
//Open Connection
          $conn = mysqli_connect("localhost","root","","hsa_project_hub");
//Gather Information
      $sql = "SELECT * FROM user_project WHERE project_id=215016";
      $result = mysqli_query($conn,$sql) or die (mysql_error());
	  
	  echo('<pre>'.print_r($_POST,1).'</pre>');
	 
	  	  
	  mysqli_close($conn);
?>    
<form action="selectionTest.php" method="post">
        <p>Final Answer by:
        <select name="first_name" >
            <option value="" >--Select--</option>
            <!-- Loop For Fetch Result -->        
            <?php while($row = mysqli_fetch_array($result) ) : ?>
             <option><?php echo($row['first_name']) , ' ', ($row['last_name']);?></option>
            <?php endwhile; ?> 
            <!-- End Loop for Fetch Result -->
    </select>
    </p>
	
<?php
		$stmt="INSERT INTO rfis(answered_by) VALUES(?) WHERE no=?'";
		$stmt->execute (array($_POST['first_name']));
		

		
?>
	
    <input type="submit" value="Complete"/>
</form>
Link to comment
Share on other sites

I'm almost there...I keep getting this error, and I don't see anything wrong with the UPDATE statement.

 

Error: UPDATE `hsa_project_hub`.`rfis` SET `answered_by` = Bob Smith, `time` = NOW() WHERE `rfis`.`no` = 23;
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Smith, `time` = NOW() WHERE `rfis`.`no` = 23' at line 1

<?php
//Open Connection
          $conn = mysqli_connect("localhost","root","","hsa_project_hub");
//Gather Information
      $sql = "SELECT * FROM user_project WHERE project_id=215016";
      $result = mysqli_query($conn,$sql) or die (mysql_error());
	  
	  if ($_SERVER["REQUEST_METHOD"] == "POST") { // if you submitted the form
	$answeredBy =  $_POST["first_name"];
	  }
	  	 
	$sql = "UPDATE `hsa_project_hub`.`rfis` SET `answered_by` = $answeredBy, `time` = NOW() WHERE `rfis`.`no` = 23;";
		
		if (mysqli_query($conn, $sql)) {
		echo "Name entered successfully";
		} else {
		echo "Error: " . $sql . "<br>" . mysqli_error($conn);
		}
	
	
	
	  mysqli_close($conn);
?>    
<form action="selectionTest.php" method="post">
        <p>Final Answer by:
        <select name="first_name" >
            <option value="" >--Select--</option>
            <!-- Loop For Fetch Result -->        
            <?php while($row = mysqli_fetch_array($result) ) : ?>
             <option><?php echo($row['first_name']) , ' ', ($row['last_name']);?></option>
            <?php endwhile; ?> 
            <!-- End Loop for Fetch Result -->
    </select>
    </p>
	

	
    <input type="submit" value="Complete"/>
</form>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.