jamieh Posted May 27, 2008 Share Posted May 27, 2008 So i'm trying to protect an admin page i'm going to be using which will be able to view signups etc. I found a tutorial on pixel2life.com which helps me do just this, here is the error i am recieving: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home1/******/public_html/form/*****.php on line 8 Could not execute query : SELECT * from login where username='*****' and password='*****'. Here is my code: <?php session_start(); include "config.php"; mysql_select_db("*****", $con); $q="SELECT * from login where username='$username' and password='$pw'"; $result= mysql_query($q, $connection) or die ("Could not execute query : $q." . mysql_error()); if (mysql_num_rows($result) == 0) { echo "<div align=center><b>Oops! Your login is wrong. Please click back and try again.</b></div>"; } else { $r=mysql_fetch_array($result); $login_username=$r["username"]; session_register("login_username"); Header("Location: admin.php"); } ?> I don't have a clue what's wrong with it personally i have googled it and tryed searching on w3schools.com for an answer. Also i'm looking for some information with preg_match here is my code: if (preg_match("/^[a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_POST["email"]) === 0) { print "Invalid email<br>"; } if (preg_match("/^STEAM_[0-2]:[0-2]:[0-9]{1,10}$/", $_POST["steamid"]) === 0) { print "Invalid Steam ID<br>"; } if (preg_match("/^[5-9]{1}$/", $_POST["members"]) === 0) { print "Invalid amount of members"; } Is there a way so i can sort of knock that down a few lines surely theres a way to use just one command for all this? Also could anyone point me in the right direction using preg_match so i know if they've typed in a correct website address at all please? Many thanks, Best wishes. Jamie Link to comment https://forums.phpfreaks.com/topic/107498-few-mysql-errors-and-some-help-please/ Share on other sites More sharing options...
revraz Posted May 27, 2008 Share Posted May 27, 2008 Sounds like you don't have a connection to MySQL. What's in config.php? Link to comment https://forums.phpfreaks.com/topic/107498-few-mysql-errors-and-some-help-please/#findComment-551003 Share on other sites More sharing options...
jamieh Posted May 27, 2008 Author Share Posted May 27, 2008 <?php $con = mysql_connect('localhost', '******', '*****'); if (!$con) { die ('Could not connect to the Database : ' . mysql_error()); } $username = "*****"; $pw = "*****"; $username = htmlentities($username); $pw = htmlentities($pw); $email = $_POST['email']; $website = $_POST['website']; $members = $_POST['members']; $steamid = $_POST['steamid']; $teamname = $_POST['teamname']; $email = htmlentities($email); $website = htmlentities($website); $members = htmlentities($members); $steamid = htmlentities($steamid); $teamname = htmlentities($teamname); ?> I'm actually connecting to the same database twice from 2 different scripts (using the same username and password) but using 2 different tables.. would this make a difference? Thank you. Jamie Link to comment https://forums.phpfreaks.com/topic/107498-few-mysql-errors-and-some-help-please/#findComment-551020 Share on other sites More sharing options...
revraz Posted May 27, 2008 Share Posted May 27, 2008 Drop the $con from here and see if it helps mysql_select_db("*****", $con); Link to comment https://forums.phpfreaks.com/topic/107498-few-mysql-errors-and-some-help-please/#findComment-551029 Share on other sites More sharing options...
jamieh Posted May 27, 2008 Author Share Posted May 27, 2008 Same error my friend nothing. [EDIT] What i did notice though.. the error is now on line 10 rather than line 8? :/ [EDIT] Jamie Link to comment https://forums.phpfreaks.com/topic/107498-few-mysql-errors-and-some-help-please/#findComment-551031 Share on other sites More sharing options...
revraz Posted May 27, 2008 Share Posted May 27, 2008 Oops, told you wrong line. You use $con but also use $connection $result= mysql_query($q, $connection) or die drop $connection Link to comment https://forums.phpfreaks.com/topic/107498-few-mysql-errors-and-some-help-please/#findComment-551037 Share on other sites More sharing options...
jamieh Posted May 27, 2008 Author Share Posted May 27, 2008 So swap $connection with $con? Ok i did that now i come up with this error unfortunetly Could not execute query : SELECT * from login where username='***' and password='***'.No database selected Thansk for the help. Jamie Link to comment https://forums.phpfreaks.com/topic/107498-few-mysql-errors-and-some-help-please/#findComment-551104 Share on other sites More sharing options...
jamieh Posted May 28, 2008 Author Share Posted May 28, 2008 Anyone please? Thanks, Jamie Link to comment https://forums.phpfreaks.com/topic/107498-few-mysql-errors-and-some-help-please/#findComment-551638 Share on other sites More sharing options...
craygo Posted May 28, 2008 Share Posted May 28, 2008 just put your connection all in one place will be much easier. <?php $dbhost = 'localhost'; // database server $dbuser = 'xxxx'; // db username $dbpass = 'xxxxx'; // db password $dbname = 'xxxxx'; // db name $mysql_conn = @mysql_connect($dbhost, $dbuser, $dbpass) or die("Could not connect to Mysql, Please check host/username/password settings and try again"); @mysql_select_db($dbname, $mysql_conn) or die("Could not connect to DataBase. Check DB name"); ?> Also once the connection is made there is no need to include the connection in the query unless you are changing connections $q="SELECT * from login where username='$username' and password='$pw'"; $result= mysql_query($q) or die ("Could not execute query : $q." . mysql_error()); Ray Link to comment https://forums.phpfreaks.com/topic/107498-few-mysql-errors-and-some-help-please/#findComment-551648 Share on other sites More sharing options...
jamieh Posted May 28, 2008 Author Share Posted May 28, 2008 Should i still use config.php to keep my login username and password secure though? Thanks Ray my friend this worked a treat. Jamie Link to comment https://forums.phpfreaks.com/topic/107498-few-mysql-errors-and-some-help-please/#findComment-551661 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.