wgoldschagg Posted April 21, 2009 Share Posted April 21, 2009 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. Quote Link to comment Share on other sites More sharing options...
Mchl Posted April 21, 2009 Share Posted April 21, 2009 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()); Quote Link to comment Share on other sites More sharing options...
soak Posted April 21, 2009 Share Posted April 21, 2009 Put quotes around $Name and escape your data: mysql_query("UPDATE client SET Name='".mysql_real_escape_string($Name)."' WHERE ClientNo='".mysql_real_escape_string($ClientNo)."'") or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 21, 2009 Share Posted April 21, 2009 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 Quote Link to comment Share on other sites More sharing options...
wgoldschagg Posted April 21, 2009 Author Share Posted April 21, 2009 Trying it now KEN, will let you know what happens. Thanks Quote Link to comment Share on other sites More sharing options...
soak Posted April 21, 2009 Share Posted April 21, 2009 There's a syntax error in your file somewhere. Either turn on error reporting or run php -l filename.php Quote Link to comment Share on other sites More sharing options...
wgoldschagg Posted April 21, 2009 Author Share Posted April 21, 2009 I did what Ken suggested. The script runs now- but it doesnt do the update. Any suggestions? Quote Link to comment Share on other sites More sharing options...
Mchl Posted April 21, 2009 Share Posted April 21, 2009 http://www.phpfreaks.com/tutorial/debugging-a-beginners-guide/page1 Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 21, 2009 Share Posted April 21, 2009 post your most recent code, along with the form. Quote Link to comment Share on other sites More sharing options...
wgoldschagg Posted April 21, 2009 Author Share Posted April 21, 2009 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. Quote Link to comment Share on other sites More sharing options...
Mchl Posted April 21, 2009 Share Posted April 21, 2009 Possibly it doesn't find any matches for given ClientNo Quote Link to comment Share on other sites More sharing options...
wgoldschagg Posted April 21, 2009 Author Share Posted April 21, 2009 this may sound silly - but i am going to change WHERE ClientNo='$ClientNo'"; to WHERE ClientNo like '$ClientNo'"; see what it does. the select statement work perfectly with WHERE ClientNo like '$ClientNo'"; to recall the data. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 21, 2009 Share Posted April 21, 2009 can you spot the difference? if ($_POST['Submit2'] = 'Update Details') and <input type=\"submit\" name=\"submit2\" value=\"Update Details\"> Quote Link to comment Share on other sites More sharing options...
wgoldschagg Posted April 21, 2009 Author Share Posted April 21, 2009 A Simple Letter Case - will try quick thanks Quote Link to comment Share on other sites More sharing options...
wgoldschagg Posted April 21, 2009 Author Share Posted April 21, 2009 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. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 21, 2009 Share Posted April 21, 2009 sure it does. and use isset() to check if your button has been pressed, ie. if (isset ($_POST['submit2'])) { Quote Link to comment Share on other sites More sharing options...
soak Posted April 21, 2009 Share Posted April 21, 2009 need another equals: if ($_POST['Submit2'] == 'Update Details') Quote Link to comment Share on other sites More sharing options...
gizmola Posted April 21, 2009 Share Posted April 21, 2009 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. Quote Link to comment Share on other sites More sharing options...
Mchl Posted April 21, 2009 Share Posted April 21, 2009 need another equals: if ($_POST['Submit2'] == 'Update Details') Lol... noone spotted it so far... Still this doesn't solve the problem I suppose... Quote Link to comment Share on other sites More sharing options...
wgoldschagg Posted April 21, 2009 Author Share Posted April 21, 2009 I will post ALL the code for you Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 21, 2009 Share Posted April 21, 2009 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? Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted April 21, 2009 Share Posted April 21, 2009 I will post ALL the code for youplease use code tags when you do. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 21, 2009 Share Posted April 21, 2009 One "=" for assignment, two "==" for comparison. Change: <?php if ($_POST['Submit2'] = 'Update Details') ?> to <?php if ($_POST['Submit2'] == 'Update Details') ?> Ken Quote Link to comment Share on other sites More sharing options...
wgoldschagg Posted April 21, 2009 Author Share Posted April 21, 2009 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. Quote Link to comment Share on other sites More sharing options...
soak Posted April 21, 2009 Share Posted April 21, 2009 $_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'"; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.