Forum Moderators: coopster

Message Too Old, No Replies

You can SELECT * FROM with limits, but INSERT with limit?

mysql

         

php4U

3:30 am on Sep 29, 2007 (gmt 0)

10+ Year Member



This may seem like a silly question, but if you can limit the results you pull from MySQL, in theory couldn't you limit what you insert into the database? I found a good quick reference that may help others, but I don't see anything about what I am looking for.

<snip>

The reason I ask is because say for instance some commercial CMS systems allow only so many pages total for a certain price...greater than that a little more and so on. There has to be a way to limit the entries. Any help is appreciated.

Brad

[edited by: eelixduppy at 7:20 am (utc) on Sep. 29, 2007]
[edit reason] removed url [/edit]

sid_o

8:39 am on Sep 29, 2007 (gmt 0)

10+ Year Member



Hi,
i dont knoe about the limit in the sql statment but you can create the limit in your PHP script.
if you'll post your sql , maybe new ideas will come

Cheers,

php4U

3:18 am on Sep 30, 2007 (gmt 0)

10+ Year Member



Thank you for the reply. The code portion containing the SQL statement wasn't long so I just posted that whole php code section.

<?php
require_once "validator_file.php";

if ($HTTP_POST_VARS){
$validator = new Validator();
$validator->validateGeneral($HTTP_POST_VARS['page_no'], EDITOR_PAGE_NO);
$validator->validateGeneral($HTTP_POST_VARS['title'], EDITOR_CATEGORY_TITLE);
$validator->validateGeneral($HTTP_POST_VARS['fckeditor'], EDITOR_CONTENT);

if ( $validator->foundErrors() ){
echo '<b style="color:#C92E2A; font-size:12px;"> '. ERROR_PLEASE_INSERT .' </b> <br /><i>'.$validator->listErrors('<br>').'</i><br><br />';
}else{
//database_connect();
[b]$insert = "INSERT INTO
category (title, text, keywords, metaDesc, robots, box1, box2, page_no)
VALUES ('$title', '$fckeditor', '$keywords', '$metaDesc', '$robots', '$box1', '$box2', '$page_no')";[/b]

if ($query3 = mysql_query($insert) or die(mysql_error())){

echo '<b> '. DATABASE_SUCCESS .' </b><br><br>';

}else{

echo('<p>MySQL Error #' . mysql_errno() . '</b><br>' . mysql_error() .'</p>');

}
}
}
?>


Sorry about the link eelixduppy, no harm intended. Thank you for any suggestions on this.
Brad

Habtom

5:09 am on Sep 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I couldn't see the point of putting any kind of limit on your insert statement. It is just inserting once, I believe.

If there was a certain kind of loop or anything which makes the insert statement to run again, you might want to have the limit. But I don't see why you need it now.

sid_o

9:09 am on Sep 30, 2007 (gmt 0)

10+ Year Member



i agree with Habtom

php4U

8:31 pm on Sep 30, 2007 (gmt 0)

10+ Year Member



Your right the INSERT statement only inserts the info once to the database. I needed to word it differently...basically I want to limit the number of times a new page can be added.

For instance...if the CMS system only allows a total of 10 pages to be added, and there are currently 10 pages...when the user goes to add the 11th page they will get an error saying something like "No more pages can be added at this time". Not sure how to create this in php...any ideas?

MrManager

8:38 pm on Sep 30, 2007 (gmt 0)

10+ Year Member



There is no such feature for MySQL. If you want to limit the number of rows for some reason, get the current number of rows before inserting and show an error if it's >= 10.

jatar_k

12:00 pm on Oct 1, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



select the count from mysql before you do the insert and if 10 pages already exist, then display the error

MrManager

12:55 pm on Oct 1, 2007 (gmt 0)

10+ Year Member



Isn't that exactly what I just said?

jatar_k

12:59 pm on Oct 1, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



yep, I obviously missed that somehow

php4U

6:09 pm on Oct 2, 2007 (gmt 0)

10+ Year Member



Thank you for the replies. I haven't had much time to mess with this at the moment, but will hopefully be able to give it a shot sometime this week.