Jump to content

Update script gives error please help


wgoldschagg

Recommended Posts

HI, I have writtem this update script, but it gives me an error I dont understand as I am relatively new to programming in PHP. Please help.

 

My script:

 

<?php

 

if ($_POST['Submit2'] = 'Update Details')

{

 

\\access the database;

  include 'src.php';

 

 

\\Posted Data, In this case the name is Koos.;

$Name=$_POST['Name'];

$ClientNo=$_Post['ClientNo'];

       

        mysql_query("UPDATE client SET Name=$Name WHERE ClientNo='$ClientNo'") or die(mysql_error());

       

        header('Location: updated.php');

 

        exit;

}

 

Echo ('Error');

exit;

?>

 

This is the error I receive:

 

Unknown column 'Koos' in 'field list'

 

Please help.

 

Link to comment
Share on other sites

Please use


tags next time.

 

Change

mysql_query("UPDATE client SET Name=$Name WHERE ClientNo='$ClientNo'") or die(mysql_error());

 

to

 

mysql_query("UPDATE client SET Name='$Name' WHERE ClientNo='$ClientNo'") or die(mysql_error());

Link to comment
Share on other sites

You need to quote the $name:

<?php
if ($_POST['Submit2'] == 'Update Details')
{

\\access the database;
      include 'src.php';


\\Posted Data, In this case the name is Koos.;
     $Name=mysql_real_escape_string(stripslashes($_POST['Name']));
     $ClientNo=mysql_real_escape_string(stripslashes($_Post['ClientNo']));
     $query = "UPDATE client SET Name='$Name' WHERE ClientNo='$ClientNo'";
     mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());
        
        header('Location: updated.php');
   
        exit;
}

Echo ('Error');
exit;
?>

 

Ken

Link to comment
Share on other sites

The update code is:

 

<?php

 

if ($_POST['Submit2'] = 'Update Details')

{

  include 'src.php';

 

    $Name=mysql_real_escape_string(stripslashes($_POST['Name']));

    $ClientNo=mysql_real_escape_string(stripslashes($_Post['ClientNo']));

    $query = "UPDATE client SET Name='$Name' WHERE ClientNo='$ClientNo'";

    mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());

       

        header('Location: updated.php');

 

        exit;

}

 

Echo ('Error');

exit;

?>

 

 

The forms code is:

 

<?php 

       

include 'src.php';

 

$id = $_POST['ClientNo'];

 

$result = mysql_query("select * from client where $id = ClientNo");

 

$row = mysql_fetch_array($result); 

 

echo "<form name=\"form2\" method=\"post\" action=\"update.php\" style=\"padding:5px;\">\n";

    echo "<tr><td style=\"padding:5px;\">Client Number: </td><td><input type=\"hidden\" name=\"ClientNo\" id=\"ClientNo\" value=\"$row[ClientNo]\">$row[ClientNo]</td></tr>\n";

 

echo "<tr><td style=\"padding:5px;\">Name: </td><td><input type=\"text\" name=\"Name\" id=\"Name\" value=\"$row[Name]\"></td></tr>\n";

 

echo "<tr><td colspan=\"2\"><center><input type=\"submit\" name=\"submit2\" value=\"Update Details\"></center></td></tr>\n";

 

?>

 

 

The form recalls the correct data - and posts the correct data and even goes to updated.php - the script just doesn't do the update - it keeps the old data.

Link to comment
Share on other sites

It makes no difference - i am going to try a different approach - dropping the data from the table and them inserting the new data.

 

Think its worth a shot.

 

Not really.  Would it help if any of us showed you one of the thousands of websites we've developed?  You have a bug in your code somewhere.  If you don't figure out what it is, you might as well just give up on learning PHP. 

 

I don't mean this to come off as insulting, but "dropping data from the table" whatever you mean by that, so that you can accomplish an update of a value in a single column of an existing table, is absolutely incorrect.

 

