taking 2 values with one query?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Gleeb
Forum Commoner
Posts: 87
Joined: Tue May 13, 2003 7:01 am
Location: UK
Contact:

Post by Gleeb »

I ran into this problem myself recently.

When you're addressing an associative array as part of a string (such as you are doing) you need to use $var[index] instead of $var['index']

Stupid, eh?
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

fixed it

Post by irealms »

managed to get round that by changing it to:

$email = $row['email'];
$pass = $row['passwd'];
$from = "from: crimson@irealms.co.uk \r\n";
$mesg = "your password is $pass \r\n";
echo "<fieldset><legend>Mail sent to</legend> $email</fieldset>";
mail($email, $from, $mesg) or die('mail not sent');
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

ok got it )

Post by irealms »

i did as you suggested and made the access fields inside the already existing user table. I want them to be set at 0 initially and later an admin interface will be set to change them. I made the fields CHAR length 1 default 0

When inserting do i leave out the value for id as it auto increments?

ie.

$db_conn = mysql_connect("localhost", "cadmin", "cpass") or die("unable to connect to the database");
mysql_select_db("crimson", $db_conn) or die("unable to select the database");
$query = "insert into user(username,passwd,charname,email) values ($_POST['username'],$_POST['passwd'],$_POST['charname'],$_POST['email'])";
$result = mysql_query($query, $db_conn) or die("query failed");


atm i tried that and i'm getting

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in /home/httpd/vhosts/irealms.co.uk/httpdocs/crimson/register.php on line 24


again, does this mean i need to do $_POST[username] instead of $_POST['username'] ?
Gleeb
Forum Commoner
Posts: 87
Joined: Tue May 13, 2003 7:01 am
Location: UK
Contact:

Post by Gleeb »

You're on the ball today :) Yeah, coz you're putting the variables inside a string, and the ' is playing with it's mind.

Be kind to PHP :P
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

Post by irealms »

atm i have

$db_conn = mysql_connect("localhost", "cadmin", "cpass") or die("unable to connect to the database");
mysql_select_db("crimson", $db_conn) or die("unable to select the database");
$query = "insert into user(username,passwd,charname,email) values ($_POST[username],$_POST[passwd],$_POST[charname],$_POST[email])";
$result = mysql_query($query, $db_conn) or die("query failed");

and i've got a query failed error , though i'm not sure why


btw your help is really appreciated sorry for the noob questions.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

(

Post by irealms »

k i've done what it says in the post and i still get query failed for some reason with this code now:

Code: Select all

$username = $_POST&#1111;'username'];
	$passwd = $_POST&#1111;'passwd'];
	$charname = $_POST&#1111;'charname'];
	$email = $_POST&#1111;'email'];
$db_conn = mysql_connect("localhost", "cadmin", "cpass") or die("unable to connect to the database");
  mysql_select_db("crimson", $db_conn) or die("unable to select the database");
  $query = "INSERT into user(username, passwd, charname, email) values ('$username', '$passwd', '$charname', '$email')";
$result = mysql_query($query, $db_conn) or die("query failed");
can't see the problem with the query line
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

$result = mysql_query($query, $db_conn) or die("query [$query] failed: ".mysql_error());
Gleeb
Forum Commoner
Posts: 87
Joined: Tue May 13, 2003 7:01 am
Location: UK
Contact:

Post by Gleeb »

twigletmac wrote:viewtopic.php?t=8726
Volka's a gal? :P
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

nope, but maybe twigletmac is ;)
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

k

Post by irealms »

i inserted some test info and got this error


query [INSERT into user(username, passwd, charname, email) VALUES ('user1', 'user1', 'user', 'user')] failed: You have an error in your SQL syntax near 'user(username, passwd, charname, email) VALUES ('user1', 'user1', 'user', 'user'' at line 1
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

ah, right user is a reservered word for mysql (in fact it's a function, http://www.mysql.com/doc/en/Miscellaneous_functions.html#IDX1337)
try

Code: Select all

$query = "INSERT into `user` (username, passwd, charname, email) values ('$username', '$passwd', '$charname', '$email')";
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

connect twice?

Post by irealms »

also do i need to give connect info twice in the page or will just the once work?

<?
if (!$_POST['username'] || !$_POST['passwd'] || !$_POST['charname'] || !$_POST['email'])
echo "Please complete all fields before submitting your registration.";
elseif (strlen($_POST['username'])<4 || strlen($_POST['passwd'])<4)
echo "<div class=\"log\">Your username and password must be greater than 4 characters.";
elseif (strlen($_POST['username'])>16 || strlen($_POST['passwd'])>16)
echo "<div class=\"log\">Your username and password must be less than 16 characters.";
elseif ($_POST['username'])
{
$db_conn = mysql_connect("localhost", "cadmin", "cpass") or die("unable to connect to the database");
mysql_select_db("crimson", $db_conn) or die("unable to select the database");
$checkname = $_POST['username'];
$name_check = mysql_query("SELECT username FROM user WHERE username='$checkname'") or die(mysql_error());
if (mysql_num_rows($name_check) == 1)
echo "user already exists, please choose a different username.";
else
{
$username = $_POST['username'];
$passwd = $_POST['passwd'];
$charname = $_POST['charname'];
$email = $_POST['email'];
$db_conn = mysql_connect("localhost", "cadmin", "cpass") or die("unable to connect to the database");
mysql_select_db("crimson", $db_conn) or die("unable to select the database");
$query = "INSERT into user(username, passwd, charname, email) VALUES ('$username', '$passwd', '$charname', '$email')";
$result = mysql_query($query, $db_conn) or die("query [$query] failed: ".mysql_error());
}
}
?>

i removed the 2 connect areas and placed it in a a config file, seems to work but will this cause problems or is it ok to do this?
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

(

Post by irealms »

still got an error the same one in fact, should i just rename the table?


i have used querys to this table bfore like

$query = "update user
set passwd = '$new_passwd'
where username = '$valid_user'";
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

You can use `'s to get rid of that problem.

$query = "update `user`
set `passwd` = '$new_passwd'
where `username` = '$valid_user'";
Post Reply