Any idea what I did wrong?

'WHERE id =' at line 16

I am not sure why it says line 16 but I think it is talking a bout line 64.

<?php

include 'resources/init.php';
if (isset ($_GET['id'])) {
    $q = "SELECT * FROM Client WHERE id = {$_GET['id']}";
    $result = mysql_query($q);
    $client = mysql_fetch_array($result);
    }
?>
    <a href="index.php">Back To List</a>
  
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
First Name: <input type="text" name="firstname" value="<?php echo $client['firstname']; ?>" />
Last Name: <input type="text" name="lastname" value="<?php echo $client['lastname']; ?>" />
<br>
<br>
Email: <input type="text" name="email" value="<?php echo $client['email']; ?>" />
<br>
<br>
Invoice #:<input type="text" name="invoice" maxlength="5" size="5" value="<?php echo $client['invoice']; ?>" />
<br>
<br>
<br>
<br>
Windows Key:<input type="text" name="wink" maxlength="24" size="24" value="<?php echo $client['wink']; ?>" />
Windows Type:<input type="text" name="wint" maxlength="24" size="24" value="<?php echo $client['wint']; ?>" />
<br>
<br>
<br>
<br>
VIPRE Type:<input type="text" name="vtype" maxlength="3" size="3" value="<?php echo $client['vtype']; ?>" />
User Count:<input type="text" name="usera" maxlength="3" size="3" value="<?php echo $client['usera']; ?>" />
Year Count:<input type="text" name="yeara" maxlength="3" size="3" value="<?php echo $client['yeara']; ?>" />
<br>
<br>
Start Date:<input type="text" name="sdate" maxlength="10" size="10" value="<?php echo $client['sdate']; ?>" />
Expire Date:<input type="text" name="edate" maxlength="10" size="10" value="<?php echo $client['edate']; ?>" />
<br>
<br>
VIPRE Key: <input type="text" name="viprek" maxlength="24" size="24" value="<?php echo $client['viprek']; ?>" />
<br>
<br>
<br>
<input type="submit" name="submit" Value="Modify" />
</form>
    
    <?php
        if(isset($_POST["submit"])) {
            $u = "UPDATE Client SET
        
                `firstname`='$_POST[firstname]',
                `lastname`='$_POST[lastname]',
                `email`='$_POST[email]',
                `invoice`='$_POST[invoice]',
                `wink`='$_POST[wink]',
                `wint`='$_POST[wint]',
                `vtype`='$_POST[vtype]',
                `usera`='$_POST[usera]',
                `yeara`='$_POST[yeara]',
                `sdate`='$_POST[sdate]',
                `edate`='$_POST[edate]',
                `viprek`='$_POST[viprek]', 
        
            WHERE id = {$_POST['id']}";
        
        mysql_query($u) or die (mysql_error());
        
        echo "User has been modified!";
            header("Location: index.php");
    }
    ?>

Try removing the coma on line 62 first, if that doesn't work remove the curly brackets on line 64 and put apostrophes instead in addition to my first suggestion. Let me know how you get on.

@Que336: The comma (62) is the issue, the curly brackets (64) are fine.

I have been playing round with code a lot but I am having not much luck. :\

You have a mixup. You are using GET on line 5, yet POST on line 64. One of them is wrong.

I agree with pritaeas, from the code it looks like you are not using the $_GET[] correctly,as well as the comma problem in line 62. You typically use get when you are passing the variable in the url ie (.../myphpscript.php?id=11243) the $_GET will be 11243. The &_POST[] is used for passing information to the page from a form. You should switch the $_GET with $_POST at the top unless you are passing a variable via URL. Since you are calling the form to process itself you should change this.

{redacted}

This is my code as it stands now..

<?php
include_once 'resources/init.php';
if (isset ($_GET[id])) {
    $q = "SELECT * FROM Client WHERE id = {$_GET[id]}";
    $result = mysql_query($q);
    $client = mysql_fetch_array($result);
    }
