Vernersson Posted October 31, 2007 Share Posted October 31, 2007 Hi, i usually code ASP but now I want to learn PHP. I'm currently working on a project which communicates with a MySQL database but I've got stuck and could really need some help. I looked through the PHP reference but couldn't find any good way of updating an already existing entry. The way they explained it i needed to put all my data into a SQL-query. Is there any way that something like this could work, and how would it be like? mysql_select_db("database", $con) or die("Could not connect to the DB<BR>"); $seledit = "SELECT * FROM Table1 WHERE ID = ".$ID ; $result = mysql_query($seledit, $con) or die("Query failed:<BR> " . mysql_error());while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { row['Entry1'] = $Variable1 row['Entry2'] = $Variable2 row['Entry3'] = $Variable3 row['Entry4'] = $Variable4 row['Entry5'] = $Variable5 row['Entry6'] = $Variable6 row['Entry7'] = $Variable7 row['Entry8'] = $Variable8 Thanks! Link to comment Share on other sites More sharing options...
sajan Posted October 31, 2007 Share Posted October 31, 2007 if u want to update use. $seledit = "update Table1 set fieldname=$fieldname WHERE ID = ".$ID;//fieldname is the name of field u want to update and $filedname is the value.$result = mysql_query($seledit, $con) or die("Query failed:<BR> " . mysql_error()); Link to comment Share on other sites More sharing options...
Vernersson Posted October 31, 2007 Author Share Posted October 31, 2007 if u want to update use.$seledit = "UPDATE Offerter SET underkonsult = ".$underkonsult . " AND offertNr = " . $offertNr . " AND offeradFran = " . $offeradFran . " AND offeradTill = " . $offeradTill . " AND bestalldFran = " . $bestalldFran . " AND bestalldTill = " . $bestalldTill . " AND lon = " . $lon . " WHERE OffertID = ".$offertID;$result = mysql_query($seledit, $con) or die("Query failed:<BR> " . mysql_error()); Link to comment Share on other sites More sharing options...
sajan Posted October 31, 2007 Share Posted October 31, 2007 don't write this way $seledit = "UPDATE Offerter SET underkonsult = ".$underkonsult . " AND offertNr = " . $offertNr . " AND offeradFran = " . $offeradFran . " AND offeradTill = " . $offeradTill . " AND bestalldFran = " . $bestalldFran . " AND bestalldTill = " . $bestalldTill . " AND lon = " . $lon . " WHERE OffertID = ".$offertID; use $seledit = "update Table1 set fieldname=$fieldname, fieldname1=$fieldname1, fieldname2=$fieldname2 WHERE ID = '$ID'"; Link to comment Share on other sites More sharing options...
Synook Posted October 31, 2007 Share Posted October 31, 2007 I followed your example but I can't seem to get it to work.Don't use the AND operator in UPDATE queries - they are for logical tests. Use commas instead. So (clean up as well)UPDATE Offerter SET underkonsult = $underkonsult, offertNr = $offertNr, offeradFran = $offeradFran, offeradTill = $offeradTill, bestalldFran = $bestalldFran, bestalldTill = $bestalldTill, lon = $lon WHERE OffertID = $offertID Link to comment Share on other sites More sharing options...
Vernersson Posted October 31, 2007 Author Share Posted October 31, 2007 Thanks for your help & feedback. Just one more question, in ASP you can use the "if not"For example If not request.querystring("ID") = "" thenresponse.redirect "wherever.asp"elseresponse.write "hello!" Is it possible in PHP to write if ($_GET["reference"] != "") { $reference = $_GET["reference"];}else { $reference = "Konsult";} Thanks! Link to comment Share on other sites More sharing options...
justsomeguy Posted October 31, 2007 Share Posted October 31, 2007 You can do that. There's also a shorthand way to write an if/else assignment statement.$referece = $_GET["reference"] != "" ? $_GET["reference"] : "Konsult";http://www.php.net/manual/en/language.oper...parison.ternary Link to comment Share on other sites More sharing options...
Vernersson Posted December 4, 2007 Author Share Posted December 4, 2007 Thanks for your help! The SQL-syntax you gave me doesn't return any errors. The only problem left is that the database doesnt get updated, i added mysql_select_db to be sure that it uses the right db. mysql_update_db doesnt return any errors aswell.. What can be wrong? Is there any cache option that is set to on by default?Thanks! Link to comment Share on other sites More sharing options...
justsomeguy Posted December 4, 2007 Share Posted December 4, 2007 No, there shouldn't be any caching with regard to the database. Print out the query you're building and run it directly on the database, make sure it's valid. If there aren't any errors but it's not updating what you expect then chances are the WHERE clause doesn't refer to any existing rows. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.