Jump to content

Few MySQL Errors And Some Help Please?!


jamieh

Recommended Posts

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

<?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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.