PHP Array_$POST to SQL -
i trying current php code , insert database. able save first name, last name, , email unable rest of form data "gender", , "console" saved. here code
<!doctype html public> <html> <body> fill out following form: <table border="1" cellpadding="10"> <td> <h1> devices owned survey </h1> <form action="submit_answer.php" method = "post"> first name: <br /> <input type="text" name="first" /><br /> <br /> last name: <br /> <input type="text" name="last" /> <br /> <br /> email: <br /> <input type="text" name="email" /> <br /> <br /> <u>gender</u>: <br /> <br /> <input type="radio" name="gender" value="male" /> male<br /> <input type="radio" name="gender" value="female" /> female <br /> <br /> <u>i have following:</u> <br /> <br /> <input type="checkbox" name="console" value="playstation3" /> playstation 3<br /> <input type="checkbox" name="console" value="xbox360" /> xbox 360 <br /> <input type="checkbox" name="console" value="wii" /> wii <br /> <input type="checkbox" name="console" value="iphone" /> iphone <br /> <input type="checkbox" name="console" value="macbook" /> macbook <br /> <br /> <input type="submit"/> </form> </td> </table> </body> </html> php //sumbmit form <?php define('db_name', 'survey'); define('db_user', 'root'); define('db_password', 'xxxx'); define('db_host', 'localhost'); $link = mysql_connect(db_host, db_user, db_password); if (!$link) { die('could not connect: ' . mysql_error()); } $db_selected = mysql_select_db(db_name, $link); if (!$db_selected) { die ('cant\'t use' . db_name. ':' . mysql_error()); } echo 'connected sucessfully'; $first = $_post["first"]; // since method=”post” in form $last = $_post["last"]; $email = $_post["email"]; $gender = $_post["gender"]; $console = $_post["console"]; $sql = "insert survey (first, last, email) values ( '$_post[first]','$_post[last]','$_post[email]','$_post[gender]','$_post[console]')"; $result = mysql_query($sql); $result = mysql_query($sql) or die ("could not save record"); mysql_close(); ?> //also trying validate form each question answered
the method you're using save data db extremely risky. you're open sql injection attacks. being said, should read on sql injection attacks mysql_query
.
i'm not going rewrite code fix sql injection vulnerabilities, fix problem you're having...
in code have:
$sql = "insert survey (first, last, email) values ( '$_post[first]','$_post[last]','$_post[email]','$_post[gender]','$_post[console]')";
you're specifying 3 columns, passing in 5 columns. need add other 2 columns
$sql = "insert survey (first, last, email, gender, console) values ( '$_post[first]','$_post[last]','$_post[email]','$_post[gender]','$_post[console]')";
but seriously, change code!
edit:
if want take array of $_post['console']
, turn string has comma separated values, try this:
add array brackets name
attribute:
<input type="checkbox" name="console[]" value="playstation3" /> playstation 3<br /> <input type="checkbox" name="console[]" value="xbox360" /> xbox 360 <br /> <input type="checkbox" name="console[]" value="wii" /> wii <br /> <input type="checkbox" name="console[]" value="iphone" /> iphone <br /> <input type="checkbox" name="console[]" value="macbook" /> macbook <br />
iterate array , append values string:
<?php $consolearray = $_post['console']; $consolecommastring = ""; if ($consolearray != null && is_array($consolearray)) { foreach ($consolearray $consolevalue) { $consolecommastring .= $consolevalue .", "; } } $sql = "insert survey (first, last, email, gender, console) values ('$_post[first]','$_post[last]','$_post[email]','$_post[gender]','$consolecommastring')"; ?>
Comments
Post a Comment