kat35601 Posted August 27, 2015 Share Posted August 27, 2015 <html> <head> <title>Status Screen</title> </head> <body> <p> Open RedTags </p> <?php $serial=$_POST["serial"]; $connect =odbc_connect("removed"); if(!$connect) { exit("Connection Failed: " . $connect); } $query= " Select serial from WIP_Master where seiral ='$serial' "; $sql=" Update WIP_Master set Status= 5 where seiral ='$serial' and Status='2'"; $result =odbc_exec($connect,$query) if (!mssql_num_rows($query)){ $result =odbc_exec($connect,$sql);} else {echo 'Serial Notfound'} $result =odbc_exec($connect,$sql); if(!$result){ exit("Error in SQL"); } <h1> Updated</h1> odbc_close($connect); sleep(5); ?> </body> </html> Hello I need to check to see if a select statement returns a row if so I then want to run and up date statement. This is as far as I have gotten would you please point me in the right direction. I just can't figure it out. Quote Link to comment https://forums.phpfreaks.com/topic/297953-mssql-if-select-returns-row-then-run-update-sql-statement/ Share on other sites More sharing options...
Barand Posted August 27, 2015 Share Posted August 27, 2015 If you are using ODBC, why suddenly attempt to use mssql_num_rows() instead of odbc_num_rows()? Quote Link to comment https://forums.phpfreaks.com/topic/297953-mssql-if-select-returns-row-then-run-update-sql-statement/#findComment-1519736 Share on other sites More sharing options...
kat35601 Posted August 27, 2015 Author Share Posted August 27, 2015 <html> <head> <title>Status Screen</title> </head> <body> <p> Open RedTags </p> <?php $serial=$_POST["serial"]; $connect =odbc_connect("removed"); if(!$connect) { exit("Connection Failed: " . $connect); } $query= " Select serial from WIP_Master where seiral ='$serial' "; $sql=" Update WIP_Master set Status= 5 where seiral ='$serial' and Status='2'"; $result =odbc_exec($connect,$query) if (odbc_num_rows($query)){ $result =odbc_exec($connect,$sql);} else {echo 'Serial Notfound'} $result =odbc_exec($connect,$sql); if(!$result){ exit("Error in SQL"); } <h1> Updated</h1> odbc_close($connect); sleep(5); ?> </body> </html> Because I have no clue what I am doing. But I am trying. Quote Link to comment https://forums.phpfreaks.com/topic/297953-mssql-if-select-returns-row-then-run-update-sql-statement/#findComment-1519737 Share on other sites More sharing options...
Ch0cu3r Posted August 27, 2015 Share Posted August 27, 2015 On line 18 you are missing the semi-colon ; at the end of the line $result =odbc_exec($connect,$query); You are running the update query twice on lines 20 and 24. You only need to run it once, so do not execute the query again on line 24. On line 29 you have HTML code which PHP tags. You can not place HTML within PHP tags if you want to output HTML you need to use either echo or print When outputting error messages "Error in SQL" is meaningless. To know why the query failed use odbc_errormsg. This will return the error message from mssql. Also do not use die or exit instead use trigger_error Quote Link to comment https://forums.phpfreaks.com/topic/297953-mssql-if-select-returns-row-then-run-update-sql-statement/#findComment-1519741 Share on other sites More sharing options...
kat35601 Posted August 27, 2015 Author Share Posted August 27, 2015 <html> <head> <title>Status Screen</title> </head> <body> <p> Open RedTags </p> <?php $serial=$_POST["serial"]; $connect =odbc_connect("removed"); if(!$connect) { exit("Connection Failed: " . $connect); } $query= " Select serial from WIP_Master where seiral ='$serial' "; $sql=" Update WIP_Master set Status= 5 where seiral ='$serial' and Status='2'"; $result =odbc_exec($connect,$query); if (odbc_num_rows($query)){ $result =odbc_exec($connect,$sql);} else {echo 'Serial Notfound'}; if(!$result){ trigger_error("Error in SQL",odbc_errormsg()); } {echo 'Updated'}; odbc_close($connect); sleep(5); ?> </body> </html> Like this??? Quote Link to comment https://forums.phpfreaks.com/topic/297953-mssql-if-select-returns-row-then-run-update-sql-statement/#findComment-1519744 Share on other sites More sharing options...
Ch0cu3r Posted August 27, 2015 Share Posted August 27, 2015 You need to use a period not a comma between "Error in SQL" and odb_errormsg() when calling trigger_error trigger_error("Error in SQL: ".odbc_errormsg()); Also you do not need the curly braces around echo 'Updated' echo "Updated"; Quote Link to comment https://forums.phpfreaks.com/topic/297953-mssql-if-select-returns-row-then-run-update-sql-statement/#findComment-1519746 Share on other sites More sharing options...
kat35601 Posted August 27, 2015 Author Share Posted August 27, 2015 <html> <head> <title>Status Screen</title> </head> <body> <p> Open RedTags </p> <?php $serial=$_POST["serial"]; $connect =odbc_connect("removed"); if(!$connect) { exit("Connection Failed: " . $connect); } $query= " Select serial from WIP_Master where seiral ='$serial' "; $sql=" Update WIP_Master set Status= 5 where seiral ='$serial' and Status='2'"; $result =odbc_exec($connect,$query); if (odbc_num_rows($query)){ $result =odbc_exec($connect,$sql);} else {echo 'Serial Notfound'}; if(!$result){ trigger_error("Error in SQL".odbc_errormsg()); } {echo 'Updated'} odbc_close($connect); sleep(5); $url = 'http://theintranet/kk_update_redtag_lk.php'; header('Location: ' . $url); ?> </body> </html> ok so this might be runnable you think? I added a redirect of sorts to the end. Quote Link to comment https://forums.phpfreaks.com/topic/297953-mssql-if-select-returns-row-then-run-update-sql-statement/#findComment-1519753 Share on other sites More sharing options...
Psycho Posted August 27, 2015 Share Posted August 27, 2015 Why are you going to output content to the page AND THEN also have a redirect at the end (which will fail anyway since you've already output content to the page).? But, you don't need to do TWO queries. Just run the ONE update query since it already has the same condition as the first query. So the record would only be updated if meets the criteria anyway. Quote Link to comment https://forums.phpfreaks.com/topic/297953-mssql-if-select-returns-row-then-run-update-sql-statement/#findComment-1519757 Share on other sites More sharing options...
kat35601 Posted August 27, 2015 Author Share Posted August 27, 2015 on this page they will enter the serial number of a repaired item and it will change the status to repaired and let it continue into our system it should then go back to the page to enter the serial number. Quote Link to comment https://forums.phpfreaks.com/topic/297953-mssql-if-select-returns-row-then-run-update-sql-statement/#findComment-1519758 Share on other sites More sharing options...
Psycho Posted August 27, 2015 Share Posted August 27, 2015 on this page they will enter the serial number of a repaired item and it will change the status to repaired and let it continue into our system it should then go back to the page to enter the serial number. A page cannot output content to the page (even the tags that start the page: <html>, <head>, etc.) and then use a header command (such as a redirect). Quote Link to comment https://forums.phpfreaks.com/topic/297953-mssql-if-select-returns-row-then-run-update-sql-statement/#findComment-1519784 Share on other sites More sharing options...
kat35601 Posted August 27, 2015 Author Share Posted August 27, 2015 So I can't go from my update page to the entry page...I don't want to pass anything I just want to start over. Quote Link to comment https://forums.phpfreaks.com/topic/297953-mssql-if-select-returns-row-then-run-update-sql-statement/#findComment-1519787 Share on other sites More sharing options...
Psycho Posted August 27, 2015 Share Posted August 27, 2015 Then don't output anything. Quote Link to comment https://forums.phpfreaks.com/topic/297953-mssql-if-select-returns-row-then-run-update-sql-statement/#findComment-1519790 Share on other sites More sharing options...
kat35601 Posted August 31, 2015 Author Share Posted August 31, 2015 <html> <head> <title>Status Screen</title> </head> <body> <p> Open RedTags </p> <?php $serial=$_POST["serial"]; $connect =odbc_connect("Removed"); if(!$connect) { exit("Connection Failed: " . $connect); } $query= " Select serial from WIP_Master where serial ='$serial' "; $sql=" Update WIP_Master set Status= '2' where serial ='$serial' "; $result =odbc_exec($connect,$query); if (odbc_num_rows($query));{ $result =odbc_exec($connect,$sql); } if(!$result){ trigger_error("Error in SQL".odbc_errormsg()); } odbc_close($connect); sleep(5); $url = 'http://theintranet/kk_update_redtag_lk.php'; header('Location: ' . $url); ?> </body> </html> So this is working but I do want to report "serial not found" if the select statement does not find it or return any rows and return to the url mentioned, is there any other error catching or correcting that I need to do??? Quote Link to comment https://forums.phpfreaks.com/topic/297953-mssql-if-select-returns-row-then-run-update-sql-statement/#findComment-1519987 Share on other sites More sharing options...
kat35601 Posted August 31, 2015 Author Share Posted August 31, 2015 or should I use try to use odbc_fetch_row????? What would that look like??? Quote Link to comment https://forums.phpfreaks.com/topic/297953-mssql-if-select-returns-row-then-run-update-sql-statement/#findComment-1519989 Share on other sites More sharing options...
ginerjm Posted August 31, 2015 Share Posted August 31, 2015 Have you tried reading a manual on how to use the things you are trying to use? Just browsed thru all the posts here and it seems that you are learning PHP line by line as each forum member gives you an answer. Probably not the best way to learn technology. Quote Link to comment https://forums.phpfreaks.com/topic/297953-mssql-if-select-returns-row-then-run-update-sql-statement/#findComment-1519990 Share on other sites More sharing options...
Barand Posted August 31, 2015 Share Posted August 31, 2015 Are you sure it's working? if (odbc_num_rows($query));{ $result =odbc_exec($connect,$sql); } The "if" condition and action finishes at the ";" so that last statement is always executed. if (odbc_num_rows($query));{ ^ | remove the semi-colon Quote Link to comment https://forums.phpfreaks.com/topic/297953-mssql-if-select-returns-row-then-run-update-sql-statement/#findComment-1519991 Share on other sites More sharing options...
kat35601 Posted August 31, 2015 Author Share Posted August 31, 2015 It is updating my sql server as expected Quote Link to comment https://forums.phpfreaks.com/topic/297953-mssql-if-select-returns-row-then-run-update-sql-statement/#findComment-1519995 Share on other sites More sharing options...
Barand Posted August 31, 2015 Share Posted August 31, 2015 It will update records that exist as expected. It also attempts to update those that do not exist. It is "working" but more by luck than good coding. Quote Link to comment https://forums.phpfreaks.com/topic/297953-mssql-if-select-returns-row-then-run-update-sql-statement/#findComment-1520000 Share on other sites More sharing options...
kat35601 Posted August 31, 2015 Author Share Posted August 31, 2015 I don't disagree that's why I came here is to get guidance. I am sorry if by asking question I have done something wrong. Quote Link to comment https://forums.phpfreaks.com/topic/297953-mssql-if-select-returns-row-then-run-update-sql-statement/#findComment-1520013 Share on other sites More sharing options...
QuickOldCar Posted August 31, 2015 Share Posted August 31, 2015 I don't disagree that's why I came here is to get guidance. I am sorry if by asking question I have done something wrong. You haven't done anything wrong. If you want to learn or reference anything php could browse php.net obdc functions Quote Link to comment https://forums.phpfreaks.com/topic/297953-mssql-if-select-returns-row-then-run-update-sql-statement/#findComment-1520016 Share on other sites More sharing options...
Barand Posted August 31, 2015 Share Posted August 31, 2015 I merely pointed out an error in your code, one which you seem to be denying exists It is updating my sql server as expected Quote Link to comment https://forums.phpfreaks.com/topic/297953-mssql-if-select-returns-row-then-run-update-sql-statement/#findComment-1520017 Share on other sites More sharing options...
Psycho Posted August 31, 2015 Share Posted August 31, 2015 That page would also 'fail' because you are outputting content to the page and THEN attempting to use a header redirect - which I already stated twice is not valid. Quote Link to comment https://forums.phpfreaks.com/topic/297953-mssql-if-select-returns-row-then-run-update-sql-statement/#findComment-1520033 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.