Jump to content

[SOLVED] Stopping a field from going into negatives.


tqla

Recommended Posts

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');
?>

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

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.  ;D

 

<?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');
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.