Solved

Record Not Updating

Posted on 2015-01-27
14
84 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
ID: 40573922
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
ID: 40573940
Nope.  It didn't work.
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 40573970
Can you post all the code?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:DS928
ID: 40574003
<!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
ID: 40574020
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
ID: 40574051
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
ID: 40574092
Okay, that looks promising. What's the query that's running on UPDATE (uncomment //echo $sql;) ?
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 40574096
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
ID: 40574133
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
ID: 40574145
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
ID: 40574162
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
ID: 40574191
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
ID: 40574195
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
ID: 40574212
You did it!!   Thank you!
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

777 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