Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP Code Not Working as Expected

Posted on 2011-03-14
3
Medium Priority
?
309 Views
Last Modified: 2012-05-11
I have this PHP code that pulls data from my MYSQL database & displays it in a browser. At the end of the record, in the browser, there is a "Delete" & a "Edit" link. The "Delete" link works perfect. It deletes the record it is supposed to. If I click on the "Edit" link, it takes me to the edit.php form but it is not the record I need to edit, it is the first record in the database. Can someone help me out?
<table>
      <thead>
      <tr>
	   <table border='7'>

<th>Customer No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Status</th>
<th>Delete</th>
<th>Edit</th>
      </tr>      
      </thead>
      <tbody>
<?php
require('connection.php');


if (isset($_GET['op']) && $_GET['op'] == "d") 
if($_GET['op'] == "d" && !empty($_GET['id']) )
{
   $query="DELETE FROM `psrinfo` WHERE `id`=" . $_GET['id'];
   $result = mysql_query($query) or die(mysql_error());  
}

$query="SELECT id, pacts, fname, lname, status FROM psrinfo";
$result = mysql_query($query) or die(mysql_error());  
 
while($row = mysql_fetch_array( $result )) {
?>
       <tr>
           
			<td><?php echo "".$row['pacts']; ?></td>
            <td><?php echo "".$row['fname']; ?></td>
            <td><?php echo "".$row['lname']; ?></td>
			<td><?php echo "".$row['status']; ?></td>
            <td><a href="<?php echo $_SERVER['PHP_SELF'];?>?op=d&id=<?php echo $row['id'];?>">delete</a></td>
		    <td><a href="edit.php"><center>edit</center></a></td>
      </tr>
<?php } ?>            
      </tbody>
 
</table>

Open in new window

0
Comment
Question by:wantabe2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 1000 total points
ID: 35129127
change
<td><a href="edit.php"><center>edit</center></a></td>
to
<td><a href="edit.php?op=e&id=<?php echo $row['id'];?>"><center>edit</center></a></td>

...and take care about operations

if (isset($_GET['op'])) 
if($_GET['op'] == "d" && !empty($_GET['id']) )
{
   $query="DELETE FROM `psrinfo` WHERE `id`=" . $_GET['id'];
   $result = mysql_query($query) or die(mysql_error());  
}
elseif($_GET['op'] == "e" && !empty($_GET['id']))
{
  /// edit the record
}

Open in new window

0
 
LVL 15

Author Comment

by:wantabe2
ID: 35129296
I can hover over the "edit" link & in my browser it shows the correct ID that I need to edit but when I click it it still takes me to the first record in the database...any ideas? I've attached the edit.php code.
<?php # edit_psrflow.php 

$page_title = 'Edit a Record'; 

$con = mysql_connect("localhost","username","password"); 
if (!$con) 
  { 
  die('Could not connect: ' . mysql_error()); 
  } 

mysql_select_db("psrflow", $con); 

$result = mysql_query("SELECT * FROM psrinfo "); 

// Check if the form has been submitted. 

if (isset($_POST['submitted'])) { 

    $errors = array(); // Initialize error array. 
     
    if (empty($errors)) { // If everything's OK. 
     
        // Make the query. 
        $query = "UPDATE psrinfo SET pacts='$pacts', fname='$fname', lname='$lname', status='$status' WHERE id=$id"; 
        $result = @mysql_query ($query); // Run the query. 

$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); 
if ((substr($url, -1) == '/') OR (substr($url, -1) == '//')) { 
$url = substr ($url, 0, -1); 
} 
$url .='/view_ts.php'; 
header("Location: $url"); 
exit(); 

         
    } else { // Report the errors. 
     
        echo '<h1 id="mainhead">Error!</h1> 
        <p class="error">The following error(s) occurred:<br />'; 
        foreach ($errors as $msg) { // Print each error. 
            echo " - $msg<br />\n"; 
        } 
        echo '</p><p>Please try again.</p><p><br /></p>'; 
         
    } // End of if (empty($errors)) IF. 

} // End of submit conditional. 