If, you would like to provide us *all the scripts you are working with in their current form* and the describe of the table, someone here will probably figure out what your error is.

Link to comment
Share on other sites

looking back at your original error (which i didn't even notice beforehand), you're not commenting your work properly .. \\ is incorrect .. // or # are correct ways to comment in PHP (as well as /* stuff here... */) .. that is what was giving you your original error.

 

and if you copied kenrbnsn's post, the comment tags were omitted altogether (accidental, i'm sure).

 

seems you've gotten rid of the comments since then.

 

what is this?

$id = $_POST['ClientNo'];

$result = mysql_query("select * from client where $id = ClientNo")

how is that working?

Link to comment
Share on other sites

For practical puroses I have reverted ALL my code to work with only 2 Database Variables (ClientNo & Name). Note that each section of code is a seperate file.

 

Here is ALL the Code:

 

<------Search Page ( Working Correctly )------------->

 

<form action="search.php" method="post"> 

    Search By <select name="catag" id="catag">

            <option value="Name">Name</option>

</select>: <input type="text" name="term" /><br />

    <p> 

    <input type="submit" name="submit" value="Submit" /> 

    </form>

 

<---------Displaying Results & Update Button (search.php)---------->

 

<?php 

       

include 'src.php';

 

$term = $_POST['term']; 

$catag = $_POST['catag'];

 

$sql = mysql_query("select * from client where $catag like '%$term%'");

 

while ($row = mysql_fetch_array($sql)){ 

 

    echo "<tr><td style=\"padding:5px;\">Client Number: </td><td>$row[ClientNo] </td><td valign=\"center\" rowspan=\"2\"><form action=\"preupdate.php\" method=\"post\"><input type=\"hidden\" name=\"ClientNo\" value=\"$row[ClientNo]\"><input type=\"submit\" name=\"submit\" value=\"Update Details\" /></form></td></tr>\n"; 

    echo "<tr><td style=\"padding:5px;\">Name: </td><td>$row[Name] </td></tr>\n";

 

    } 

   

?>

 

<----------Displaying the Selected Record in Text Fields exept ClientNo (preupdate.php)-------->

 

 

<?php 

       

include 'src.php';

 

$id = $_POST['ClientNo'];

 

$result = mysql_query("select * from client where $id = ClientNo");

 

$row = mysql_fetch_array($result);

 

echo "<form name=\"form2\" method=\"post\" action=\"update.php\" style=\"padding:5px;\">\n";

    echo "<tr><td style=\"padding:5px;\">Client Number: </td><td><input type=\"hidden\" name=\"ClientNo\" id=\"ClientNo\" value=\"$row[ClientNo]\">$row[ClientNo]</td></tr>\n"; 

    echo "<tr><td style=\"padding:5px;\">Name: </td><td><input type=\"text\" name=\"Name\" id=\"Name\" value=\"$row[Name]\"></td></tr>\n";

 

echo "<tr><td colspan=\"2\"><center><input type=\"submit\" name=\"submit2\" value=\"Update Details\"></form></center></td></tr></table>\n";

 

<----------The Actual Update (update.php)----------->

 

<?php

 

if ($_POST['submit2'] == 'Update Details')

{

  include 'src.php';

 

    $Name=mysql_real_escape_string(stripslashes($_POST['Name']));

    $ClientNo=mysql_real_escape_string(stripslashes($_Post['ClientNo']));

    $query = "UPDATE client SET Name='$Name' WHERE ClientNo like '$ClientNo'";

    mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());

       

        header('Location: updated.php');

 

        exit;

}

 

Echo ('Error');

exit;

?>

 

 

All this works exept the actual update - the database still keeps the old data.

Link to comment
Share on other sites

$_POST should be capitalised:

     $ClientNo=mysql_real_escape_string(stripslashes($_POST['ClientNo']));

 

And while you're there change the LIKE to an = (unless you did really mean LIKE)

     $query = "UPDATE client SET Name='$Name' WHERE ClientNo = '$ClientNo'";

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.