greencoin Posted June 15, 2007 Share Posted June 15, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/55769-way-over-my-head-query-mysql-return-highest-value-then-create-value-1/ Share on other sites More sharing options...
Nhoj Posted June 15, 2007 Share Posted June 15, 2007 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/55769-way-over-my-head-query-mysql-return-highest-value-then-create-value-1/#findComment-275517 Share on other sites More sharing options...
chigley Posted June 15, 2007 Share Posted June 15, 2007 SELECT MAX(id + 1) AS maximum FROM table ORDER BY maximum DESC LIMIT 1 Try that. Quote Link to comment https://forums.phpfreaks.com/topic/55769-way-over-my-head-query-mysql-return-highest-value-then-create-value-1/#findComment-275518 Share on other sites More sharing options...
greencoin Posted June 15, 2007 Author Share Posted June 15, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/55769-way-over-my-head-query-mysql-return-highest-value-then-create-value-1/#findComment-275522 Share on other sites More sharing options...
chigley Posted June 15, 2007 Share Posted June 15, 2007 Nah you'd get an error. Not 100% if that query will work, give it a go! Quote Link to comment https://forums.phpfreaks.com/topic/55769-way-over-my-head-query-mysql-return-highest-value-then-create-value-1/#findComment-275529 Share on other sites More sharing options...
chigley Posted June 15, 2007 Share Posted June 15, 2007 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; Quote Link to comment https://forums.phpfreaks.com/topic/55769-way-over-my-head-query-mysql-return-highest-value-then-create-value-1/#findComment-275531 Share on other sites More sharing options...
greencoin Posted June 18, 2007 Author Share Posted June 18, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/55769-way-over-my-head-query-mysql-return-highest-value-then-create-value-1/#findComment-276802 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.