Jump to content

way over my head - query mysql, return highest value then create value +1


Recommended Posts

OK, you guys got me hooked and now I want to go farther with the stuff I've learned.

 

I have a product input form I created (with php freaks help) for my website which I input general information and specs on our products. What I'd like to do now is when that form loads, it searches a seperate table and looks for the highest "ID" number. It returns the number, adds 1 to it and displays the number somewhere on the input form for reference + error checking. When the form is submitted it takes that form and creates a record pushing that number in the correct field.

 

I really appreciate when you guys give me code as it speeds up my development time and I get to study it off work but I don't want to be a mooch so even some advice on the steps I'd need to take is greatly appreciated.

 

I'm thinking the order will be something like: query->get data ->for loop ->print ->push

 

****************************************************************************************************

The theory of my site is product information is stored on one table while the marketing information is stored on a seperate table in the same database. A query to the product table will populate a php template and a relevant link in that table will link to an ID in the marketing table that retrieves the associated record of where the website  data is stored ie., url to text, product images, etc.

****************************************************************************************************

 

Thanks again guys, ~Rich

Not exactly sure what you mean after getting the highest ID# but to get the highest ID number you can do something like this:

 

<?php
$highest_id = mysql_result(mysql_query('SELECT `id` FROM `table` ORDER BY `id` DESC LIMIT 1'), 0);
?>

SELECT MAX(id + 1) AS maximum FROM table ORDER BY maximum DESC LIMIT 1

 

Try that.

 

OOOOOO - that looks nice... out of curiosity if I used that on an empty table, what would it return? 1? ~Rich

OK the query works, but the last bits weren't needed:

 

$query = mysql_query("SELECT MAX(id+1) AS maximum FROM table") or die(mysql_error());
list($maximum) = mysql_fetch_row($query);
echo $maximum;

OK the query works, but the last bits weren't needed:

 

$query = mysql_query("SELECT MAX(id+1) AS maximum FROM table") or die(mysql_error());
list($maximum) = mysql_fetch_row($query);
echo $maximum;

 

OK, I used your code and it works ...well, sorta. My ID is set as a mediumint (5), unsigned zerofill - in the MySQL gui (godaddy) it shows 00001 which is correct. When I echo $maximum; in my page it shows 2. While mathmatically correct, it's not displaying correctly which needs to be 00002.

 

So I'm wondering where the fault is? Is the PHP stripping my extra zeros? Is it cause I set it as an Integer and not a Varchar in MySql? ????

 

Thanks for the help ~Rich

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.