tags:

views:

1545

answers:

4

Hey all, I want to change a value in a table (in a mysql database) via PHP code. I have a row in the table called 'approved' and there are two options it can be set to, "0" (not approved) and "1" (approved). I am creating a script that will change an individual approved from "0" to "1".

For example, there is a different value called 'position' and 'approved' sets the 'position' as approved or not approved (where approved is set to 1 or 0). If that is worded wrong, I will try to make it more clear.

I guess my question is can I make an individual 'position' value have its 'approved' data be switched from 0 to 1 and vice versa.

Thanks!

EDIT/UPDATE:

here's the info from the dump for this specific table:

 CREATE TABLE `positions` (
  `posID` int(10) unsigned NOT NULL auto_increment,
  `postitle` varchar(500) NOT NULL default '',
  `addtitletext` varchar(35) default NULL,
  `description` text NOT NULL,
  `print_website` enum('1','2') NOT NULL default '1',
  `userID` tinyint(4) unsigned NOT NULL default '0',
  `submitted_on` datetime NOT NULL default '0000-00-00 00:00:00',
  `approved_date` date NOT NULL default '0000-00-00',
  `approved` enum('0','1') NOT NULL default '0',
  PRIMARY KEY  (`posID`)
) ENGINE=MyISAM AUTO_INCREMENT=464 DEFAULT CHARSET=latin1;

LOCK TABLES `positions` WRITE;
/*!40000 ALTER TABLE `positions` DISABLE KEYS */;
INSERT INTO `positions` (`posID`,`postitle`,`addtitletext`,`description`,`print_website`,`userID`,`submitted_on`,`approved_date`,`approved`)
VALUES

and I was trying to edit this code here to make it change from not approved to approved (or vice versa)

    <? 
include('secure.php');
include('config.php'); 

if (isset($_POST['submitted'])) { 
foreach($_POST AS $key => $value) { $_POST[$key] = mysql_real_escape_string($value); } 
$sql = "UPDATE `positions` SET `approved` =  '{$_POST['approved']}'"; 
mysql_query($sql) or die(mysql_error()); 
echo (mysql_affected_rows()) ? "Edited row.<br />" : "Nothing changed. <br />"; 
echo "<a href='list.php'>Back To Listing</a>"; 
} 
$row = mysql_fetch_array ( mysql_query("SELECT * FROM `positions` WHERE `posID` = '$posID' ")); 




?>

<form action='' method='POST'> 
<p><b>Approved:</b><br /><input type='text' name='approved' value='<?= stripslashes($row['approved']) ?>' /> 
<p><input type='submit' value='Edit Row' /><input type='hidden' value='1' name='submitted' /> 
</form>

If it helps, awesome, but sorry if it is more confusing haha.

EDIT: Here's what I have, but getting a blank page (errors I know)

<? 
include('secure.php');
include('config.php'); 
if (isset($_GET['posID']) ) { 
$posID = (int) $_GET['posID']; 
if (isset($_POST['submitted'])) { 
foreach($_POST AS $key => $value) { $_POST[$key] = mysql_real_escape_string($value); } 
$sql = "UPDATE `positions` SET `approved` =  '{$_POST['approved']}'  WHERE `posID` = '$posID'"; 
mysql_query($sql) or die(mysql_error()); 
echo (mysql_affected_rows()) ? "Edited row.<br />" : "Nothing changed. <br />"; 
echo "<a href='list.php'>Back To Listing</a>"; 
} 

<form action='' method='POST'> 
<p><b>Approved:</b><br /><input type='text' name='approved' value='<?= stripslashes($row['approved']) ?>' /> 
<p><input type='submit' value='Edit Row' /><input type='hidden' value='1' name='submitted' /> 
</form> 
<? } ?>
+4  A: 

I'm not entirely sure what you are trying to do, because it sounds like approved is a column, not a row, in the table. If you wanted to do something like the following:

function toggle_approved($position_id) {
$query = "UPDATE positions SET approved = !approved WHERE posID = '$position_id'";
// execute the query here with your mysql_query() call
}

I am assuming you have some method of executing mysql queries, if not see this link. Also make sure you use your table name and position field names.

ghills
use single equal sign `=` for comparison, not `==`
duckyflip
oops, typo. Thanks for catching that.
ghills
A: 

Edit: Still not sure I 100% understand you, but are you trying to do something like the following?

if($_POST['approved'] == '0')
    $sql = "UPDATE `positions` SET `approved` =  '1' WHERE `posID` = '$posID'"; 
else
    $sql = "UPDATE `positions` SET `approved` =  '0' WHERE `posID` = '$posID'"; 
mysql_query($sql) or die(mysql_error());
lc
Did I miss something? I hate when people downvote without mentioning why; I don't learn anything and I just get frustrated.
lc
I did not, but maybe because I asked for it in PHP and not mysql? I could be wrong, thanks for the help though!
SkyWookie
I guess. I'd think when someone wants to change a mysql database using PHP, they're going to use SQL to query the database, right? So, I understood the question to be more "what query should I use to do XXX" and not "how do I use mysql in PHP" because it was worded as such...
lc
This answers seems more complicated than it needs to be. Why use 'case' when he can simply set the value to 1 or 0?
jmccartie
This was before the OP's edit, that I'm trying to understand now. The OP seemed to be looking for how to toggle values for matching row(s).
lc
Yes that is what I was looking for! The only other thing I have to comment on is that it must do it for an individual posID, not all at once. Any ideas?
SkyWookie
Well, that's what the variable $posID is for. I suggest you keep it as a hidden input so it gets added to the POST vars when submitted. You seemed to know what $posID was in the line "$row = mysql_fetch_array ( mysql_query("SELECT * FROM `positions` WHERE `posID` = '$posID' "));" so I didn't question or comment on it.
lc
oh yeah sorry, I am just stuck, I can post my code and show what I am doing. It keeps coming up blank..
SkyWookie
+1  A: 

yes. you would do something like:

UPDATE table_name
SET approved=value
WHERE position=some_value

A good place for learning SQL and PHP is w3schools: http://www.w3schools.com/sql/default.asp

Josh Curren
A: 

Figured out what I was doing wrong! Here's my code, it was actually something in a previous page that I neglected to fix (stupid little link error).

<? 
include('secure.php');
include('config.php'); 

if (isset($_GET['posID']) ) { 
$posID = (int) $_GET['posID'];
if (isset($_POST['submitted'])) { 
$sql = "UPDATE `positions` SET  `approved` =  '{$_POST['approved']}'   WHERE `posID` = '$posID'"; 
mysql_query($sql) or die(mysql_error()); 
echo (mysql_affected_rows()) ? "Edited row.<br />" : "Nothing changed. <br />"; 
echo "<a href='list.php'>Back To Listing</a>"; 
} 
$row = mysql_fetch_array ( mysql_query("SELECT `approve` FROM `positions` WHERE `posID` = '$posID' ")); 
//echo "<p><b>Department</p></b>";
//$query="SELECT deptname,deptID FROM depts";

//$result = mysql_query ($query);
//echo "<select name=depts value=''>Department</option>";

//while($nt=mysql_fetch_array($result)){
//echo "<option value=$nt[deptID]>$nt[deptname]</option>";
/* Option values are added by looping through the array */
//}
//echo "</select>";
//`department` =  '{$_POST['department']}'

?>

<form action='' method='POST'> 
<p><b>Approved:</b><br /><input type='text' name='approved' value='<?= stripslashes($row['approved']) ?>' /> 
<p><input type='submit' value='Edit Row' /><input type='hidden' value='1' name='submitted' /> 
</form> 

<? } ?>
SkyWookie