?>
    <a href="index.php">Back To List</a>
  
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
First Name: <input type="text" name="firstname" value="<?php echo $client['firstname']; ?>" />
Last Name: <input type="text" name="lastname" value="<?php echo $client['lastname']; ?>" />
<br>
<br>
Email: <input type="text" name="email" value="<?php echo $client['email']; ?>" />
<br>
<br>
Invoice #:<input type="text" name="invoice" maxlength="5" size="5" value="<?php echo $client['invoice']; ?>" />
<br>
<br>
<br>
<br>
Windows Key:<input type="text" name="wink" maxlength="24" size="24" value="<?php echo $client['wink']; ?>" />
Windows Type:<input type="text" name="wint" maxlength="24" size="24" value="<?php echo $client['wint']; ?>" />
<br>
<br>
<br>
<br>
VIPRE Type:<input type="text" name="vtype" maxlength="3" size="3" value="<?php echo $client['vtype']; ?>" />
User Count:<input type="text" name="usera" maxlength="3" size="3" value="<?php echo $client['usera']; ?>" />
Year Count:<input type="text" name="yeara" maxlength="3" size="3" value="<?php echo $client['yeara']; ?>" />
<br>
<br>
Start Date:<input type="text" name="sdate" maxlength="10" size="10" value="<?php echo $client['sdate']; ?>" />
Expire Date:<input type="text" name="edate" maxlength="10" size="10" value="<?php echo $client['edate']; ?>" />
<br>
<br>
VIPRE Key: <input type="text" name="viprek" maxlength="24" size="24" value="<?php echo $client['viprek']; ?>" />
<input type="text" name="id" value="<?php echo $client['id']; ?>" />
<br>
<br>
<br>
<input type="submit" Value="Modify" />
</form>
    
    <?php
if (isset($_POST["submit"])) {
    
       $u = "UPDATE Client SET 

        `firstname`='{$_POST['firstname']}',
        `lastname`='{$_POST['lastname']}',
        `email`='{$_POST['email']}',
        `invoice`='{$_POST['invoice']}',
        `wink`='{$_POST['wink']}',
        `wint`='{$_POST['wint']}',
        `vtype`='{$_POST['vtype']}',
        `usera`='{$_POST['usera']}',
        `yeara`='{$_POST['yeara']}',
        `sdate`='{$_POST['sdate']}',
        `edate`='{$_POST['edate']}',
        `viprek`='{$_POST['viprek']}'

    WHERE id = {$_GET['id']}";
        
        mysql_query($u) or die (mysql_error());
        
            header("Location: index.php");
    }

    mysql_close($con)

?>

It complains about this snip-it right here.

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

I also tried moving the update code to another page and that worked a little better. No more errors but it just updated all my current data with the same thing.. Like it wrote all the IDs. I am reading about proper structure of PHP code and seeing if I can learn anything more by surfing the tutorials around the web.

What I have done in the past for this is have a page that passes on the specific record to be edited to the edit page.

On the edit page I have a sql statement to get the current information on the record. then print it. under that there is a form that actions to a verification page.

it would visually look like :
Record to Edit:
ID :"dynamically produced id"
type: "Dynamically produced type"
Size: "dynamically produced size"
etc
hr tag to separate the edited item from the new information.
form for editing action to a verification page
box for new type
box for new size
submit/reset buttons


The verification page checks to see if there was anything entered in the boxes, if so then it sets it, if not it only sets the items that were altered. I use if then statements for this usually to append the sql query, then it runs and informs the user, and the user then has the option of editing another record or going back to a menu.

a sample of code from this sort of set up is below. (this was used to populate an edit record)

<?php
//drug_1 
$sql = "SELECT user.drug_1, user_doc.doc_id, user_doc.type, user_doc.level, user_doc.path, user_doc.upload_date, user_doc.start_date  FROM user, user_doc WHERE user.drug_1 = user_doc.doc_id AND id = $id"; 

$result = mysqli_query($mysqli,$sql) or die("MySQL error: " . mysqli_error($mysqli) . "<hr>\nQuery: $sql"); ;

while($row = mysqli_fetch_array($result, MYSQLI_NUM))
{	
	if($row[0] != 0){
	?>
    <table>
    <tr><td>Start Date: </td><td><?php echo $row[6]; ?></td></tr>
    <tr><td>File: </td><td><a href="<?php echo $row[4]; ?>" target="_blank">DOC</a></td></tr>
    </table>
    <?php
	}
}
mysqli_free_result($result);
?>

Alright, well I have the mysql database now updating. I did the following.

Modify.php

<?php
include_once 'resources/init.php';
if (isset ($_GET[id])) {
    $q = "SELECT * FROM Client WHERE id = {$_GET[id]}";
    $result = mysql_query($q);
    $client = mysql_fetch_array($result);
    }
?>
    <a href="index.php">Back To List</a>
  
