简体   繁体   中英

PHP and MySQL query based on user input not working

I am creating a users database where there are 4 fields: ID, username, password, and occupation. This is a test database. I tried querying the db table and it worked but i have a lot of trouble having a user input and a MySQL query based off of it. I run an Apache server in Linux (Debian, Ubuntu).

I have 2 pages. The first one is a bare-bone test index page. this is where there are textboxes for people to input easy info to register their info in the db. Here is the code for 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 the submit button is clicked. It goes to the reg.php file. This is where it gets complicated. The page goes blank!!! Nothing is displayed or inputted in the db. Normal queries work well, but when user interaction is added, something is wrong. Here is the code for 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. We Have A Problem Here: ' . mysql_error()); } if ($link){ echo 'connected succesfully'; } mysql_select_db("forum") or die(' Oops. We Have A Problem Here: ' . mysql_error()); $data = mysql_query("INSERT INTO users (username, password, occupation) VALUES ('{$u}', '{$p}', '{$o}')"); ?> 

Can anyone hep me to correct this code to make this work? Thank you so much for your time. Much appreciated.

EDIT: I noticed that i did not add semicolons in the first 3 lines. after doing so i got this error: "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 '{'', '', '')' at line 1." Can someone explain why?

EDIT: the website is just on my local machine... on an apache server on linux

You are missing semi-colons in the first three lines.

$un = $_POST["u"];
$pk = $_POST["p"];
$ok = $_POST["o"];

mysql_real_escape_string() requires a db connection. Try this ....

<?php
$un = $_POST["u"];
$pk = $_POST["p"];
$ok = $_POST["o"];

$link = mysql_connect('localhost', 'root', 'randompassword');
if (!$link){
  die(' Oops. We Have A Problem Here: ' . mysql_error());
}

if ($link){
  echo 'connected succesfully';
}

mysql_select_db("forum") or die(' Oops. We Have A Problem Here: ' . mysql_error());

$u = mysql_real_escape_string($un);
$p = mysql_real_escape_string($pk);
$o = mysql_real_escape_string($ok);
$sql = "INSERT INTO users (username, password, occupation) VALUES ('$u', '$p', '$o')";
$ins_sql = mysql_query($sql);
IF($ins_sql) {
  echo 'Inserted new record.';
}ELSE{
  echo 'Insert Failed.';
}
?>

Try adding this to the top of your script:

error_reporting(E_ALL);
ini_set("display_errors", 1);

This way you will see all errors that you made syntactically or even within your SQL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM