Jump to content

Use returned value for second insert query


kurt.santo

Recommended Posts

I run an insert query and need to use the auto-incremented value for company_id straight away for an insert into different table. I created the script as:

$query = "INSERT INTO users (email, pass, first_name, last_name, company, telephone, address1, address2, address3, town, county, postcode, country, source, other, active, registration_date) VALUES ('$email', SHA('$pass'), '$first_name', '$last_name', '$company', '$telephone', '$address1', '$address2', '$address3', '$town', '$county', '$postcode', '$country', '$source', '$other', '$a', NOW() )";					$result = mysqli_query ($dbc, $query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());			if (mysqli_affected_rows($dbc) == 1) { // If it ran ok			$uid = @mysqli_insert_id($dbc);			if ($uid > 0) {			$query = "INSERT INTO association (county_id, company_id, active) VALUES ";			foreach ($_POST['counties'] as $v) {			$query .= "($v, $uid, 'N'), ";			}			$query = substr ($query, 0, -2);			$result = @mysqli_query ($dbc, $query);			if (mysqli_affected_rows($dbc) == count($_POST['counties'])) {			echo '<p>Thank you</p>';			$_POST = array();			} else {			echo '<p>Did not work!</p>';			$query = "DELETE FROM association WHERE company_id=$uid";			@mysqli_query ($dbc, $query);			}					}	

but the insertion into the association table (second) does not work (it does not enter any values). I was a bit unsure about the nesting of both queries. It should run the first insert query and if this one worked well (it inserted one record) it runs the second one. Not sure were I went wrong. Also had the varible $uid outside of conditional and made it global. Did also not work.Kurt

Link to comment
Share on other sites

i think (not sure), the error is at:foreach($_POST['counties'] as $v){ $query .= "($v, $uid, 'N'), ";}(if $v is a string it should be between ' ')for each counties is adds (...),which means the query ends at a comma, and i think sql will think there should come more coz of the comma, and give an errorif this is indeed the problem, try using implode() instead: $query = "INSERT INTO association (county_id, company_id, active) VALUES ("; $query .= implode(",$uid,'n'),(",$_POST['counties']); $query = ")";or use substr or trim to remove the space and comma of the endalso try putting or die(mysqli_error($dbc)) after the other mysqli_query()'s, and echo the query's so u can see what goes wrong

Link to comment
Share on other sites

I find the error message now a bit confusing. It complains "mysqli_num_rows() expects exactly 1 parameter, 2 given" concerning line "if (mysqli_num_rows($dbc, $result) == 0) " (which was fine before I created the second query). Also, counties refers to a the name of related tickboxes, for which I test via:

		// check that a category has been selected	if (isset($_POST['counties']))  {		$counties = TRUE;		} else {			$counties = FALSE;			echo "<p>select one county</p>";			}

Looking into the $_POST array it says for counties:[counties] => onIn addition, I thought I was trimming off the comma via:$query = substr ($query, 0, -2);Do you see what I am doing wrong here?Kurt

Link to comment
Share on other sites

oh im sorry, missed the substrcan you try echoing each statement and see if they show what u expect them to show?can you remove the @'s and use mysqli_errorand then say what line exactly give the error? im kinda sleepy, so guess thats y im confused :)

Link to comment
Share on other sites

oh im sorry, missed the substrcan you try echoing each statement and see if they show what u expect them to show?can you remove the @'s and use mysqli_errorand then say what line exactly give the error? im kinda sleepy, so guess thats y im confused :)
I find the error message now a bit confusing. It complains "mysqli_num_rows() expects exactly 1 parameter, 2 given" concerning line "if (mysqli_num_rows($dbc, $result) == 0) " (which was fine before I created the second query). Then it also complains:An error occurred in script 'file' on line 272: Query: INSERT INTO users (email, pass, first_name, last_name, company, telephone, address1, address2, address3, town, county, postcode, country, source, other, active, registration_date) VALUES ('aon@lycso.co.uk', SHA('test'), 'dfsa', 'sdafd', 'sdafs', '5656', 'adfafs', '', 'afdsds', 'asdfasd', 'County Down', 'asdfdsa', 'uk', '', '', 'f37f07fe7f097e104fc718113ce0ca26', NOW() ) MySQL Error: Do you understand what I am doing wrong here? I am lost...Kurt
Link to comment
Share on other sites

i guess with mysqli you should remove the $result in mysqli_num_rows, it will automatically use the last result, so if you have know the number of rows of a past query, while theres already a query after that ull have to save them in a variabletry it that way first, and see if it gives any new errors

Link to comment
Share on other sites

i guess with mysqli you should remove the $result in mysqli_num_rows, it will automatically use the last result, so if you have know the number of rows of a past query, while theres already a query after that ull have to save them in a variabletry it that way first, and see if it gives any new errors
I changed it to if (mysqli_num_rows($dbc) == 0)and get now for same line error mysqli_num_rows() expects parameter 1 to be mysqli_result, object givenIt also complains aboutforeach ($_POST['counties'] as $v) { $query .= "($v, $uid, 'N'), "; } withInvalid argument supplied for foreach() In addition it shows that it could not connect to second table from line$result = mysqli_query ($dbc, $query) or die ("could not connect to second table"); I really do not get why there is a problem with mysqli_num_rows. Before I created the second query this part was working well. Kind of strange it now starts complaining about the bit that was actually ok. As for the rest I can grasp that my new code might cause some problems, but really do not know why that is|-) As I said counties are a group of checkboxes.Could you have anohter look for me?Kurt
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...