Re: Eof not working correctly



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

.



Relevant Pages

  • ADO exception with character combination inside string...
    ... We also have various Delphi7 programs that operate on this database. ... These programs are set up to trap the exception in an ADO operation ... and log the query text that was in use when the exception happened. ... What happens is that if we have a string to store inside the database ...
    (borland.public.delphi.database.ado)
  • Row Level Locking from Excel VBA query
    ... There are multiple users accessing the database at ... query that I am using so that two users will not get the same order ... Dim dbsConn As ADODB.Connection ... Dim connString As String ...
    (microsoft.public.access.queries)
  • Re: Fastest String search
    ... I have to query the database with the string from text file. ... By this I mean writing your own Binary File Access method and reading the Oracle database records without using any database engine. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Date extraction
    ... I don't know which database you're using, ... Assuming, for now, that you're reading these dates as text strings then the ... Dim sDateString As String ... did you mean a VB query or a SQL query? ...
    (microsoft.public.vb.general.discussion)
  • JSP Input Validation - URGENT PLEASE HELP!
    ... //create a Connection, Statement, and ResultSet object to connect to ... //open a connection to the database ... String username = ""; ... //create a query to extract data from the database ...
    (comp.lang.java.help)