Jump to content


Photo

[RESOLVED] mysql_insert_id()


  • Please log in to reply
4 replies to this topic

#1 Mr Chris

Mr Chris
  • Members
  • PipPipPip
  • Advanced Member
  • 336 posts

Posted 11 July 2006 - 10:10 AM

Hi Guys,

The below part of a form submits data to a Mysql database.

<?php

session_start();

// ** Connect to DB **
include("*********");


// ** If Submit is hit do your stuff **
    if (isset($_POST['Submit'])) {

    $section = $_POST['section'];
    $added_by = $_POST['added_by'];
    $headline = $_POST['headline'];
    $byline_name = $_POST['byline_name'];
    $appeared = $_POST['appeared'];
    $published = $_POST['published'];
    $opening = $_POST['opening'];
    $body_text = $_POST['body_text'];
    $quote = $_POST['quote'];
    $term_one = $_POST['term_one'];
    $term_two = $_POST['term_two'];
    $term_three = $_POST['term_three'];
    $term_four = $_POST['term_four'];
    $notes = $_POST['notes'];

// ** Check for Required Fields with IF statements **
       if (empty($section)){
        $error = "** You forgot to enter the section for the story! **"; 
    }  else if (empty($added_by)){
        $error = "** Error: You forgot to who added the story! **";
    }  else if (empty($headline)){
        $error = "** Error: You forgot to enter a headline for the story! **";
    }  else if (empty($byline_name)){
        $error = "** Error: You forgot to enter a byline name for the story! **";
    }  else if (empty($published)){
        $error = "** Error: You forgot to the date you wish to publish the story! **";
    }  else if (empty($opening)){
        $error = "** Error: You forgot to the Opening Paragraph for the Indexes! **";
    }  else if (empty($body_text)){
        $error = "** Error: You forgot to enter any body text! *";

// ** If all of the statements are true then **
    } else {
    $query = "INSERT INTO cms_stories(section,added_by,headline,byline_name,appeared,published,opening,body_text,quote,term_one,term_two,term_three,term_four,notes) VALUES 
    ('$section','$added_by','$headline','$byline_name','$appeared','$published','$opening','$body_text','$quote','$term_one','$term_two','$term_three','$term_four','$notes')";
    $story_id=mysql_insert_id();
    header("Location: http://*****************/pictures/upload.php?story_id=$story_id"); 

// ** And finally run the query to add data to the database **
     $link = mysql_connect; 
     mysql_select_db($db); 
     $result = mysql_query($query) or die('Query failed: ' . mysql_error()); 
     mysql_close(); 
} 
}
?>

Now when the if conditions are met and the data is added it then re-directs to:

header("Location: http://www.*****************/pictures/upload.php?story_id=$story_id"); 

Now this is where I want to get the story_id of the record just entered, but each time it just re-directs as:

http://www.*********....php?story_id=0

When I thought   $story_id=mysql_insert_id() would grab that last story_id?

Many Thanks

Chris


#2 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 11 July 2006 - 10:11 AM

I dont think you dont appear to be insterting a last id, so it wont be able to retrieve one.

#3 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 11 July 2006 - 10:14 AM

you have not submitted the query prior to calling the insert id!!!!
$query = "INSERT INTO cms_stories(section,added_by,headline,byline_name,appeared,published,opening,body_text,quote,term_one,term_two,term_three,term_four,notes) VALUES 
    ('$section','$added_by','$headline','$byline_name','$appeared','$published','$opening','$body_text','$quote','$term_one','$term_two','$term_three','$term_four','$notes')";
    $story_id=mysql_insert_id();

cahneg to:
$query = "INSERT INTO cms_stories(section,added_by,headline,byline_name,appeared,published,opening,body_text,quote,term_one,term_two,term_three,term_four,notes) VALUES 
    ('$section','$added_by','$headline','$byline_name','$appeared','$published','$opening','$body_text','$quote','$term_one','$term_two','$term_three','$term_four','$notes')";
$query = mysql_query($query);
    $story_id=mysql_insert_id();

follow me on twitter @PHPsycho

#4 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 11 July 2006 - 10:18 AM

yes, you need to run the query before you call the insert_id bit, as it is taking it as you have not inserted anything and showing you that

#5 Mr Chris

Mr Chris
  • Members
  • PipPipPip
  • Advanced Member
  • 336 posts

Posted 11 July 2006 - 11:16 AM

Cheers GUYS,

That was stupid of me  :-\

I guess though an extra pair of eyes helps!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users