CloudBreaker Posted August 4, 2015 Share Posted August 4, 2015 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> Quote Link to comment https://forums.phpfreaks.com/topic/297635-selection-in-while-loop/ Share on other sites More sharing options...
requinix Posted August 4, 2015 Share Posted August 4, 2015 Since you're using a form with method="post" then the information you want will be in the $_POST superglobal array. if ($_SERVER["REQUEST_METHOD"] == "POST") { // if you submitted the form echo $_POST["first_name"]; Quote Link to comment https://forums.phpfreaks.com/topic/297635-selection-in-while-loop/#findComment-1518028 Share on other sites More sharing options...
NotionCommotion Posted August 4, 2015 Share Posted August 4, 2015 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'])); Quote Link to comment https://forums.phpfreaks.com/topic/297635-selection-in-while-loop/#findComment-1518029 Share on other sites More sharing options...
ginerjm Posted August 4, 2015 Share Posted August 4, 2015 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/297635-selection-in-while-loop/#findComment-1518030 Share on other sites More sharing options...
CloudBreaker Posted August 5, 2015 Author Share Posted August 5, 2015 (edited) 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 August 5, 2015 by CloudBreaker Quote Link to comment https://forums.phpfreaks.com/topic/297635-selection-in-while-loop/#findComment-1518049 Share on other sites More sharing options...
Ch0cu3r Posted August 5, 2015 Share Posted August 5, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/297635-selection-in-while-loop/#findComment-1518056 Share on other sites More sharing options...
CloudBreaker Posted August 5, 2015 Author Share Posted August 5, 2015 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> Quote Link to comment https://forums.phpfreaks.com/topic/297635-selection-in-while-loop/#findComment-1518115 Share on other sites More sharing options...
CloudBreaker Posted August 6, 2015 Author Share Posted August 6, 2015 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> Quote Link to comment https://forums.phpfreaks.com/topic/297635-selection-in-while-loop/#findComment-1518123 Share on other sites More sharing options...
Barand Posted August 6, 2015 Share Posted August 6, 2015 String values in a query need to be inside single quotes $sql = "UPDATE `hsa_project_hub`.`rfis` SET `answered_by` = '$answeredBy', `time` = NOW() WHERE `rfis`.`no` = 23;"; Quote Link to comment https://forums.phpfreaks.com/topic/297635-selection-in-while-loop/#findComment-1518132 Share on other sites More sharing options...
Solution CloudBreaker Posted August 6, 2015 Author Solution Share Posted August 6, 2015 All contributed....no one best answer for this one. Thanks everyone. Quote Link to comment https://forums.phpfreaks.com/topic/297635-selection-in-while-loop/#findComment-1518145 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.