Jump to content


Photo

Is there an error checker?


  • Please log in to reply
11 replies to this topic

#1 DaVuLf

DaVuLf
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 17 May 2006 - 05:00 PM

Is there a method to check to see if there is an error? Suppose I'm using a script to add information to a table. I add the information based off of a ClientID field within the form. I want an IF statement to check whether a table for that ClientID exists, and if not, then make it and proceed with the script. If it does exist, then just continue with the script.

Another quick question; what is the method to get the information in the last row of a table in a certain field? So suppose I want it to select * from clients where [value] = lastrow_value;. I think that makes sense, but I'm not sure how I would incorporate that.

Any suggestions to either of these would be welcome.

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 17 May 2006 - 05:52 PM

if you have any unique constraints on your table, the record will just not be inserted, but there will be a constraint violation SQL error thrown, so if you want to avoid this, you need to run your own error checking on it.

if you've got an auto incrementing field, you could always just select the MAX(id) from the table, although, there are times where this could cause issues if there are multiple people updating the database at the same time. otherwise, this should work fine for you.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 DaVuLf

DaVuLf
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 17 May 2006 - 06:36 PM

Obsidian: I'm not sure that answers the question. What I'm wondering is that if the table doesn't exist, I need to create it and then insert the data. This way I don't need to create the tables seperately. The reason I do this is because I have a table for each ClientID.

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 17 May 2006 - 06:47 PM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]What I'm wondering is that if the table doesn't exist, I need to create it and then insert the data. This way I don't need to create the tables seperately. The reason I do this is because I have a table for each ClientID.[/quote]
Two thoughts.

#1 - Are you sure that the optimum database design is to have one table for each client. That decision may come back to haunt you later.

#2 - If I remember right this users need to sign up/be approved for your system so make the table creation part of the sign up process. That way you'll always have the table ready.
Legend has it that reading the manual never killed anyone.
My site

#5 DaVuLf

DaVuLf
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 17 May 2006 - 07:07 PM

Hey Andy, thanks for the reply.

#1: After considering the alternatives I have heard on this forum, having a table for each user seems to be the best option.

#2: All users will be entered manually, so there is no place for them to enter anything. This isn't an 'online' type thing, I will be running this through a LAN. There will be the organizers who will input data, and the participants who go to one of the organizers in order to state their wishes.

#6 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 17 May 2006 - 07:17 PM

use this create table query
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--]CREATE TABLE IF NOT EXISTS table1 ... [!--sql2--][/div][!--sql3--]
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#7 DaVuLf

DaVuLf
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 17 May 2006 - 08:12 PM

That's exactly what I'm looking for. Thanks man.

#8 DaVuLf

DaVuLf
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 17 May 2006 - 08:40 PM

Hey, how do you do If statements in php?

#9 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 17 May 2006 - 09:19 PM

are you asking for how to write if statements in general, or are you asking about a specific condition?
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#10 DaVuLf

DaVuLf
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 17 May 2006 - 09:45 PM

Actually, I figured it out. Here is a problem though:

$team=$_POST['team'];
$stock=$_POST['stock'];
$type=$_POST['type'];
$time=$_POST['time'];
$quantity=$_POST['quantity'];


mysql_connect($hostname,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query="CREATE TABLE IF NOT EXISTS $team (id int(6) NOT NULL auto_increment,team varchar(25) NOT NULL,stock varchar(4) NOT NULL,type varchar(20) NOT NULL,time int(6) NOT NULL,quantity int(20) NOT NULL,value double NOT NULL,balance double NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))";
mysql_query($query);

//NEED HELP WITH THIS AREA
//IF I HAD TO CREATE A TABLE, I NOW NEED TO ADD SOME VALUES, OTHERWISE MY NUMROWS WILL BE 
//AN ERROR
//$query = "INSERT INTO $team VALUES ('','$team','$stock','$type','$time','$quantity','$value','$balance')";
//mysql_query($query);

$value_result = "SELECT * FROM $stock WHERE id = '$time'";
$value_get = mysql_query($value_result) or die(mysql_error());;
$value_find = mysql_fetch_assoc($value_get) or die(mysql_error());;
$value = $value_find[value];

$query="SELECT * FROM $team";
$result=mysql_query($query);
$num=mysql_numrows($result);

$find_balance="SELECT balance FROM $team WHERE id = '$num'";
$find_result= mysql_query($find_balance);
$last_balance= mysql_result($find_result,0,0);

$number=$quantity*$value;

if ($number > $last_balance){
    $quantity=intval($last_balance/$value);
    $new_number=$quantity*$value;
    $balance=$last_balance-$new_number;
    echo "is bigger";
} else {
    $balance = $last_balance-$number;
    echo "isn't bigger";
}


$query = "INSERT INTO $team VALUES ('','$team','$stock','$type','$time','$quantity','$value','$balance')";
mysql_query($query);

mysql_close();

Basically, I check for the last balance in order to gauge what to do. If I'm just creating the table, I want to have an initial balance of $100,000. I'm not really sure how I could go about doing that. It always seems like I need an error checker. I need something to say that if we can't find how many rows there are, it means the table is empty, if the table is empty, the balance is $100,000.

#11 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 17 May 2006 - 10:22 PM

in this case, dont use IF NOT EXISTS. Do an ordinary create table query. Then check whether mysql_query returns true. If it returns true, the table was successfully created (meaning a new table), and you insert $100,000.

$query="CREATE TABLE $team (id int(6) NOT NULL auto_increment,team varchar(25) NOT NULL,stock varchar(4) NOT NULL,type varchar(20) NOT NULL,time int(6) NOT NULL,quantity int(20) NOT NULL,value double NOT NULL,balance double NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))";
$result = mysql_query($query);

if($result)
{
    initial balance of $100,000
}
else
{
    table already exists;
}

This doesn't seem like a good idea to me though...
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#12 DaVuLf

DaVuLf
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 18 May 2006 - 12:34 AM

[!--quoteo(post=374801:date=May 17 2006, 06:22 PM:name=ryanlwh)--][div class=\'quotetop\']QUOTE(ryanlwh @ May 17 2006, 06:22 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
This doesn't seem like a good idea to me though...
[/quote]

What do you think I should go with?

I think what I'm struggling most with is that I originally made this with MS Excel, and I'm translating it to PHP and mySql.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users