html - PHP and MySQL query based on user input not working -
i creating users database there 4 fields: id, username, password, , occupation. test database. tried querying db table , worked have lot of trouble having user input , mysql query based off of it. run apache server in linux (debian, ubuntu).
i have 2 pages. first 1 bare-bone test index page. there textboxes people input easy info register info in db. here code it:
<html> <form action="reg.php" method="post"> username: <input type="text" name="u">password: <input type="password" name="p">occupation: <input type="text" name="o"> <input type="submit" value="register"> </form> </html>
after submit button clicked. goes reg.php file. gets complicated. page goes blank!!! nothing displayed or inputted in db. normal queries work well, when user interaction added, wrong. here code reg.php:
<?php $un = $_post["u"] $pk = $_post["p"] $ok = $_post["o"] $u = mysql_real_escape_string($un); $p = mysql_real_escape_string($pk); $o = mysql_real_escape_string($ok); $link = mysql_connect('localhost', 'root', 'randompassword'); if (!$link){ die(' oops. have problem here: ' . mysql_error()); } if ($link){ echo 'connected succesfully'; } mysql_select_db("forum") or die(' oops. have problem here: ' . mysql_error()); $data = mysql_query("insert users (username, password, occupation) values ('{$u}', '{$p}', '{$o}')"); ?>
can hep me correct code make work? thank time. appreciated.
edit: noticed did not add semicolons in first 3 lines. after doing got error: "you have error in sql syntax; check manual corresponds mysql server version right syntax use near '{'', '', '')' @ line 1." can explain why?
edit: website on local machine... on apache server on linux
mysql_real_escape_string()
requires db connection. try ....
<?php $un = $_post["u"]; $pk = $_post["p"]; $ok = $_post["o"]; $link = mysql_connect('localhost', 'root', 'randompassword'); if (!$link){ die(' oops. have problem here: ' . mysql_error()); } if ($link){ echo 'connected succesfully'; } mysql_select_db("forum") or die(' oops. have problem here: ' . mysql_error()); $u = mysql_real_escape_string($un); $p = mysql_real_escape_string($pk); $o = mysql_real_escape_string($ok); $sql = "insert users (username, password, occupation) values ('$u', '$p', '$o')"; $ins_sql = mysql_query($sql); if($ins_sql) { echo 'inserted new record.'; }else{ echo 'insert failed.'; } ?>
Comments
Post a Comment