Solved

Record Not Updating

Posted on 2015-01-27
14
80 Views
Last Modified: 2015-01-27
This table almost works.  Delete and Add are fine.  However: Update doesn't update.  I tried both checked and unchecked  Nothing happens.  The link.

http://www.davidschure.com/index.php

PHP
<?php
		  include("conn.php");
		  $mode=$_GET["mode"];
		  if($mode=="add") {
		  	$recordSite=$_POST["recordSite"];
			$recordUser=$_POST["recordUser"];
			$recordPass=$_POST["recordPass"];
			$sql="insert into $branch(recordSite,recordUser, recordPass) values('$recordSite','$recordUser','$recordPass')";
			$result=mysql_query($sql,$connection) or die(mysql_error());
			header("location: index.php");
			
		  } elseif($mode=="update") {
		  	$recordSite=$_POST["recordSite"];
			$recordUser=$_POST["recordUser"];
			$recordPass=$_POST["recordPass"];
			$sql="update $branch set recordSite='$recordSite',recordUser='$recordUser',recordPass='$recordPass' where recordId='$recordId'";
			//echo $sql;
			$result=mysql_query($sql,$connection) or die(mysql_error());
			header("location: index.php");
		  }
		  ?>

Open in new window


HTML
<table width="775" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
  <tr>
    <td><hr size="1" noshade></td>
  </tr>
  <tr>
    <td>
      <form action="" method="post" name="" id="">
        <table width="600" border="1" align="center" cellpadding="2" cellspacing="2">
          <tr>
            <td><input name="topcheckbox" type="checkbox" class="check" id="topcheckbox" onClick="selectall();" value="ON">
Select All &nbsp;&nbsp;&nbsp;</td>
            <td colspan="3" align="center"><a href="form.php?mode=add">Add New Record </a></td>
          </tr>
          <tr>
            <td><strong><a href="javascript:goDel()">Delete</a></strong></td>
            <td><strong>Site</strong></td>
            <td><strong>User</strong></td>
            <td><strong>Pass</strong></td>
            <td><strong>Update</strong></td>
          </tr>
		  <?
		  include("conn.php");
		  $sql="select recordId,recordSite,recordUser,recordPass from $branch order by recordId";
		  $result=mysql_query($sql,$connection) or die(mysql_error());
		  while($row=mysql_fetch_array($result)) {
		  ?>
          <tr>
            <td><input name="<? echo $row['recordId']; ?>" type="checkbox" class="check"></td>
            <td><? echo $row['recordSite']; ?></td>
            <td><? echo $row['recordUser']; ?></td>
            <td><? echo $row['recordPass']; ?></td>
            <td><a href="<? echo "form.php?recordId=".$row['recordId']."&mode=update"; ?>">Update</a></td>
          </tr>
		  <? } ?>
        </table>
    </form></td>
  </tr>
</table>

Open in new window

0
Comment
Question by:DS928
  • 8
  • 6
14 Comments
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
Try changing:
$mode=$_GET["mode"];
to:
$mode=$_REQUEST["mode"];

Your intermediate update form is using a POST method, which means that mode=update will be in $_POST["mode"], not $_GET["mode"]. The $_REQUEST variable is basically both $_GET and $_POST merged together, with $_POST values taking precedence.
0
 

Author Comment

by:DS928
Comment Utility
Nope.  It didn't work.
0
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
Can you post all the code?
0
 

Author Comment

