Jump to content

can someone help me get this code working please?


jamesyrawr

Recommended Posts

here are the three pages i am using to alter information stored in my database

 

 

 

editrow.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=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<table> 
  <tr> 
    <td align="center">EDIT DATA</td> 
  </tr> 
  <tr> 
    <td> 
      <table border="1"> 
      <?php 
      include"include/connect.php";//database connection 
      $order = "SELECT * FROM products"; 
      $result = mysql_query($order); 
      while ($row=mysql_fetch_array($result)){ 
                                                               
      print "    <td>" . $row["prod_id"] . "</td>";                  
      print "    <td>" . $row["prod_name"]. "</td>";
   print "    <td>" . $row["prod_price"] . "</td>";                  
      print "    <td>" . $row["prod_desc"]. "</td>";  
   print "    <td>" . $row["prod_colour"] . "</td>";  
        print ("<td><a href=\"editform.php?id=$row[prod_id]\">Edit</a></td></tr>"); 
      } 
      ?> 
      </table> 
    </td> 
   </tr> 
</table>
</body>
</html>

 

then

editform.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=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<table border=1> 
  <tr> 
    <td align=center>Form Edit Employees Data</td> 
  </tr> 
  <tr> 
    <td> 
      <table> 
      <?php 
      include "include/connect.php";//database connection 
	  $id = $_GET['id'];
      $order = "SELECT * FROM products  
	WHERE prod_id='$id'"; 
      $result = mysql_query($order); 
      $row = mysql_fetch_array($result); 
      ?> 
      <form method="post" action="include/updatedata.php"> 
      <input type="hidden" name="prodid" value="<? echo "$row[prod_id]"?>"> 
        <tr>         
          <td>Name</td> 
          <td> 
            <input type="text" name="prodname" 
        size="20" value="<?php echo "$row[prod_name]"?>"> 
          </td> 
        </tr> 
        <tr> 
          <td>Address</td> 
          <td> 
            <input type="text" name="prodprice" size="40" 
          value="<?php echo "$row[prod_price]"?>"> 
          </td> 
        </tr>
		 <tr> 
          <td>Address</td> 
          <td> 
            <input type="text" name="proddesc" size="40" 
          value="<?php echo "$row[prod_desc]"?>"> 
          </td> 
        </tr>
        <tr> 
          <td>Address</td> 
          <td> 
            <input type="text" name="prodcolour" size="40" 
          value="<?php echo "$row[prod_colour]"?>"> 
          </td> 
        </tr>   
        <tr> 
          <td align="right"> 
            <input type="submit" 
          name="submit value" value="Edit"> 
          </td> 
        </tr> 
      </form> 
      </table> 
    </td> 
  </tr> 
</table> 
</body>
</html>

 

 

now here it all goes weird

i get this error

Notice: Undefined index: $id in C:\wamp\www\uniwork\include\updatedata.php on line 4

but without it it doesnt know how to get the information from the previous form.

include/updatedata.php

<?php 
//edit_data.php 
include "connect.php";
$id=$_GET['$id'];

$order = "UPDATE products  
          SET prod_name='prodname',  
              prod_price='prodprice',
			  prod_desc='proddesc',
			  prod_colour='prodcolour' 
          WHERE  
          prod_id='$id'"; 
mysql_query($order); 
header("location:updatedata.php"); 
?> 

Should have spotted this earlier, sorry.

 

<form method="post" action="include/updatedata.php">

 

Change it to:

<form method="get" action="include/updatedata.php">

 

$_GET takes values from the requested address.

For example, updatedata.php?id=1 would set $_GET['id'] to 1. Submitting a form using post would set the values in the $_POST global array. $_REQUEST is a combination of both, although I steer clear of it.

ahh thanks

learned something there :)

but now im getting this error

Parse error: parse error in C:\wamp\www\uniwork\include\updatedata.php on line 4

in this piece of code

<?php 
//edit_data.php 
include "connect.php";
$prod $_GET['id'];

$order = "UPDATE products  
          SET prod_name='prodname',  
              prod_price='prodprice',
			  prod_desc='proddesc',
			  prod_colour='prodcolour' 
          WHERE  
          prod_id='$prod'"; 
mysql_query($order); 
header("location:updatedata.php"); 
?> 

 

 

Since the query can't run properly without the value of $_GET['id'], use a conditional to check if $_GET['id'] is set before allowing it to.

 

if( isset($_GET['id']) ) {
include "connect.php";
$prod = $_GET['id']; // if $_GET['id'] is expected to always be an integer, use $prod = (int) $_GET['id'];

$order = "UPDATE products  
          SET prod_name='prodname',  
              prod_price='prodprice',
			  prod_desc='proddesc',
			  prod_colour='prodcolour' 
          WHERE  
          prod_id='$prod'"; 
mysql_query($order); 
header("location:updatedata.php"); 
}

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.