Onions Posted April 3, 2012 Share Posted April 3, 2012 Hello all! I have been playing about with PHP, trying to make a simple registration form. The code below should connect to a MySQL database. If no information has been passed to it (via POST) then it will present a form to register. All the details put into it are then sent to a table in a database. Otherwise, it will say "Thank you for registering". The problem is, when I upload the .php file to my server, I get the error Parse error: syntax error, unexpected $end in harryrabbit.co.uk/electronics/misc/mysql-connect.php on line 36 This makes no sense as line 36 is the "?> to close the PHP script. I thought the heredoc used for echoing the form HTML might be the cause of the problem, however the same error occurs when the whole load of HTML is condensed into one line and echoed by echo "[the HTML]"; What is going wrong and how can I fix it? Onions. <?php require_once 'credentials.php'; $server = mysql_connect($db_hostname, $db_username, $db_password); if(!$server) die("Unable to connect to the database: " . mysql_error()); mysql_select_db($db_database) or die("Unable to select database: " . mysql_error()); echo 'Connected!'; if(isset($_POST['username']) && isset($_POST['password']) && isset($_POST['pplink'])){ $username = get_post('username'); $password = get_post('password'); $pplink = get_post('pplink' ); $query = "INSERT INTO users VALUES" . "('$username', '$password', '$pplink')"; if(!mysql_query($query, $db_server)) echo "Registration failed: " . mysql_error(); else{ echo "Thankyou for registering"; } echo <<<END <form action='mysql-connect.php' method='post'> Username <input type='text' name='username' /><br /> Password <input type='password' name='password' /><br /> Picture <input type='text' name='pplink' /><br /> <input type='submit' value='Sign up!' /><br /><br /> Please enter your desired username and password. <br /> 'Picture' is the link to your desired profile picture. We reccomend uploading the picture to imgur. Pictures must be no larger than 100 X 100.<br /> </form> END; ?> Quote Link to comment Share on other sites More sharing options...
NLT Posted April 3, 2012 Share Posted April 3, 2012 Try this: <?php require_once 'credentials.php'; $server = mysql_connect($db_hostname, $db_username, $db_password); if(!$server) die("Unable to connect to the database: " . mysql_error()); mysql_select_db($db_database) or die("Unable to select database: " . mysql_error()); echo 'Connected!'; if(isset($_POST['username']) && isset($_POST['password']) && isset($_POST['pplink'])){ $username = get_post('username'); $password = get_post('password'); $pplink = get_post('pplink' ); $query = "INSERT INTO users VALUES" . "('$username', '$password', '$pplink')"; if(!mysql_query($query, $db_server)) echo "Registration failed: " . mysql_error(); else{ echo "Thankyou for registering"; } } echo <<<END <form action='mysql-connect.php' method='post'> Username <input type='text' name='username' /><br /> Password <input type='password' name='password' /><br /> Picture <input type='text' name='pplink' /><br /> <input type='submit' value='Sign up!' /><br /><br /> Please enter your desired username and password. <br /> 'Picture' is the link to your desired profile picture. We reccomend uploading the picture to imgur. Pictures must be no larger than 100 X 100.<br /> </form> END; ?> You forgot a curly bracket to close the first if() Quote Link to comment Share on other sites More sharing options...
Onions Posted April 3, 2012 Author Share Posted April 3, 2012 Thanks for that - it works perfectly. I was searching for ages to find out what was wrong! Quote Link to comment Share on other sites More sharing options...
NLT Posted April 3, 2012 Share Posted April 3, 2012 Click Solved if your issue has been sorted. Quote Link to comment Share on other sites More sharing options...
Onions Posted April 3, 2012 Author Share Posted April 3, 2012 Click Solved if your issue has been sorted. I was looking for it, but I couldn't find the button to click. As it happens, I have another problem Once a set of data is put into the form, it should add the data to a MySQL database. However, I get this error: supplied argument is not a valid MySQL-Link resource It is referring to these lines of code, which should extract the sent data, create a query then put the data into a table. Instead, it gives the error message above. $username = $_POST['username']; $password = $_POST['password']; $pplink = $_POST['pplink']; $query = "INSERT INTO users VALUES " . "('$username', '$password', '$pplink')"; if(!mysql_query($query, $db_server)) echo "Registration failed: " . mysql_error(); else{ echo "Thankyou for registering"; } My assumption is that the query being put together doesn't make sense to MySQL, however I haven't had enough experience of the language to know what I've done wrong this time Any hints? Thanks again, Onions. Quote Link to comment Share on other sites More sharing options...
NLT Posted April 3, 2012 Share Posted April 3, 2012 Click Solved if your issue has been sorted. I was looking for it, but I couldn't find the button to click. As it happens, I have another problem Once a set of data is put into the form, it should add the data to a MySQL database. However, I get this error: supplied argument is not a valid MySQL-Link resource It is referring to these lines of code, which should extract the sent data, create a query then put the data into a table. Instead, it gives the error message above. $username = $_POST['username']; $password = $_POST['password']; $pplink = $_POST['pplink']; $query = "INSERT INTO users VALUES " . "('$username', '$password', '$pplink')"; if(!mysql_query($query, $db_server)) echo "Registration failed: " . mysql_error(); else{ echo "Thankyou for registering"; } My assumption is that the query being put together doesn't make sense to MySQL, however I haven't had enough experience of the language to know what I've done wrong this time Any hints? Thanks again, Onions. What I can see is that you haven't executed a query. Try using: $query = "INSERT INTO users VALUES ('{$username}', '{$password}', '{$pplink}')"; Then changing this line: if(!mysql_query($query, $db_server)) To: if(!$query) Let us see if you get any joy. Also, echo out this line to see if everything is parsing correctly: $query = "INSERT INTO users VALUES ('{$username}', '{$password}', '{$pplink}')"; Quote Link to comment Share on other sites More sharing options...
Onions Posted April 3, 2012 Author Share Posted April 3, 2012 It's working perfectly now, thanks again! I'll probably be back on with another problem shortly, but it's working smoothly for the minute! Quote Link to comment Share on other sites More sharing options...
NLT Posted April 3, 2012 Share Posted April 3, 2012 It's working perfectly now, thanks again! I'll probably be back on with another problem shortly, but it's working smoothly for the minute! That's what we're here for. 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.