Jump to content

Recommended Posts

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.
Link to comment
https://forums.phpfreaks.com/topic/9870-is-there-an-error-checker/
Share on other sites

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.
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.
[!--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.
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.
Actually, I figured it out. Here is a problem though:

[code]
$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();
[/code]

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

[code]$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;
}
[/code]

This doesn't seem like a good idea to me though...
[!--quoteo(post=374801:date=May 17 2006, 06:22 PM:name=ryanlwh)--][div class=\'quotetop\']QUOTE(ryanlwh @ May 17 2006, 06:22 PM) [snapback]374801[/snapback][/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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.