billgod Posted April 30, 2010 Share Posted April 30, 2010 if (isset ($_POST[callcust])){ // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); //get todays date and set format $today = date(DATE_RFC822); //grab old comments from db $sql=" SELECT col_techcomments FROM tbl_ticket where id = $_SESSION[ticketid]"; $result=mysql_query($sql) or die ('Error: '.mysql_error ()); while($row = mysql_fetch_array($result)) { // combine old comments with time stamp $newtechcomments = $row[col_techcomments] . " " .$_POST[callcust] . " " . $today; //update DB mysql_query("UPDATE tbl_ticket set col_techcomments='$newtechcomments' where id = $_POST[ticketid]") or die ('Error: '.mysql_error ()); } } If i echo my update to the screen. The update sql looks correct. If I copy and paste it into mysql it runs and works fine. It's like php is just skipping th update. PS I am in no way shape or form a software developer. If my code looks like crap thats why. Quote Link to comment https://forums.phpfreaks.com/topic/200295-can-anyone-tell-me-why-this-wont-update/ Share on other sites More sharing options...
Ken2k7 Posted April 30, 2010 Share Posted April 30, 2010 A good practice is to store the SQL string into a variable before running it. $sql = "UPDATE tbl_ticket set col_techcomments='$newtechcomments' where id = $_POST['ticketid']";. That way, you can echo out $sql and verify that it's correct before you run it using mysql_query. Quote Link to comment https://forums.phpfreaks.com/topic/200295-can-anyone-tell-me-why-this-wont-update/#findComment-1051135 Share on other sites More sharing options...
Adam Posted April 30, 2010 Share Posted April 30, 2010 You need to add curly braces around an array when accessed within a string: "UPDATE tbl_ticket set col_techcomments='$newtechcomments' where id = {$_POST['ticketid']}" It's also better to add quotes around an associative key. Quote Link to comment https://forums.phpfreaks.com/topic/200295-can-anyone-tell-me-why-this-wont-update/#findComment-1051140 Share on other sites More sharing options...
billgod Posted April 30, 2010 Author Share Posted April 30, 2010 that just made it blow up.. ok figured out a little more. It's updating the wrong table row? if I echo my $_POST[ticketid] it is correct. If I echo the SQL statement it is correct. But if I let it run. It seems to updating rows starting with id 1 then 2 then 3 and so on? here is the SQL statement that gets generated UPDATE tbl_ticket set col_techcomments=' Left VM Fri, 30 Apr 10 14:22:06 -0400' where tbl_ticket.id = 670 If I paste this into mysql it works perfect? Quote Link to comment https://forums.phpfreaks.com/topic/200295-can-anyone-tell-me-why-this-wont-update/#findComment-1051195 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.