Jump to content

Recommended Posts

Hi all,

 

Here's my coding issue: I have a transaction script that is supposed to insert into a sold_items and then update the products table--this works perfectly. However, I want to introduce a third query that will execute the transaction ONLY if there is an existing employee number. The problem is I keep getting the else statement: "Employee ID is invalid". For some reason, the query doesn't allow the other blocks of code to be excuted. Any insight ya'll could provide would be greatly appreciated.

<?php
		
require ('connect.php');

//connect
$connection =mysqli_connect($db_host, $db_user, $db_pass);
if(!$connection){
	die ("Could not connect to database: <br />".mysql_error());
}

//select database
$db_select = mysqli_select_db($connection, $db_database);
if (!$db_select){
	die ("Could not select to database: <br />". mysql_error());
}

if ( isset($_POST['sellbtn']) ) {
// Get values from form 
$emp_id =mysql_real_escape_string(htmlentities($_POST['emp_id']));
$item_id =mysql_real_escape_string(htmlentities($_POST['item_id']));
$qnty =mysql_real_escape_string(htmlentities($_POST['qnty']));


//Cancel auto commit option in the database
mysqli_autocommit($connection, FALSE);


//check to see if there is an existing employee
$query = mysql_query("SELECT * FROM employees WHERE emp_id='$emp_id' AND comp_id='$comp_id'");
if (mysql_query($query) == TRUE)
{

//query to update products table--this part works when I take out the first query

//query to add sold_table--this part works when I take out the first query


$success = true;

foreach( $results as $result) {
	if(!$result) {
		$success = false;
		
	}
}
if(!$success) {
	echo "Error. Please make sure all fields have been entered correctly.";
	mysqli_rollback($connection);
} else {
	echo "Sold!";
	mysqli_commit($connection);
}

} else echo "<center><p><font color ='red'>Employee ID is invalid.</font></p></center>";

mysqli_close($connection);
}

?>
Link to comment
https://forums.phpfreaks.com/topic/280779-transactions-works-but-query-doesnt/
Share on other sites

from the manual 

"notes-reject.gif Return Values

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queriesmysqli_query() will return TRUE."

 

http://php.net/manual/en/mysqli.query.php

You're mixing mysqli_* and mysql_* functions, you can't do that. Since your initial connection is mysqli, you will want to change your mysql_real_escape_string and mysql_query functions to the mysqli_ equivalents.

Edited by kicken
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.