// Retrieve the user's information. 
$query = "SELECT pacts, fname, lname, status FROM psrinfo"; 
$result = @mysql_query ($query); // Run the query. 

list($pacts, $fname, $lname, $status) = mysql_fetch_array($result, MYSQL_NUM); 

?>     

<script type="text/javascript"> 
var valid; 

function d2(v) { return (v<10)?("0"+v):v; } 

function dcheck(form) { 
var a = form.assgn_date.value; 
var s = form.sent_date.value; 
var i = form.interv_date.value; 
var dr = form.due_rev.value 
var su = form.due_suspo 
var clk = form.due_clerk 
var att = form.due_super 
var jdg = form.due_owner 
var assn  = new Date(a); 
var sent = new Date(s); 
var intv = new Date(i); 
var due_rev = new Date(dr); 
var due_suspo = new Date(su); 
var due_clerk = new Date(clk); 
var due_super = new Date(sup); 
var due_owner = new Date(own); 


if (isNaN(intv)) { 
intv = new Date(assn.getFullYear(),assn.getMonth(),assn.getDate()+0); 
} 
if (isNaN(assn)) { 
assn = new Date(assn.getFullYear(),assn.getMonth(),assn.getDate()+0); 
} 
if (isNaN(due_rev)) { 
due_rev = new Date(sent.getFullYear(),sent.getMonth(),sent.getDate()-42); 
} 
if (isNaN(due_suspo)) { 
due_suspo = new Date(sent.getFullYear(),sent.getMonth(),sent.getDate()-40); 
} 
if (isNaN(due_clerk)) { 
due_clerk = new Date(sent.getFullYear(),sent.getMonth(),sent.getDate()-38); 
} 
if (isNaN(due_super)) { 
due_super = new Date(sent.getFullYear(),sent.getMonth(),sent.getDate()-36); 
} 
if (isNaN(due_owner)) { 
due_owner = new Date(sent.getFullYear(),sent.getMonth(),sent.getDate()-7); 
} 


switch(due_rev.getDay()){ 
  case 0: due_rev.setDate(due_rev.getDate() - 1); // take one for Sunday 
  case 6: due_rev.setDate(due_rev.getDate() - 1); // take two for Sunday or one for Saturday 
} 

switch(due_suspo.getDay()){ 
  case 0: due_suspo.setDate(due_suspo.getDate() - 1); // take one for Sunday 
  case 6: due_suspo.setDate(due_suspo.getDate() - 1); // take two for Sunday or one for Saturday 
} 

switch(due_clerk.getDay()){ 
  case 0: due_clerk.setDate(due_clerk.getDate() - 1); // take one for Sunday 
  case 6: due_clerk.setDate(due_clerk.getDate() - 1); // take two for Sunday or one for Saturday 
} 

switch(due_super.getDay()){ 
  case 0: due_super.setDate(due_super.getDate() - 1); // take one for Sunday 
  case 6: due_super.setDate(due_super.getDate() - 1); // take two for Sunday or one for Saturday 
} 

switch(due_owner.getDay()){ 
  case 0: due_owner.setDate(due_owner.getDate() - 1); // take one for Sunday 
  case 6: due_owner.setDate(due_owner.getDate() - 1); // take two for Sunday or one for Saturday 
} 

switch(due_rev.getDay()){ 
  case 0: due_rev.setDate(due_rev.getDate() - 1); // take one for Sunday 
  case 6: due_rev.setDate(due_rev.getDate() - 1); // take two for Sunday or one for Saturday 
} 

form.assgn_date.value = (assn.getFullYear()+0) + "-" + d2(assn.getMonth()+1) + "-" + d2(assn.getDate()); 
form.interv_date.value = (intv.getFullYear()+0) + "-" + d2(intv.getMonth()+1) + "-" + d2(intv.getDate()); 
form.sent_date.value = (sent.getFullYear()+0) + "-" + d2(sent.getMonth()+1) + "-" + d2(sent.getDate()); 
form.due_rev.value = (due_rev.getFullYear()+0) + "-" + d2(due_rev.getMonth()+1) + "-" + d2(due_rev.getDate()); 
form.due_suspo.value = (due_suspo.getFullYear()+0) + "-" + d2(due_suspo.getMonth()+1) + "-" + d2(due_suspo.getDate()); 
form.due_clerk.value = (due_clerk.getFullYear()+0) + "-" + d2(due_clerk.getMonth()+1) + "-" + d2(due_clerk.getDate()); 
form.due_super.value = (due_super.getFullYear()+0) + "-" + d2(due_super.getMonth()+1) + "-" + d2(due_super.getDate()); 
form.due_owner.value = (due_owner.getFullYear()+0) + "-" + d2(due_owner.getMonth()+1) + "-" + d2(due_owner.getDate()); 
return true; 
} 

