Jump to content

[SOLVED] Editing using switch/case


careym1989

Recommended Posts

Hello again! Got another question. I'm using switch and cases to change the command. Examples of the commands are: view, add, edit, delete. Everything else is working perfectly, but I'm having trouble with the 'edit' case. Here's the code:

 

case 'edit':
      
$submitted == $_REQUEST['submitted'];
    
    if($submitted=="1") {
    
    $text == $_REQUEST['text'];
    $url == $_REQUEST['url'];
    $id == $_REQUEST['id'];
    
include("MySQL_Connect.php");
$sql = "UPDATE navbar SET text='$text', url='$url' WHERE id='$id'";
$result = mysql_query($sql);  

if($result) {
    echo mysql_affected_rows();
    echo " records successfully updated.";
    exit;
    } else {
    echo "Error encountered. Try again.";
    }
    
    } else {
    echo "Please fill out the form above and click \"Submit.\"";
    }
?> 
    
</p>

<p>
<?php

    $id = $_REQUEST['id'];
    
include("MySQL_Connect.php");
$sql = "SELECT * FROM navbar WHERE id='$id'";
$result = mysql_query($sql);  

while($info = mysql_fetch_assoc($result))  
{
    ?>

<form name="nb" method="post" action="http://www.blackandwhitecms.com/Control_Panel/Navigation_Bar.php?cmd=edit&id=<?php echo $id ?>">
  <table width="476" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td width="112">Link Title</td>
      <td width="364"><label>
        <input name="text" type="text" id="text" size="40" value="<?php echo $info[text] ?>" />
      </label></td>
    </tr>
    <tr>
      <td>Link URL</td>
      <td><label>
        <input name="url" type="text" id="url" size="40" value="<?php echo $info[url] ?>" />
      </label></td>
    </tr>
  </table>
  <p>
  	<input type="hidden" name="submitted" id="submitted" value="1" />
    <input type="hidden" name="id" id="id" value="<?php echo $info[id] ?>" />
    <label>
      <input type="submit" name="Submit" id="Submit" value="Submit" />
    </label>
  </p>
</form>

<?php } 
  
  
    break;

 

The problem is the $submitted if statement isn't working properly. Currently, I change the information that's in the fields and hit "Submit" and the fields change back to what they are currently in the table.

 

Help would be much appreciated! I've tried a couple of different approaches, but none have worked.

 

Thanks,

C

Link to comment
https://forums.phpfreaks.com/topic/138116-solved-editing-using-switchcase/
Share on other sites

