Closed Thread Icon

Topic awaiting preservation: Lots of numbers (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=24523" title="Pages that link to Topic awaiting preservation: Lots of numbers (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Lots of numbers <span class="small">(Page 1 of 1)</span>\

 
Alexer
Bipolar (III) Inmate

From: Juneau, Alaska
Insane since: Jun 2004

posted posted 12-28-2004 20:40

I'm attempting to track and modify two sets of numbers in a table with PHP. The first is id, and the second is subid.

code:
if ( !isset($subid)) {
$Link = mysql_connect ($host, $user, $password);
$Query = "INSERT into $tablename values('0', '0', '$body', '$title', '$date', '$name')";
} else {
$Link = mysql_connect ($host, $user, $password);
$Query = "INSERT into $tablename values('0', '$subid', '$body', '$title', '$date', '$name')";
}



In both examples of $Query, the first value will go to the field called id, and the second value will go to the field called subid. In both cases, id will autoincrement to the lowest available value. I'd like subid's value to become the same as id's value if subid = 0 or $subid is not set.

Double Cross
The political blog for righties and lefties.

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 12-29-2004 06:21

Not really sure what you're doing here.

But this is going to take a few more querys. I'm assuming you're using an auto increment field for the id (if not, you should be) .

code:
if ( !isset($subid) || $subid == 0) {

$Link = mysql_connect ($host, $user, $password);

// Use an auto increment field and change this to
// insert into $tablename (whatever, whatever ) values ( $body , $title...

$Query = "INSERT into $tablename values('0', '0', '$body', '$title', '$date', '$name')";
mysql_query($Query);l

$id = mysql_insert_id();
$Query = "UPDATE $tablename set subid='$id'";

} else {

$Link = mysql_connect ($host, $user, $password);
// Same syntax as above
$Query = "INSERT into $tablename values('0', '$subid', '$body', '$title', '$date', '$name')";

}





.:[ Never resist a perfect moment ]:.

Alexer
Bipolar (III) Inmate

From: Juneau, Alaska
Insane since: Jun 2004

posted posted 12-29-2004 17:57

Thanks. I'm not very familiar with database operations in PHP, so this has been a big help. Thanks again.

EDIT: Having applied it, there seems to be an issue.

The following line of code updates all the subid fields, instead of only the one that shares a row with the id value being derived.

code:
$Query = "UPDATE $tablename set subid='$id'";



I attempted to use my limited knowledge to edit the query to change only the one subid field.

code:
$Query = "UPDATE $tablename where(id='$id') (subid) values ('$id')";



I've tried a variety of minor changed, but everything simply returns the error message that I set in place.

code:
if (mysql_db_query($dbname, $Query, $Link)) {
header("Location: readboard.php");
exit;
} else {
// Error that is returned if the above db query fails
echo "<center>Your submission failed to process correctly. Please contact your administrator with this error and try again later.</center>";
}



I know that $dbname and $Link are correct, because I've applied the same variables with the same values to other queries in the document. That just leaves $Query itself, but for the life of me I can't seem to figure out the correct statements to update a particular row.

(Edited by Alexer on 12-29-2004 20:47)

Alexer
Bipolar (III) Inmate

From: Juneau, Alaska
Insane since: Jun 2004

posted posted 01-05-2005 00:54

TA-DA!

code:
if ( !isset($subid)) {
$Link = mysql_connect ($host, $user, $password);
$Query = "INSERT into $tablename (position, body, title, date, name) values ('1', '$body', '$title', '$date', '$name')";
mysql_db_query($dbname, $Query, $Link);

$Query2 = "UPDATE $tablename SET subid = LAST_INSERT_ID() WHERE id = LAST_INSERT_ID()";
if (mysql_db_query($dbname, $Query2, $Link)) {
header("Location: readboard.php");
exit;
} else {
echo "<center>Your submission failed to process correctly. Please contact your administrator with this error and try again later.</center>";
}

} else {
$Link = mysql_connect ($host, $user, $password);
$Query = "INSERT into $tablename (subid, position, body, title, date, name) values('$subid', '0', '$body', '$title', '$date', '$name')";




//Once the data is sent off, send the user off or give them the bad news. REMEMBER TO CHANGE URL WHERE APPROPRIATE.
if (mysql_db_query($dbname, $Query, $Link)) {
header("Location: readboard.php");
exit;
} else {
echo "<center>Your submission failed to process correctly. Please contact your administrator with this error and try again later.</center>";



Thanks for the help.

Double Cross
The political blog for righties and lefties.

« BackwardsOnwards »

Show Forum Drop Down Menu