<form action="edit.php" method="post">
First Name: <input type="text" name="firstname" value="<?php echo $client['firstname']; ?>" />
Last Name: <input type="text" name="lastname" value="<?php echo $client['lastname']; ?>" />
<br>
<br>
Email: <input type="text" name="email" value="<?php echo $client['email']; ?>" />
<br>
<br>
Invoice #:<input type="text" name="invoice" maxlength="5" size="5" value="<?php echo $client['invoice']; ?>" />
<br>
<br>
<br>
<br>
Windows Key:<input type="text" name="wink" maxlength="24" size="24" value="<?php echo $client['wink']; ?>" />
Windows Type:<input type="text" name="wint" maxlength="24" size="24" value="<?php echo $client['wint']; ?>" />
<br>
<br>
<br>
<br>
VIPRE Type:<input type="text" name="vtype" maxlength="3" size="3" value="<?php echo $client['vtype']; ?>" />
User Count:<input type="text" name="usera" maxlength="3" size="3" value="<?php echo $client['usera']; ?>" />
Year Count:<input type="text" name="yeara" maxlength="3" size="3" value="<?php echo $client['yeara']; ?>" />
<br>
<br>
Start Date:<input type="text" name="sdate" maxlength="10" size="10" value="<?php echo $client['sdate']; ?>" />
Expire Date:<input type="text" name="edate" maxlength="10" size="10" value="<?php echo $client['edate']; ?>" />
<br>
<br>
<input type="hidden" name="id" value="<?php echo $client['id']; ?>" />
VIPRE Key: <input type="text" name="viprek" maxlength="24" size="24" value="<?php echo $client['viprek']; ?>" />
<br>
<br>
<br>
<input type="submit" name="submit" Value="Modify" />
</form>

edit.php

<?php
    include 'resources/init.php';
    
        $sql="UPDATE Client SET `firstname`='{$_POST['firstname']}',
        
       `lastname`='{$_POST['lastname']}',
       `email`='{$_POST['email']}',
       `invoice`='{$_POST['invoice']}',
       `wink`='{$_POST['wink']}',
       `wint`='{$_POST['wint']}',
       `vtype`='{$_POST['vtype']}',
       `usera`='{$_POST['usera']}',
       `yeara`='{$_POST['yeara']}',
       `sdate`='{$_POST['sdate']}',
       `edate`='{$_POST['edate']}',
       `viprek`='{$_POST['viprek']}'
        
        WHERE id = '{$_POST['id']}'";
        
if (!mysql_query($sql))
  {
  die('Error: ' . mysql_error());
  }
header("Location: index.php");

mysql_close($con)
?>

The only issue now is after I summit my data I get a white screen. With the weblog error of.


[01-Feb-2012 10:16:44] PHP Warning: Cannot modify header information - headers already sent by (output started at /home5/arkamanc/public_html/vipre/edit.php:1) in /home5/arkamanc/public_html/vipre/edit.php on line 24

I would delete the line 24 and actually put a full web page under the php script with a message that the updates completed successfully or not then give the user the option to go to another page via link. This helps assure a person that the settings have changed and that their editing was fruitful. You could go one step further and print out the edited version of what was edited so they can see the changes. Small things like this let less savvy computer users and scatterbrained computer users realize what they have done, and allow them the time to review for errors should the edit have failed.

I would delete the line 24 and actually put a full web page under the php script with a message that the updates completed successfully or not then give the user the option to go to another page via link. This helps assure a person that the settings have changed and that their editing was fruitful. You could go one step further and print out the edited version of what was edited so they can see the changes. Small things like this let less savvy computer users and scatterbrained computer users realize what they have done, and allow them the time to review for errors should the edit have failed.

How would I format the URL, thanks BTW.

Never mind I got it. Alright well looks like this issue has been solved. :D


THANK YOU FOR ALL OF THE HELP EVERYONE! :D

Your web page would look like this:

edit.php

<?php
    include 'resources/init.php';
     
    $sql="UPDATE Client SET `firstname`='{$_POST['firstname']}',
     
    `lastname`='{$_POST['lastname']}',
    `email`='{$_POST['email']}',
    `invoice`='{$_POST['invoice']}',
    `wink`='{$_POST['wink']}',
    `wint`='{$_POST['wint']}',
    `vtype`='{$_POST['vtype']}',
    `usera`='{$_POST['usera']}',
    `yeara`='{$_POST['yeara']}',
    `sdate`='{$_POST['sdate']}',
    `edate`='{$_POST['edate']}',
    `viprek`='{$_POST['viprek']}'
     
    WHERE id = '{$_POST['id']}'";
     
    if (!mysql_query($sql))
    {
    die('Error: ' . mysql_error());
    }
   
     
    mysql_close($con)
    ?>
<!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>Edit Page</title>
</head>

<body>
<?php
//here you could put a message generated by the code above if you had some verification script running.

?><p>Record Updated!<br/>
<a href="index.php"> Return to menu page</a><br/>
<a href="modify.php"> Modify another record</a>
</p>

</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.