studgate Posted January 27, 2007 Share Posted January 27, 2007 I am trying to update my database using phpI have the update.php that is supposed to checkfor changes and insert the changes into the databaseand into the sorting categories but everytimethere is change, it just add a new entry instead ofupdating the previous one, like if the city is Parisand I change it to Marseille, there is a new entryfor Marseille in the city sorting and the entry forParis is empty. ???Here is the code, any help is welcome.Thank you guys! :)<? $dbconn = @mysql_connect("localhost","user","passwordhere"); @mysql_select_db("databasehere"); $strsql = "update tblreports set `event_date`='$event_date', `event_time`='$event_time', `kb`='$kb', `event_duration`='$event_duration', `city`='$city', `state`='$state', `zip`='$zip', `foreign_country`='$foreign_country', `county`='$county', `nr_witness`='$nr_witness', `activity`='$activity', `location_type`='$location_type', `location_age`='$location_age', `description`='$description', `summary`='$summary', `characteristics`='$characteristics', `uname`='$uname', `uemail`='$uemail', `uaddress`='$uaddress', `ucity`='$ucity', `ustate`='$ustate', `uzip`='$uzip', `uphone`='$uphone', `uoccupation`='$uoccupation', `religy`='$religy', `education`='$education', `uage`='$uage', `ugender`='$ugender', `medication`='$medication', `medication_recent`='$medication_recent', `illness`='$illness', `drug`='$drug', `drug_recent`='$drug_recent', `alcohol`='$alcohol', `alcohol_recent`='$alcohol_recent', `anyuseof`='$anyuseof', `charlast`='$charlast' where id='$editid'"; @mysql_query($strsql); list($nr_rows) = @mysql_fetch_row(@mysql_query("SELECT COUNT(*) FROM tblreportsbyedate WHERE edate='$event_date';")); if (empty($nr_rows)) $strsql = "INSERT INTO tblreportsbyedate (`edate`,`nr`) VALUES('$event_date','1')"; else $strsql = "UPDATE tblreportsbyedate SET nr=nr WHERE edate='$event_date';"; @mysql_query($strsql); list($nr_rows) = @mysql_fetch_row(@mysql_query("SELECT COUNT(*) FROM tblreportsbystate WHERE state='$state';")); if (empty($nr_rows)) $strsql = "INSERT INTO tblreportsbystate (`state`,`nr`) VALUES('$state','1')"; else $strsql = "UPDATE tblreportsbystate SET nr=nr WHERE state='$state';"; @mysql_query($strsql); list($nr_rows) = @mysql_fetch_row(@mysql_query("SELECT COUNT(*) FROM tblreportsbyactivity WHERE activity='$activity';")); if (empty($nr_rows)) $strsql = "INSERT INTO tblreportsbyactivity (`activity`,`nr`) VALUES('$activity','1')"; else $strsql = "UPDATE tblreportsbyactivity SET nr=nr WHERE activity='$activity';"; @mysql_query($strsql); list($nr_rows) = @mysql_fetch_row(@mysql_query("SELECT COUNT(*) FROM tblreportsbycity WHERE city='$city';")); if (empty($nr_rows)) $strsql = "INSERT INTO tblreportsbycity (`city`,`nr`) VALUES('$city','1')"; else $strsql = "UPDATE tblreportsbycity SET nr=nr WHERE city='$city';"; @mysql_query($strsql); @mysql_close($dbconn);?> Quote Link to comment https://forums.phpfreaks.com/topic/35970-looking-for-some-help/ Share on other sites More sharing options...
tippy_102 Posted January 28, 2007 Share Posted January 28, 2007 I don't know if it will correct your problem, but I don't think you need all those quotes. Try this idea:[code] $strsql = "update tblreports set event_date='$event_date', event_time='$event_time',[/code]I took the quotes off the [i]event_date[/i], but not the string. Quote Link to comment https://forums.phpfreaks.com/topic/35970-looking-for-some-help/#findComment-170936 Share on other sites More sharing options...
HuggieBear Posted January 28, 2007 Share Posted January 28, 2007 OK, let me give you a few pieces of advice before I tell you your problem...1. Use [b][nobbc][code]...[/code][/nobbc][/b] tags around your code when posting in the forum as it looks a lot nicer and easy to read.2. If you include <?php ?> tags in the code section you get syntax highlighting which is also a lot easier to read and also aids with spotting syntactically incorrect code.[code]<?php// Here's an example of what I mean by [code] tags and syntax highlighting$var = "variable";if (isset($var)){ echo "You've set \$var to: " . $var;}?>[/code]3. Don't nest your functions, as it's more difficult to read and report errors. so avoid strings like this:[code]$num = mysql_num_rows(mysql_query("SELECT column FROM table_name"));[/code]in favour of:[code]$sql = "SELECT column FROM table_name";$result = mysql_query($sql) or die ("Couldn't execute $sql: " . mysql_error());$num = mysql_num_rows($result);[/code]4. Use UPPER CASE for MySQL command syntax as it makes things a lot easier to read. So something like this:[code]SELECT column_name FROM table_name WHERE column_name = 'condition'[/code]This brings me onto solving your problem. You don't have a WHERE clause in your MySQL statement. Now this would have been a lot easier for me to spot if you'd capitalised your code :)Basically you need to be saying something along the lines of...[color=green][color=maroon]UPDATE[/color] tblreports [color=maroon]SET[/color] date_event = '$date_event' .... [b][color=maroon]WHERE[/color][/b] city = 'paris'[/color]If you don't define a WHERE clause, the code will try to update every row in the table.RegardsHuggie[/code] Quote Link to comment https://forums.phpfreaks.com/topic/35970-looking-for-some-help/#findComment-171125 Share on other sites More sharing options...
studgate Posted January 28, 2007 Author Share Posted January 28, 2007 Thanks HuggieBear for the help and suggestions& tippy_102, I did try your suggestions but it didn't make any differenceWhat I wanted the query to do is to update the newcity or event_date or the new state or the new activity and then delete the old one and replace the new one.HuggieBear, I can't find where the WHERE clause was not setI feel that there is a problem in this part of the code (it should addHere is the update_report.php[code] <?php<? $dbconn = @mysql_connect("localhost","user","passwordhere"); @mysql_select_db("databasehere"); $strsql = "UPDATE tblreports SET `event_date`='$event_date', `event_time`='$event_time', `kb`='$kb', `event_duration`='$event_duration', `city`='$city', `state`='$state', `zip`='$zip', `foreign_country`='$foreign_country', `county`='$county', `nr_witness`='$nr_witness', `activity`='$activity', `location_type`='$location_type', `location_age`='$location_age', `description`='$description', `summary`='$summary', `characteristics`='$characteristics', `uname`='$uname', `uemail`='$uemail', `uaddress`='$uaddress', `ucity`='$ucity', `ustate`='$ustate', `uzip`='$uzip', `uphone`='$uphone', `uoccupation`='$uoccupation', `religy`='$religy', `education`='$education', `uage`='$uage', `ugender`='$ugender', `medication`='$medication', `medication_recent`='$medication_recent', `illness`='$illness', `drug`='$drug', `drug_recent`='$drug_recent', `alcohol`='$alcohol', `alcohol_recent`='$alcohol_recent', `anyuseof`='$anyuseof', `charlast`='$charlast' WHERE id='$editid'"; @mysql_query($strsql); list($nr_rows) = @mysql_fetch_row(@mysql_query("SELECT COUNT(*) FROM tblreportsbyedate WHERE edate='$event_date';")); if (empty($nr_rows)) $strsql = "INSERT INTO tblreportsbyedate (`edate`,`nr`) VALUES('$event_date','1')"; else $strsql = "UPDATE tblreportsbyedate SET nr=nr WHERE edate='$event_date';"; @mysql_query($strsql); list($nr_rows) = @mysql_fetch_row(@mysql_query("SELECT COUNT(*) FROM tblreportsbystate WHERE state='$state';")); if (empty($nr_rows)) $strsql = "INSERT INTO tblreportsbystate (`state`,`nr`) VALUES('$state','1')"; else $strsql = "UPDATE tblreportsbystate SET nr=nr WHERE state='$state';"; @mysql_query($strsql); list($nr_rows) = @mysql_fetch_row(@mysql_query("SELECT COUNT(*) FROM tblreportsbyactivity WHERE activity='$activity';")); if (empty($nr_rows)) $strsql = "INSERT INTO tblreportsbyactivity (`activity`,`nr`) VALUES('$activity','1')"; else $strsql = "UPDATE tblreportsbyactivity SET nr=nr WHERE activity='$activity';"; @mysql_query($strsql); list($nr_rows) = @mysql_fetch_row(@mysql_query("SELECT COUNT(*) FROM tblreportsbycity WHERE city='$city';")); if (empty($nr_rows)) $strsql = "INSERT INTO tblreportsbycity (`city`,`nr`) VALUES('$city','1')"; else $strsql = "UPDATE tblreportsbycity SET nr=nr WHERE city='$city';"; @mysql_query($strsql); @mysql_close($dbconn);?> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/35970-looking-for-some-help/#findComment-171217 Share on other sites More sharing options...
Destruction Posted January 28, 2007 Share Posted January 28, 2007 list($nr_rows) = @mysql_fetch_rowFirstly, why are you using list instead of just $nr_rows = @mysql_fetch_row etc.Secondly, that will return an array, empty() is for checking if a string is empty. An empty array, if I recall, will still return a string value of Array ().Thirdly, SELECT COUNT(*) doesn't return a row, it returns a value/result. You would be best using mysql_fetch_result($query, 0) perhaps.Lastly, You are suppressing the error messages using @ but are not providing an alternative. Without an alternative, you are not going to see what the actual error is. That is a part of why nesting queries is a very bad idea, as well as the performance issues of using mysql_fetch_row regardless of whether mysql_query actually worked correctly etc.Dest Quote Link to comment https://forums.phpfreaks.com/topic/35970-looking-for-some-help/#findComment-171226 Share on other sites More sharing options...
HuggieBear Posted January 28, 2007 Share Posted January 28, 2007 [quote author=studgate link=topic=124316.msg515473#msg515473 date=1170003438]HuggieBear, I can't find where the WHERE clause was not set[/quote]Sorry, I didn't see it in the first post, even more reason to capitalise the syntax :)There doesn't seem to be any syntax errors so try this and let me know if you get an error messages...[code] <?php$dbconn = mysql_connect("localhost","user","passwordhere");mysql_select_db("databasehere"); $strsql = "UPDATE tblreports SET `event_date`='$event_date', `event_time`='$event_time', `kb`='$kb', `event_duration`='$event_duration', `city`='$city', `state`='$state', `zip`='$zip', `foreign_country`='$foreign_country', `county`='$county', `nr_witness`='$nr_witness', `activity`='$activity', `location_type`='$location_type', `location_age`='$location_age', `description`='$description', `summary`='$summary', `characteristics`='$characteristics', `uname`='$uname', `uemail`='$uemail', `uaddress`='$uaddress', `ucity`='$ucity', `ustate`='$ustate', `uzip`='$uzip', `uphone`='$uphone', `uoccupation`='$uoccupation', `religy`='$religy', `education`='$education', `uage`='$uage', `ugender`='$ugender', `medication`='$medication', `medication_recent`='$medication_recent', `illness`='$illness', `drug`='$drug', `drug_recent`='$drug_recent', `alcohol`='$alcohol', `alcohol_recent`='$alcohol_recent', `anyuseof`='$anyuseof', `charlast`='$charlast' WHERE id='$editid'";mysql_query($strsql) or die ("Unable to execute query:<br>$strsql<br>" . mysql_error()); list($nr_rows) = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM tblreportsbyedate WHERE edate='$event_date';"));if (empty($nr_rows)) $strsql = "INSERT INTO tblreportsbyedate (`edate`,`nr`) VALUES('$event_date','1')";else $strsql = "UPDATE tblreportsbyedate SET nr=nr WHERE edate='$event_date';";mysql_query($strsql) or die ("Unable to execute query:<br>$strsql<br>" . mysql_error()); list($nr_rows) = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM tblreportsbystate WHERE state='$state';"));if (empty($nr_rows)) $strsql = "INSERT INTO tblreportsbystate (`state`,`nr`) VALUES('$state','1')";else $strsql = "UPDATE tblreportsbystate SET nr=nr WHERE state='$state';";mysql_query($strsql) or die ("Unable to execute query:<br>$strsql<br>" . mysql_error()); list($nr_rows) = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM tblreportsbyactivity WHERE activity='$activity';"));if (empty($nr_rows)) $strsql = "INSERT INTO tblreportsbyactivity (`activity`,`nr`) VALUES('$activity','1')";else $strsql = "UPDATE tblreportsbyactivity SET nr=nr WHERE activity='$activity';";mysql_query($strsql) or die ("Unable to execute query:<br>$strsql<br>" . mysql_error()); list($nr_rows) = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM tblreportsbycity WHERE city='$city';"));if (empty($nr_rows)) $strsql = "INSERT INTO tblreportsbycity (`city`,`nr`) VALUES('$city','1')";else $strsql = "UPDATE tblreportsbycity SET nr=nr WHERE city='$city';";mysql_query($strsql) or die ("Unable to execute query:<br>$strsql<br>" . mysql_error()); mysql_close($dbconn);?> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/35970-looking-for-some-help/#findComment-171228 Share on other sites More sharing options...
studgate Posted January 28, 2007 Author Share Posted January 28, 2007 HuggieBear, i Didn't get an error message, and thanks for the helpDestruction , maybe a little example will help me a little morebasically what I wanted this code to do is to update my databaseand to update the four tables (tblreportsbyedate, tblreportsbystate,tblreportsbyactivity, & tblreportsbycity) for better sorting. It doesn'tseem to be doing that.it does update my database but it is adding to the four tables withoutremoving the old ones. Quote Link to comment https://forums.phpfreaks.com/topic/35970-looking-for-some-help/#findComment-171255 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.