By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,466 Members | 1,138 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,466 IT Pros & Developers. It's quick & easy.

Posting into a MSSQL Database

P: 5
I am trying to post values into a mssql database and having some trouble. I create a detail_id in the first query and want to pass this detail_id to several other queries to insert into several other tables. It creates the detail_id in the first table, but doesn't pass it on, when I try to retrieve it. Here is my code:

------------------------------------------------------------------------------------------------------------------
$query1 = "INSERT INTO Detail(date_contacted, user_id, issue_type, ref_by, contact_type, contact_descr, date_added, time_added)
VALUES '$date_contacted','$REMOTE_USER', '$issue_type_id','$ref _type_id','$type_id','$contact_descr','$today','$t ime')";

$mssql_result = mssql_query($query1);

$get_id = "SELECT detail_id FROM Detail where issue_type='$issue_type_id' and ref_by='$ref_type_id'";

$mssql_result = mssql_query($get_id);
$row = mssql_fetch_assoc ($mssql_result);
$NewDetail = intval ( $row['detail_id']);
$detail_id=$NewDetail;

$query2 = "INSERT INTO Contact(detail_id, user_id, date_contacted, contact_type, first_name, last_name, address, city, state, zip, email, phone, ext, alt_phone, contacted_by, start_time, end_time, total_time, email_content)

VALUES ($NewDetail, '$REMOTE_USER', '$date_contacted','$type_id', '$first_name', '$last_name', '$address', '$city', '$state', '$zip', '$email', '$phone', '$ext', '$alt_phone', '$contacted_by_id', '$start_time', '$end_time', '$total_time', '$email_content')";

$mssql_result = mssql_query($query2);
-------------------------------------------------------------------------------------------------------------------

The values from the form are going into the database, it just isn't getting the detail_id from the first query and putting into the second table.

Any help would be greatly appreciated!
Jul 27 '07 #1
Share this Question
Share on Google+
7 Replies


kovik
Expert 100+
P: 1,044
MySQL has a nifty little function called mysql_insert_id() which gets the primary key value of the last insert statement. Just another reason MySQL is better. :-P

print_r($row) so that you can see if the data is even being retrieved.
Jul 27 '07 #2

P: 5
MySQL has a nifty little function called mysql_insert_id() which gets the primary key value of the last insert statement. Just another reason MySQL is better. :-P

print_r($row) so that you can see if the data is even being retrieved.

The data is going into the database, it is just not getting the detail_id from the previous query.
Jul 27 '07 #3

kovik
Expert 100+
P: 1,044
The data is going into the database, it is just not getting the detail_id from the previous query.
I didn't say the data wasn't going in, I'm saying that you might not be getting the data out. That's what $row is. print_r($row) and tell us what the contents are.
Jul 27 '07 #4

P: 5
I didn't say the data wasn't going in, I'm saying that you might not be getting the data out. That's what $row is. print_r($row) and tell us what the contents are.
I get the following error:
Warning: mssql_fetch_assoc(): supplied argument is not a valid MS SQL-result resource in /var/www/html/victim/victim_form.php
Jul 30 '07 #5

kovik
Expert 100+
P: 1,044
I get the following error:
Warning: mssql_fetch_assoc(): supplied argument is not a valid MS SQL-result resource in /var/www/html/victim/victim_form.php
-_-
What did you change? print_r() only prints data like echo, but prints the elements as well. All I want you to do is get the contents of $row displayed to the screen.
Jul 30 '07 #6

P: 5
-_-
What did you change? print_r() only prints data like echo, but prints the elements as well. All I want you to do is get the contents of $row displayed to the screen.
Okay get the following:

Array ( [detail_id] => 252 )

This is the same result every time I run the query.
Jul 31 '07 #7

P: 5
Okay get the following:

Array ( [detail_id] => 252 )

This is the same result every time I run the query.
It is now working. I guess the following line:

$get_id = "SELECT detail_id FROM Detail where issue_type='$issue_type_id' and ref_by='$ref_type_id'";

The "where" portion of this was causing the problem. Once I commented this out it started working. Thank you for your help on this issue.
Jul 31 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.