Jump to content

Update if Exists If Not Show Error


brandon66

Recommended Posts

Hey everyone having some troubles here with an update statement. I want to update a value(scanISN) if it exists and show information updated and if it doesn't exist show an error right now it show information updated even if it doesn't exist. How would I do that?

<?php

        if(!isset($_POST['addMe'])){
        //show form
        include_once('View/Scan.html.php');
        }else{
        //process the form
        //connect to the database
        include('Model/DBAdapter.php');


        //values to store in RMA Database
        $receiveDate = date('Y-m-d');   
        $rmaStatus = "Received";
        $scanISN = htmlspecialchars($_POST['scanISN'], ENT_QUOTES, 'UTF-8');

        //create sql query
        $sql = ("UPDATE RMA SET Received_Date = '$receiveDate', RMA_Status = '$rmaStatus' 
                WHERE Unit_Serial_Number = '$scanISN'");

        $result = $connection->query($sql);
        if(!$result){
        //ISN Does not exist
        $output = "ISN does not exist please contact support";
        }else{
        $output = "RMA information updated.";
        }

        include('View/Scan.html.php');
}

Link to comment
Share on other sites

<?php
		
		if(!isset($_POST['addMe'])){
		//show form
		include_once('View/Scan.html.php');
		}else{
		//process the form
		//connect to the database
		include('Model/DBAdapter.php');
		
		
		//values to store in RMA Database
		$receiveDate = date('Y-m-d');	
		$rmaStatus = "Received";
		$scanISN = htmlspecialchars($_POST['scanISN'], ENT_QUOTES, 'UTF-8');
		$rmaNumber = htmlspecialchars($_POST['rmaNumber'], ENT_QUOTES, 'UTF-8');

		
		//create sql query
		$sql = ("UPDATE RMA SET Received_Date = '$receiveDate', RMA_Status = '$rmaStatus' 
				WHERE Unit_Serial_Number = '$scanISN' AND RMA_Number = '$rmaNumber'");
		//run sql query
		$result = $connection->query($sql);
		
		//create sql query
		$sql2 = ("SELECT Received_Date, RMA_Status FROM RMA WHERE Unit_Serial_Number = '$scanISN' AND RMA_Number = '$rmaNumber'"); 
		
		//run sql query
		$result2 = $connection->query($sql2);
		
		//loop through data
		while ($row = $result2->fetch()){
		
		$check1 = $row['Received_Date'];
		$check2 = $row['RMA_Status'];
		
				
		if($check1 == $receiveDate || $check2 == $rmaStatus) {
    	 //success
    	$output = "RMA information updated.";
		} else {
   		  //error
   		 $output = "RMA does not exist please contact support";

		include('View/ScanOutput.html.php');
		}
}}

Looks like my copy didn't get all the code the first time

Link to comment
Share on other sites

from the manual:

"

 

notes-reject.gif Return Values

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.

Use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.

mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query."

 

 

 

http://php.net/manual/en/function.mysql-query.php

 

a syntactically correct UPDATE that doesn't affect any record doesn't return an error (FALSE), it only doesn't return a resultset.

 

[EDIT] You posted a new code while I was replying to your first code... but same answer apply... not idea why now you included a SELECT only to check if the record was updated or not... 

Edited by mikosiko
Link to comment
Share on other sites

This is what I have it seems to work but when I try to update the same twice it will say it doesn't exist. Am I doing this right?

<?php
		
		if(!isset($_POST['addMe'])){
		//show form
		include_once('View/Scan.html.php');
		}else{
		//process the form
		//connect to the database
		include('Model/DBAdapter.php');
		
		
		//values to store in RMA Database
		$receiveDate = date('Y-m-d');	
		$rmaStatus = "Received";
		$scanISN = strtoupper(htmlspecialchars($_POST['scanISN'], ENT_QUOTES, 'UTF-8'));
		$rmaNumber = htmlspecialchars($_POST['rmaNumber'], ENT_QUOTES, 'UTF-8');

		
		//create sql query
		$sql = $connection->prepare("UPDATE RMA SET Received_Date = '$receiveDate', RMA_Status = '$rmaStatus' 
				WHERE Unit_Serial_Number = '$scanISN' AND RMA_Number = '$rmaNumber'");
		//run sql query
		$sql->execute();
		$affected = $sql->rowCount();
				
		if($affected <= 0){
    	     	 $output = "RMA does not exist please contact support";
    	
		} else {
   		 $output = "RMA information updated.";
		
		}include('View/ScanOutput.html.php');
}
Edited by brandon66
Link to comment
Share on other sites

I think this works for that but the only other question is when one record is updated already it will say it doesn't exist. im doing something wrong here

<?php
		
		if(!isset($_POST['addMe'])){
		//show form
		include_once('View/Scan.html.php');
		}else{
		//process the form
		//connect to the database
		include('Model/DBAdapter.php');
		
		
		//values to store in RMA Database
		$receiveDate = date('Y-m-d');	
		$rmaStatus = "Received";
		$scanISN = strtoupper(htmlspecialchars($_POST['scanISN'], ENT_QUOTES, 'UTF-8'));
		$rmaNumber = htmlspecialchars($_POST['rmaNumber'], ENT_QUOTES, 'UTF-8');
		
		//create sql query to check if unit exists
		$sql1 = $connection->prepare("SELECT Received_Date,RMA_Status FROM rma WHERE Unit_Serial_Number = '$scanISN' AND RMA_Number = '$rmaNumber'");
		//run sql query
		$sql1->execute();
		$affected1 = $sql1->rowCount();
		
		//create sql query
		$sql = $connection->prepare("UPDATE RMA SET Received_Date = '$receiveDate', RMA_Status = '$rmaStatus' 
				WHERE Unit_Serial_Number = '$scanISN' AND RMA_Number = '$rmaNumber'");
		//run sql query
		$sql->execute();
		$affected = $sql->rowCount();		
		
		if($affected <= 0 || $affected1 <= 0){
    	 //error
    	 $output = "RMA does not exist please contact support";
    	
		} else {
   		  //success
   		 $output = "RMA information updated.";
		
		}include('View/ScanOutput.html.php');
}
Link to comment
Share on other sites

There's no need to do the update if the row does not exist.

//create sql query to check if unit exists
$sql1 = $connection->prepare("SELECT Received_Date,RMA_Status FROM rma WHERE Unit_Serial_Number = '$scanISN' AND RMA_Number = '$rmaNumber'");
//run sql query
$sql1->execute();
if ($sql1->rowCount() > 0){
	//create sql query
	$sql = $connection->prepare("UPDATE RMA SET Received_Date = '$receiveDate', RMA_Status = '$rmaStatus' 
			WHERE Unit_Serial_Number = '$scanISN' AND RMA_Number = '$rmaNumber'");
	//run sql query
	if (!$sql->execute()){
		$output = "RMA updated failed.";
	}
	else {
		$output = "RMA information updated.";
	}
}
else {
	$output = "RMA does not exist please contact support";
}
Link to comment
Share on other sites

I achieved what I wanted by changing

<?php
		
		if(!isset($_POST['addMe'])){
		//show form
		include_once('View/Scan.html.php');
		}else{
		//process the form
		//connect to the database
		include('Model/DBAdapter.php');
		
		
		//values to store in RMA Database
		$receiveDate = date('Y-m-d');	
		$rmaStatus = "Received";
		$scanISN = strtoupper(htmlspecialchars($_POST['scanISN'], ENT_QUOTES, 'UTF-8'));
		$rmaNumber = htmlspecialchars($_POST['rmaNumber'], ENT_QUOTES, 'UTF-8');
		
		//create sql query to check if unit exists
		$sql1 = $connection->prepare("SELECT Received_Date,RMA_Status FROM rma WHERE Unit_Serial_Number = '$scanISN' AND RMA_Number = '$rmaNumber'");
		//run sql query
		$sql1->execute();
		
		//if the query executes do the update statement
		if ($sql1->rowCount() > 0){
		//create sql query
		$sql = $connection->prepare("UPDATE RMA SET Received_Date = '$receiveDate', RMA_Status = '$rmaStatus' 
				WHERE Unit_Serial_Number = '$scanISN' AND RMA_Number = '$rmaNumber' AND RMA_Status != '$rmaStatus'");
		
		//if the update statement does not execute
		if ($sql->rowCount() <= 0){
			$output = "RMA updated failed.";
		}
		else {
			$output = "RMA information updated.";
		}
}
else {
	$output = "RMA does not exist please contact support";
	}
			
		include('View/ScanOutput.html.php');
		
}

 the last if and adding another AND to the last sql statement! Thanks everyone for your help I really appreciate it! I look forward to learning more from you :)

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.