Jump to content

MSSQL If select returns row then run update sql statement


kat35601

Recommended Posts

<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.

 

 

Link to comment
Share on other sites

<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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<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???

Link to comment
Share on other sites

<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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

<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???

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.