dapcigar Posted February 21, 2016 Share Posted February 21, 2016 Hello all, Am trying to create a simple feedback form and also save the answers of each questions. i manage to get the system to save the answers but am not getting the id of my questions saved.. My code below <?php if($_POST) { $answer = $_POST['answer']; $hobb = $_POST['comp']; foreach($hobb as $key=>$val) { $var1=$hobb[$key]; $var2=$answer[$key]; include('manpower_db.php'); $table = "INSERT INTO answer (question_id,answer) ". "VALUES ('$var1','$var2')"; mysql_query($table) or die(mysql_error()); $inserted_fid = mysql_insert_id(); mysql_close(); } echo'<script>alert("Inserted Successfully")</script>'; } ?> <?php //session_start(); include('manpower_db.php'); $sql = mysql_query("SELECT * FROM question") or die(mysql_error()); $i = '1'; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <form action="" method="post"> <H2> How Will You Rate This Service</H2> <?php while($row = mysql_fetch_array($sql)) { echo $row['question']; $comp = $row['id'][$i]; // $_SESSION['comp']= $comp; ?> <br /> <?php echo "<td> <input type='radio' name='answer[".$row['id']."]' value='1' >1"; echo "<td> <input type='radio' name='answer[".$row['id']."]' value='2' >2"; echo "<td> <input type='radio' name='answer[".$row['id']."]' value='3' >3"; echo "<td> <input type='radio' name='answer[".$row['id']."]' value='4' >4"; echo "<td> <input type='radio' name='answer[".$row['id']."]' value='5' >5"; echo "<input type='hidden' name='comp[".$row['id']."]' value=$comp >"; echo'<br/>'; $i++; } ?> <br/> <strong> NOTE:</strong> 1 - Very Poor, 2 - poor, 3 - Okay, 4 - Fair, 5 - Good <p> <input name="submit" type="submit" value="submit" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 21, 2016 Share Posted February 21, 2016 (edited) I have no idea what you mean by "not getting the id of my questions saved" since you don't show us any code involving "saving". One thing that puzzles me is this: $comp = $row['id'][$i]; The result of a fetch is a one-dimensional array. You are trying to use it with two. Try turning on php error checking (in my sign.) and see what error message(s) you get. As for your question - please elaborate. Upon Further Review: You are placing your radio buttons in td elements but you have NOT built a proper html table. Is this the problem you are having - simply not SEEING the radio buttons with the id values you have assigned? Edited February 21, 2016 by ginerjm Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 21, 2016 Share Posted February 21, 2016 (edited) Upon EVER More Further Review: I now see the insert. Don't know how I missed it. That leads to the question - you're showing us your process of some input, but not the actual html producing that input. Can we not see the incoming form for this script? The form you are sending out here I assumed was the one you are using for future input, but I see now that it is not. Edited February 21, 2016 by ginerjm Quote Link to comment Share on other sites More sharing options...
dapcigar Posted February 21, 2016 Author Share Posted February 21, 2016 am getting the questions from the DB and displaying each question with the radio button options. <?php //session_start(); include('manpower_db.php'); $sql = mysql_query("SELECT * FROM question") or die(mysql_error()); $i = '1'; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <form action="" method="post"> <H2> How Will You Rate This Service</H2> <?php while($row = mysql_fetch_array($sql)) { echo $row['question']; $comp = $row['id'][$i]; // $_SESSION['comp']= $comp; ?> <br /> <?php echo "<td> <input type='radio' name='answer[".$row['id']."]' value='1' >1"; echo "<td> <input type='radio' name='answer[".$row['id']."]' value='2' >2"; echo "<td> <input type='radio' name='answer[".$row['id']."]' value='3' >3"; echo "<td> <input type='radio' name='answer[".$row['id']."]' value='4' >4"; echo "<td> <input type='radio' name='answer[".$row['id']."]' value='5' >5"; echo "<input type='hidden' name='comp[".$row['id']."]' value=$comp >"; echo'<br/>'; $i++; } ?> <br/> <strong> NOTE:</strong> 1 - Very Poor, 2 - poor, 3 - Okay, 4 - Fair, 5 - Good <p> <input name="submit" type="submit" value="submit" /> </form> </body> </html> the other part is to save the result . <?php session_start(); if($_POST) { $answer = $_POST['answer']; $hobb = $_POST['comp']; foreach($hobb as $key=>$val) { $var1=$hobb[$key]; $var2=$answer[$key]; include('manpower_db.php'); $table = "INSERT INTO answer (question_id,answer) ". "VALUES ('$var1','$var2')"; mysql_query($table) or die(mysql_error()); $inserted_fid = mysql_insert_id(); mysql_close(); } echo'<script>alert("Inserted Successfully")</script>'; } ?> Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 21, 2016 Share Posted February 21, 2016 Now that I understand. So the value of $comp might be not what you want. As I see it the key value of your 'answer[]' array is the id you want. Yet you are using the comp input which I believe you have appeneded an extra value to. $comp = $row['id'][$i] is going to give you this assume id='1' from your table and $i is 3. $comp will end up being invalid since $row is not a 2 dimensional array. Skip the comp element and just use the $k value you already have. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted February 21, 2016 Share Posted February 21, 2016 The code makes no sense whatsoever. I recommend you start with the basics of PHP before you jump to more advanced topics like form processing. How to write valid HTML markup. How to escape data for HTML contexts. How to access a MySQL database with PHP. How to make PHP show errors. assume id='1' from your table and $i is 3. $comp will end up being invalid since $row is not a 2 dimensional array. $row['id'] is a string, so $row['id'][$i] is technically valid -- until you hit the string limit. 1 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 21, 2016 Share Posted February 21, 2016 sure looks like a 2-dimensional array reference to me. But yes I see what you mean. I certainly don't think the OP ever thought of it that way though! Quote Link to comment 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.