</script> 

<form action="edit_psrflow.php" method="post"> 
<fieldset><legend><h1> You are editing a record!</h1></legend> 

<b>Cust No:</b> <br><input type="text" name="pacts" size="15" maxlength="30" value="<?php echo $pacts; ?>" /><br> 
<b>First Name:</b> <br><input type="text" name="fname" size="15" maxlength="30" value="<?php echo $fname; ?>" /><br /> 
<b>Last Name:</b> <br><input type="text" name="lname" size="15" maxlength="30" value="<?php echo $lname; ?>" /><br /> 
<b>Status: </b><br><input type="text" name="status" size="15" maxlength="30" value="<?php echo $status; ?>" /> <br> 


<p><input type="button" value="Calculate" onclick="return dcheck(this.form);">    <b> DO NOT enter anything below this line. Click the "Calculate" Button.</b></p> 

<b>-----------------------------------------------------------</b> <br /> 

<b>Due to Reviewer:</b><br><input type="text" name="due_rev" size="15" maxlength="30" value="<?php echo $due_rev; ?>" /> <br> 
<b>Due to SUSPO:</b><br><input type="text" name="due_suspo" size="15" maxlength="30" value="<?php echo $due_suspo; ?>" /> <br> 
<b>Due to Clerk:</b><br><input type="text" name="due_clerk" size="15" maxlength="30" value="<?php echo $due_clerk; ?>" /> <br> 
<b>Due to Supervisor:</b><br><input type="text" name="due_super" size="15" maxlength="30" value="<?php echo $due_super; ?>" /> <br> 
<b>Due to Owner:</b><br><input type="text" name="due_owner" size="15" maxlength="30" value="<?php echo $due_owner; ?>" /> <br> 
<br> 

</fieldset> 
<input type="hidden" name="submitted" value="TRUE" /> 

<div align="left"><input type="submit" name="submit" value="Submit" /></div> 

</form> 
<?php 
mysql_close(); // Close the database connection. 

?>

Open in new window

0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 1000 total points
ID: 35129455
In your original code fragment you have this

if (isset($_GET['op']) && $_GET['op'] == "d") 
if($_GET['op'] == "d" && !empty($_GET['id']) )
{
   $query="DELETE FROM `psrinfo` WHERE `id`=" . $_GET['id'];
   $result = mysql_query($query) or die(mysql_error());  
}

Open in new window


That looks to me like you are retrieving a record for deletion (op == d && and id code) but I did not notice anything similar for 'edit'

In your second frament you have a line

 $query = "UPDATE psrinfo SET pacts='$pacts', fname='$fname', lname='$lname', status='$status' WHERE id=$id"; 

Open in new window


but $id has not been set so it will be zero which means the WHERE clause selects record zero - the first record.

You need to set $id
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

636 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