tqla Posted January 2, 2009 Share Posted January 2, 2009 Hello. This script Gets the page_id and makes the position field go 1 lower. Does anybody know how to prevent someone from going below 1? As it stands now it can go into the negatives. Thanks. <?php include('../includes/master.php'); $ID = $_GET['page_id']; $sql = "UPDATE $content SET position = position-1 WHERE page_id = $ID"; $result = mysql_query($sql) or die (mysql_error()); // redirect header('Location: index.php'); ?> Link to comment https://forums.phpfreaks.com/topic/139160-solved-stopping-a-field-from-going-into-negatives/ Share on other sites More sharing options...
aasmith26 Posted January 2, 2009 Share Posted January 2, 2009 Your Code: <?php include('../includes/master.php'); $ID = $_GET['page_id']; $sql = "UPDATE $content SET position = position-1 WHERE page_id = $ID"; $result = mysql_query($sql) or die (mysql_error()); // redirect header('Location: index.php'); ?> First, I commend you on your die(); LOL not many people do that and that would fix a lot of their errors. Hmm I would try this. <?php include('../includes/master.php'); $ID = $_GET['page_id']; // if $ID is greater than or equal to one, then subtract the actual value (1-1=0; 502-1 = 501) if ($ID>=1) { $sql = "UPDATE $content SET position = position-1 WHERE page_id = $ID"; } // if $ID is equal to 0 then automatically set the field to Zero, so it won't go into negative values. else { $sql = "UPDATE $content SET position = '0' WHERE page_id = $ID"; } $result = mysql_query($sql) or die (mysql_error()); // redirect header('Location: index.php'); ?> Try That. If it works, give me a shout. If not let me know, i'll try to fangle something else up. Best of luck! Andy Link to comment https://forums.phpfreaks.com/topic/139160-solved-stopping-a-field-from-going-into-negatives/#findComment-727864 Share on other sites More sharing options...
tqla Posted January 2, 2009 Author Share Posted January 2, 2009 Thanks aasmith26! That was perfect. I added a quick * query to it so that I can get the value of position first and then ran your code. Awesome. <?php include('../includes/master.php'); $ID = $_GET['page_id']; $sql = "SELECT * FROM $content WHERE page_id = $ID"; $result = mysql_query($sql) or die (mysql_error()); $row = mysql_fetch_assoc($result); $PID = $row['position']; // if $ID is greater than or equal to one, then subtract the actual value (1-1=0; 502-1 = 501) if ($PID>=1) { $sql = "UPDATE $content SET position = position-1 WHERE page_id = $ID"; } // if $ID is equal to 0 then automatically set the field to Zero, so it won't go into negative values. else { $sql = "UPDATE $content SET position = '0' WHERE page_id = $ID"; } $result = mysql_query($sql) or die (mysql_error()); // redirect header('Location: index.php'); ?> Link to comment https://forums.phpfreaks.com/topic/139160-solved-stopping-a-field-from-going-into-negatives/#findComment-727903 Share on other sites More sharing options...
aasmith26 Posted January 2, 2009 Share Posted January 2, 2009 Hey Bud! Glad I could Help. Anytime you need help with somethin, just give me a PM or post on here. The guys on here are great at what they do. Thanks again! Link to comment https://forums.phpfreaks.com/topic/139160-solved-stopping-a-field-from-going-into-negatives/#findComment-727909 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.