Re: Eof not working correctly
- From: "Ken Robinson" <kenrbnsn@xxxxxxxxx>
- Date: 30 Jun 2005 22:31:17 -0700
adamsroy@xxxxxxxxxxxxx wrote:
> Hi, new to php trying to connect to a db check if certain records are
> there.
> Basically if the records aren't there it won't insert the records.
> heres the code
>
> mysql_connect($hostname_conn_mail, $username_conn_mail, '') or
> die("Could not connect");
> mysql_select_db($database_conn_mail) or die("Could not select
> database");
> $query="SELECT UserName,Password FROM cart_users WHERE
> UserName='".$UserName."' AND Password='".$cart."'";
Where are the variables $UserName and $cart being set?
> $rsCheckDetails = mysql_query($query)or die(mysql_error());
> $numrows = mysql_num_rows ($rsCheckDetails);
>
>
>
> if(!$rsCheckDetails->EOF||!$rsCheckDetails->BOF) {//heres the problem
> if i remove the "!" it will insert every time
What do you think the above "if" statement is supposed to be checking?
Shouldn't you be using
if ($numrows > 0) { // There are records, update them
> mysql_connect($hostname_conn_mail, $username_conn_mail, '') or
> die("Could not connect");
> mysql_select_db($database_conn_mail) or die("Could not select
> database");
Why are you reconnecting? You've already connected to your MySQL server
and selected the database.
> //insert cart name and userName into recent orders
> $query=("INSERT INTO cart_recentorders (UserName,cart_name) VALUES
> ('".$UserName."','".$cart."')");
When you assign a string to a varible you don't need the parenthesis "(
)". Also since you're using double quotes, you can write the above line
as
$query = "insert into cart_recentorders (UserName, cart_names) values
('$UserName','$cart')";
> mysql_query($query)or die(mysql_error());
>
> $query=("UPDATE cart_users SET cart_name= '".$cart."' WHERE UserName =
> '".$UserName."'");
The above comment applies here also.
> mysql_query($query)or die(mysql_error());
>
> }else{//no records found, insert them into users
> mysql_connect($hostname_conn_mail, $username_conn_mail, '') or
> die("Could not connect");
> mysql_select_db($database_conn_mail) or die("Could not select
> database");
See my comments above on reconnecting.
> $query="INSERT INTO cart_users
> (name,UserName,Password,address,postcode,country,phone,sendmail,cart_name)"
> ;
> $query=$query." VALUES
> ('".$name."','".$UserName."','".$cart."','".$baddr1.$baddr2."','".$bpostalcode."','".$bcountyprovince."','".$phone."','".$sendmail."','".$cart."')";
>
If you're adding characters to a variable, you can use the ".="
operator
$query .= ' values
('$name','$UserName','$cart','$baddr1.$baddr2','$bpostalcode');
> mysql_query($query1)or die(mysql_error());
>
> $query=("INSERT INTO cart_recentorders (UserName,cart_name) VALUES
> ('".$UserName."','".$cart."')");
> mysql_query($query)or die(mysql_error());
See my above comments on assigning strings to the $query varible.
>
> }
> simple stuff but the solution eludes me!!
General comments:
When using the "or die()" statement, I find it's helpfull to always
output your own error message, the query string that cause the error,
and the output from mysql_error().
Again, where are your variables line $UserName, $cart being set?
Hope this helps,
Ken
.
- Follow-Ups:
- Re: Eof not working correctly
- From: adamsroy
- Re: Eof not working correctly
- References:
- Eof not working correctly
- From: adamsroy
- Eof not working correctly
- Prev by Date: Eof not working correctly
- Next by Date: Re: Eof not working correctly
- Previous by thread: Eof not working correctly
- Next by thread: Re: Eof not working correctly
- Index(es):
Relevant Pages
|
|