Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm having an issue checking if the data I entered is already in the database table and if it's not then inserting it into the table. What I can't figure out is how to get the id (it is primary and auto increment) of the data just inserted into the database.

I don't think it's possible to insert it if it is not already in the database and then get the id of that data to be inserted into another table all from the same script. I could be wrong though. So what I have now is one script to check if it's inserted already and inserting it if it's not, then redirecting to another php script to get the id of the information.

I'm having many errors with this, it inserts it even if it's in the database and when it gets redirected to the second script I am receiving a server error. Any help would be great, thanks in advance!

EDIT: I replaced the two files with one, the new code is below. I am still getting problems though, it inserts duplicate interests in the first table with my interests list

<?php

require_once '../scripts2/app_config.php';
require_once '../scripts2/database_connection.php';
require_once '../scripts2/authorize.php';

session_start();

// Authorize any user, as long as they're logged in
authorize_user();

$user_id = $_SESSION['user_id'];

$interest = trim($_REQUEST['interest']);


$get_interests = "SELECT COUNT(*) AS cnt FROM interests WHERE name = " . $interest;
$query_interests = mysql_query($get_interests);
if($query_interests) {
$hjk = mysql_fetch_array($query_interests);
$cnt = $hjk['cnt'];
}
if($cnt < 1) {
// $num_interests = mysql_num_rows($query_interests);

// insert the interest if it is not already in the database
$insert_sql = sprintf("INSERT INTO interests " .
                              "(name) " .
    "VALUES ('%s');",
         mysql_real_escape_string($interest));


//insert the user into the database
$insert_query = mysql_query($insert_sql);
$id = mysql_insert_id();

// From the newly inserted interest get the id of the interest
$insert_user_interests = sprintf("INSERT INTO user_interests " .
                                                 "(user_profile_id, interest_id) " .
                            "VALUES (%d, %d);",
                             mysql_real_escape_string($user_id),
                             mysql_real_escape_string($id));

mysql_query($insert_user_interests);
//Redirect this user to the page that displays user information
$url = 'show_profile_user_interests.php';
header('Location: '. $url);
exit();
} else {
/* If the interest is already in the table, just insert the id of the interest and
user_profile_id */

$get_interests2 = "SELECT id FROM interests WHERE name = " . $interest;
$query_interests2 = mysql_query($get_interests2);
if($query_interests2) {
$hye = mysql_fetch_array($query_interests2);
$interest_id = $hye['id'];
}
$insert_user_interests = sprintf("INSERT INTO user_interests " .
                                                 "(user_profile_id, interest_id) " .
                            "VALUES (%d, %d);",
                             mysql_real_escape_string($user_id),
                             mysql_real_escape_string($interest_id));

mysql_query($insert_user_interests);

//Redirect this user to the page that displays user information
$url = 'show_profile_user_interests.php';
header('Location: '. $url);
exit();
}
?>

What I think is causing the main problem is below, But I could be wrong...

$get_interests = "SELECT COUNT(id) AS cnt FROM interests WHERE name = " . $interest;
$query_interests = mysql_query($get_interests);
if($query_interests) {
$hjk = mysql_fetch_array($query_interests);
$cnt = $hjk['cnt'];
}
if($cnt > 0) {
share|improve this question
add comment

4 Answers

up vote 0 down vote accepted

Why not

NSERT INTO user_interests IF NOT EXISTS name = $interest
    (user_profile_id, interest_id) 
    VALUES (...,...)

Because this way you're doing only 1 query instead of 2

EDITED

There is an error in your select query

"SELECT COUNT(id) AS cnt FROM interests WHERE name = " . $interest;

you need to add ' like

...name = '". $interest . "'";

if name is a varchar – pc-shooter just now edit

share|improve this answer
    
I'll give this a shot –  nikito2003 Feb 1 at 16:38
    
I'm not having much luck with this technique... –  nikito2003 Feb 1 at 20:10
    
There is an error in your select query "SELECT COUNT(id) AS cnt FROM interests WHERE name = " . $interest; you need to add ' like name = '". $interest . "'"; if name is a varchar –  pc-shooter Feb 1 at 21:32
    
That did the trick, adding the ' –  nikito2003 Feb 1 at 21:53
add comment

Sounds to me like you're looking for mysql_insert_id

share|improve this answer
add comment

You can do that in single file itself..

In insert_interest.php,you can get the recently data inserted id by using,

$insert_query = mysql_query($insert_sql);<br>
     $id = mysql_insert_id($insert_query);<br>
     Write your second table inser code here

by using the id you can insert the data to second table.

share|improve this answer
    
I changed it but now even if the interest is in the interest table it will insert it anyway and in the user_interest table it doesn't insert the id of the interest. –  nikito2003 Feb 1 at 16:38
    
How would I go about getting the id if the data is already in the table, to just insert it in user_interest? –  nikito2003 Feb 1 at 16:50
    
It should just be $id = mysql_insert_id(); I believe –  nikito2003 Feb 1 at 21:11
add comment

First of all use mysqli instead of mysql

Also there is an error

 $get_interests = "SELECT COUNT(*) AS cnt FROM interests WHERE name = " . $interest;

Use

 $get_interests = "SELECT COUNT(*) AS cnt FROM interests WHERE name = '".$interest."'";
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.