by:DS928
Comment Utility
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Index</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript" src="script.js"> </script>
<style type="text/css">
<!--
.style1 {color: #FFFFFF}
-->
</style>
</head>

<body>
<table width="775" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
  <tr>
    <td><hr size="1" noshade></td>
  </tr>
  <tr>
    <td>
      <form action="" method="post" name="" id="">
        <table width="600" border="1" align="center" cellpadding="2" cellspacing="2">
          <tr>
            <td><input name="topcheckbox" type="checkbox" class="check" id="topcheckbox" onClick="selectall();" value="ON">
Select All &nbsp;&nbsp;&nbsp;</td>
            <td colspan="3" align="center"><a href="form.php?mode=add">Add New Record </a></td>
          </tr>
          <tr>
            <td><strong><a href="javascript:goDel()">Delete</a></strong></td>
            <td><strong>Site</strong></td>
            <td><strong>User</strong></td>
            <td><strong>Pass</strong></td>
            <td><strong>Update</strong></td>
            <td><strong>Delete</strong></td>
          </tr>
		  <?
		  include("conn.php");
		  $sql="select recordId,recordSite,recordUser,recordPass from $branch order by recordId";
		  $result=mysql_query($sql,$connection) or die(mysql_error());
		  while($row=mysql_fetch_array($result)) {
		  ?>
          <tr>
            <td><input name="<? echo $row['recordId']; ?>" type="checkbox" class="check"></td>
            <td><? echo $row['recordSite']; ?></td>
            <td><? echo $row['recordUser']; ?></td>
            <td><? echo $row['recordPass']; ?></td>
            <td><a href="<? echo "form.php?recordId=".$row['recordId']."&mode=update"; ?>">Update</a></td>
            <!--td><a href="<!--? echo "delete.php?recordId=".$row['recordId']."&mode=delete"; ?>">Delete</a></td-->
            <!--td><strong><a href="javascript:goDel()">Delete</a></strong></td-->
            <!--td><input name="<!--? echo $row['recordId']; ?>" type="checkbox" class="check"></td-->
          </tr>
		  <? } ?>
        </table>
    </form></td>
  </tr>
</table>
</body>
</html>

Open in new window


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Branch</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<table width="775" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>      <table width="700" border="0" align="center" cellpadding="2" cellspacing="2">
        <tr>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>
		  <?PHP
		  $mode=$_GET["mode"];
		  if($mode=="add") {
		  ?>
          <form name="form1" method="post" action="formsubmit.php?mode=add">
            <table width="500" border="1" align="center" cellpadding="2" cellspacing="2">
              <tr>
                <td><strong>Add New Record </strong></td>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td>Site </td>
                <td><input name="recordSite" type="text" id="recordSite"></td>
              </tr>
              <tr>
                <td>User</td>
                <td><input name="recordUser" type="text" id="recordUser"></td>
              </tr>
              <tr>
                <td>Pass</td>
                <td><input name="recordPass" type="text" id="recordPass"></td>
              </tr>
              <tr>
                <td><input type="submit" name="Submit" value="Save Data"></td>
                <td>&nbsp;</td>
              </tr>
            </table>
          </form>
		  <?PHP		
		  } else  {
		  	include("conn.php");
			$recordId=$_GET["recordId"];
			$sql="select recordId,recordSite,recordUser, recordPass from $branch where recordId='$recordId'";
			
			$result=mysql_query($sql,$connection) or die(mysql_error());
			while($row=mysql_fetch_array($result)) {
				$recordId=$row['recordId'];
				$recordSite=$row['recordSite'];
				$recordUser=$row['recordUser'];
				$recordPass=$row['recordPass'];
			}
		?>
		<form name="form1" method="post" action="formsubmit.php?mode=update">
            <table width="500" border="1" align="center" cellpadding="2" cellspacing="2">
              <tr>
                <td><strong>Update Record </strong></td>
                <td><input type="hidden" name="recordId" value="<? echo $recordId; ?>">
                  &nbsp;</td>
              </tr>
              <tr>
                <td>Site </td>
                <td><input name="recordSite" type="text" id="recordSite" value="<? echo $recordSite; ?>"></td>
              </tr>
              <tr>
                <td>User </td>
                <td><input name="recordUser" type="text" id="recordUser" value="<? echo $recordUser; ?>"></td>
              </tr>
              <tr>
                <td>Pass </td>
                <td><input name="recordPass" type="text" id="recordPass" value="<? echo $recordPass; ?>"></td>
              </tr>
              <tr>
                <td><input type="submit" name="Submit" value="Update Data"></td>
                <td>&nbsp;</td>
              </tr>
            </table>
          </form>
		
		<?PHP	
			
		  }
		  ?>
		 
		  </td>
        </tr>
        <tr>
          <td>&nbsp;</td>
        </tr>
    </table></td>
  </tr>
</table>
</body>
</html>

Open in new window


<?php
		  include("conn.php");
		  $mode=$_GET["mode"];
		  if($mode=="add") {
		  	$recordSite=$_POST["recordSite"];
			$recordUser=$_POST["recordUser"];
			$recordPass=$_POST["recordPass"];
			$sql="insert into $branch(recordSite,recordUser, recordPass) values('$recordSite','$recordUser','$recordPass')";
			$result=mysql_query($sql,$connection) or die(mysql_error());
			header("location: index.php");
			
		  } elseif($mode=="update") {
		  	$recordSite=$_POST["recordSite"];
			$recordUser=$_POST["recordUser"];
			$recordPass=$_POST["recordPass"];
			$sql="update $branch set recordSite='$recordSite',recordUser='$recordUser',recordPass='$recordPass' where recordId='$recordId'";
			//echo $sql;
			$result=mysql_query($sql,$connection) or die(mysql_error());
			header("location: index.php");
		  }
		  ?>

Open in new window

0
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
1. Which file is which? Is the last one the formsubmit.php?
2. Which file did you change the $_GET to $_REQUEST?
3. At the top of formsubmit.php, add:
print_r($_POST);

Then run an update, view the source of the page and past the results in here.
0
 

Author Comment

by:DS928
Comment Utility
index.php is the main form after you click on "Update" form.php opens up.  When you click on "Uate Data" formsubmit.php takes over. Changed Get to Request on formsubmit.php

Array ( [recordId] => 13 [recordSite] => You Tube [recordUser] => Jerry [recordPass] => Jerry12345 [Submit] => Update Data )
Warning: Cannot modify header information - headers already sent by (output started at /home/content/d/s/t/dstr3/html/Data/formsubmit.php:2) in /home/content/d/s/t/dstr3/html/Data/formsubmit.php on line 21
0
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
Okay, that looks promising. What's the query that's running on UPDATE (uncomment //echo $sql;) ?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
Also, it's been a long day - you don't need to use $_REQUEST instead of $_GET. That was a mistake on my part. The mode=update is in the query string on the final submission URL, so it -is- in $_GET, not in $_POST.
0
 

Author Comment

by:DS928
Comment Utility
This is the query...
 $sql="select recordId,recordSite,recordUser,recordPass from $branch order by recordId";

Open in new window

0
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
Sorry - I meant what is the SQL for the update query - the one commented out in this block:


} elseif($mode=="update") {
		  	$recordSite=$_POST["recordSite"];
			$recordUser=$_POST["recordUser"];
			$recordPass=$_POST["recordPass"];
			$sql="update $branch set recordSite='$recordSite',recordUser='$recordUser',recordPass='$recordPass' where recordId='$recordId'";
			//echo $sql;
			$result=mysql_query($sql,$connection) or die(mysql_error());
			header("location: index.php");
		  }

Open in new window

0
 

Author Comment

by:DS928
Comment Utility
Not quite sure what you mean.  Isn't the $sql right above it.  In any case I put it back in....

Try the link now...

http://www.davidschure.com/index.php

getting this error
int(8) string(28) "Undefined variable: recordId" string(50) "/home/content/d/s/t/dstr3/html/Data/formsubmit.php" int(16) array(14) { ["GLOBALS"]=> *RECURSION* ["_POST"]=> array(5) { ["recordId"]=> string(2) "13" ["recordSite"]=> string(8) "You Tube" ["recordUser"]=> string(5) "Jerry" ["recordPass"]=> string(12) "Jerry1234567" ["Submit"]=> string(11) "Update Data" } ["_GET"]=> array(1) { ["mode"]=> string(6) "update" } ["_COOKIE"]=> array(0) { } ["_FILES"]=> array(0) { } ["mrno"]=> int(1) ["db_name"]=> string(8) "Chevelle" ["connection"]=> resource(3) of type (mysql link) ["db"]=> bool(true) ["branch"]=> string(7) "records" ["mode"]=> string(6) "update" ["recordSite"]=> string(8) "You Tube" ["recordUser"]=> string(5) "Jerry" ["recordPass"]=> string(12) "Jerry1234567" } update records set recordSite='You Tube',recordUser='Jerry',recordPass='Jerry1234567' where recordId=''int(2) string(132) "Cannot modify header information - headers already sent by (output started at /home/content/d/s/t/dstr3/html/Data/formsubmit.php:16)" string(50) "/home/content/d/s/t/dstr3/html/Data/formsubmit.php" int(19) array(16) { ["GLOBALS"]=> *RECURSION* ["_POST"]=> array(5) { ["recordId"]=> string(2) "13" ["recordSite"]=> string(8) "You Tube" ["recordUser"]=> string(5) "Jerry" ["recordPass"]=> string(12) "Jerry1234567" ["Submit"]=> string(11) "Update Data" } ["_GET"]=> array(1) { ["mode"]=> string(6) "update" } ["_COOKIE"]=> array(0) { } ["_FILES"]=> array(0) { } ["mrno"]=> int(1) ["db_name"]=> string(8) "Chevelle" ["connection"]=> resource(3) of type (mysql link) ["db"]=> bool(true) ["branch"]=> string(7) "records" ["mode"]=> string(6) "update" ["recordSite"]=> string(8) "You Tube" ["recordUser"]=> string(5) "Jerry" ["recordPass"]=> string(12) "Jerry1234567" ["sql"]=> string(103) "update records set recordSite='You Tube',recordUser='Jerry',recordPass='Jerry1234567' where recordId=''" ["result"]=> bool(true) }
0
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
Ah, okay.

Underneath this line in the update section:
$recordPass=$_POST["recordPass"];

...add:
$recordId = $_POST["recordId"];
0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
Comment Utility
When I asked you to uncomment that $sql, it was to see what the FINAL query was, with all the values inside it, in case there were any values that were not defined. In this particular case, $recordId was not defined. It was in $_POST, but it was not a local variable.
0
 

Author Closing Comment

by:DS928
Comment Utility
You did it!!   Thank you!
0

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now