Thanks for the speedy reply. Here's my code:

 

	$submitted == $_REQUEST['submitted'];
    
    if($submitted=="1") {
    
    $text == $_REQUEST['text'];
    $url == $_REQUEST['url'];
    $id == $_REQUEST['id'];
    
include("MySQL_Connect.php");
$sql = "UPDATE navbar SET text='$text', url='$url' WHERE id='$id'";
$result = mysql_query($sql) or die(mysql_error($sql));

 

Unfortunately, it's not even debugging. I put your code first and then tried the above code. Still, nothing is appearing.

Here's the entire page:

 

<?
session_start();
if(!session_is_registered(myusername)){
header("location:Default.php");
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Control Panel</title>
<link href="media/Style.css" type="text/css" rel="stylesheet" />
</head>

<body>
<img src="media/Logo.jpg" width="290" height="99" alt="BWCMS" class="topleft" />
<img src="media/ControlPanel.png" class="topmid" />
<div class="navcontainer">
<ul id="navlist">
<li><a href="Main.php">Home</a></li>
<li><a href="Statistics.php">Statistics</a></li>
<li><a href="Navigation_Bar.php">Navigation Bar</a></li>
<li><a href="Clients.php">Clients</a></li>
<li><a href="Products.php">Products</a></li>
<li><a href="Updates.php">Updates</a></li>
<li><a href="Help.php">Help</a></li>
<li><a href="Logout.php">Logout</a></li>
</ul>
</div>
<div class="content">
<?php

if (isset($_GET['cmd'])) {
  switch ($_GET['cmd']) {

// ADD FUNCTION

case 'add':
      
$text = $_POST['text'];
$url = $_POST['url'];
$submitted = $_POST['submitted'];

if ($submitted == 1) {

mysql_connect("mysql.blackandwhitecms.com", "bwcms", "deafly12") or die(mysql_error()); 
mysql_select_db("bwcms_db") or die(mysql_error()); 

$sql = "INSERT INTO navbar (text, url) VALUES ('$text', '$url')";
mysql_query($sql); 

echo "<font color=green>";
echo mysql_affected_rows();
echo " record(s) successfully added to the database.";
echo "</font>";

} else {

?>

<form id="navbaradd" name="navbaradd" method="post" action="http://www.blackandwhitecms.com/Control_Panel/Navigation_Bar.php?cmd=add">
  <table width="476" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td width="112">Link Title</td>
      <td width="364"><label>
        <input name="text" type="text" id="text" size="40" value="<?php echo $text ?>" />
      </label></td>
    </tr>
    <tr>
      <td>Link URL</td>
      <td><label>
        <input name="url" type="text" id="url" size="40" value="<?php echo $url ?>" />
      </label></td>
    </tr>
  </table>
  <p>
  	<input type="hidden" name="submitted" value="1" />
    <label>
      <input type="submit" name="Submit" id="Submit" value="Submit" />
    </label>
  </p>
</form>

<?php } ?>
<?php
      break;
  
  // DELETE FUNCTION
  
    case 'del':

    $id = $_GET['id'];
    
include("MySQL_Connect.php");
$sql = "DELETE FROM navbar WHERE id='$id'";
$result = mysql_query($sql);  

if($result)
   	{
    echo mysql_affected_rows();
    echo " record successfully deleted from the database.";
    } else {
    echo "Error occurred. Please try again.";
    }


      break;

// EDIT FUNCTION

case 'edit':
      
$submitted == $_REQUEST['submitted'];
    
    if($submitted=="1") {
    
    $text == $_REQUEST['text'];
    $url == $_REQUEST['url'];
    $id == $_REQUEST['id'];
    
include("MySQL_Connect.php");
$sql = "UPDATE navbar SET text='$text', url='$url' WHERE id='$id'";
$result = mysql_query($sql) or die(mysql_error() . "<br />$sql<br />");
$resultold = mysql_query($sql2) or die(mysql_error($sql));

if($result) {
    echo mysql_affected_rows();
    echo " records successfully updated.";
    exit;
    } else {
    echo "Error encountered. Try again.";
    }
    
    } else {
    echo "Please fill out the form below and click \"Submit.\"";
    }
?> 
    
</p>

<p>
<?php

    $id = $_REQUEST['id'];
    
include("MySQL_Connect.php");
$sql = "SELECT * FROM navbar WHERE id='$id'";
$result = mysql_query($sql);  

while($info = mysql_fetch_assoc($result))  
{
    ?>

<form name="nb" method="post" action="http://www.blackandwhitecms.com/Control_Panel/Navigation_Bar.php?cmd=edit&id=<?php echo $id ?>">
  <table width="476" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td width="112">Link Title</td>
      <td width="364"><label>
        <input name="text" type="text" id="text" size="40" value="<?php echo $info[text] ?>" />
      </label></td>
    </tr>
    <tr>
      <td>Link URL</td>
      <td><label>
        <input name="url" type="text" id="url" size="40" value="<?php echo $info[url] ?>" />
      </label></td>
    </tr>
  </table>
  <p>
  	<input type="hidden" name="submitted" id="submitted" value="1" />
    <input type="hidden" name="id" id="id" value="<?php echo $info[id] ?>" />
    <label>
      <input type="submit" name="Submit" id="Submit" value="Submit" />
    </label>
  </p>
</form>

<?php } 
  
  
    break;
    
case 'view':

    $id = $_GET['id'];
    
include("MySQL_Connect.php");
$sql = "SELECT * FROM navbar WHERE id='$id'";
$result = mysql_query($sql);  

while($info = mysql_fetch_assoc($result))  
{
echo "Text: $info[text]";
echo "<br />";
echo "URL: $info[url]";
}

break;

default:
      echo "A command is needed in the URL to continue. Please try again.";
  break;
  }
} else {
    
echo "<a href=http://www.blackandwhitecms.com/Control_Panel/Navigation_Bar.php?cmd=add>Add Record</a><br /><br /><br />";

    include("MySQL_Connect.php"); 
$sql = "SELECT * FROM navbar ORDER BY id ASC";
$result = mysql_query($sql);  

while($info = mysql_fetch_assoc($result))  
{
echo "$info[text] links to $info[url]  ";
echo "<a href=http://www.blackandwhitecms.com/Control_Panel/Navigation_Bar.php?cmd=view&id=$info[id]>View</a> | <a href=http://www.blackandwhitecms.com/Control_Panel/Navigation_Bar.php?cmd=edit&id=$info[id]>Edit</a> | <a href=http://www.blackandwhitecms.com/Control_Panel/Navigation_Bar.php?cmd=del&id=$info[id]>Delete</a><br /><br />";
    }


}

?>
</div>
</body>
</html>

 

The case I'm having trouble with is case 'edit.' Delete, add, view are no problem.

Got it working. Seems the $_POST did it. Not sure why -- I've use $_REQUEST for almost everything, except for $_GET's. It seems to work as $_POST too.

 

Thanks for the help, though! Took me a day to get that working!

 

Marking As Solved

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.