Catherine
Catherine

Reputation: 45

pass the value of get_insert_id

Can you please help me here. I am trying to save the last id inserted, but it keeps on saving 0, which I don't understand why. Sorry noob here :( Here are my codes

the index

<form action="save_member.php" method="POST">
            Enter your username: <input type="text" name="member"/>
            <input type="submit" value="Submit"/>
        </form>

the save_member

include 'connect.php';
session_start();
$_SESSION['member_id'] = $_POST[member];
$sql = mysql_query("insert into users (idusers, member) values ('', '$_POST[member]')");
$_SESSION['last_id'] = mysql_insert_id();
header("Location: chat.php");

the chat

    <?php
    session_start();
    echo "hello" . " " . $_SESSION['member_id'];
    echo "<br/>";
    ?>
    <?php
    include 'connect.php';
    $q = mysql_query("select * from messages order by time_sent desc");
    while ($w = mysql_fetch_array($q)) {
        ?>
        <div id="show_messages" style="">
            <span id="wrap_messages" style="">
                <span id="message" style=""><?php echo $w['message']; ?> </span> :
                <span id="member_id" style=""><?php echo $w['member_id']; ?></span>
            </span>
        </div>
    <?php } ?>
    <br/>
    <div style="margin-right: 450px !important; width: 300px; float: right;">
        <form action="sent_chat_message.php" method="POST">
            <input type="hidden" name="member_id"/>
            <input type="text" name="chat"/>
            <input type="submit" value="Submit"/>
        </form>
    </div>

and the sent_chat_message

include 'connect.php';
session_start();
$_SESSION['last_id'] = $last_id;
$r = mysql_query("insert into messages (idmessages, message, member_id) values ('', '$_POST[chat]', '.$last_id.')");
header("Location: chat.php")

Upvotes: 0

Views: 210

Answers (4)

Catherine
Catherine

Reputation: 45

this thread is solved. What I did is

if (isset($_SESSION['last_id']))
    $_SESSION['last_id'] = $_SESSION['last_id'];
else
    $_SESSION['last_id'];

Upvotes: 0

swapnilsarwe
swapnilsarwe

Reputation: 1300

Are you sure the insert query is getting executed successfully?

Coz' thats must be the reason you are getting 0 as the result of the mysql_insert_id aster insert query

If idusers is the primary key I will suggest pass the NULL value instead of empty string

$sql = mysql_query("insert into users (idusers, member) values ('', '$_POST[member]')");

Your query should be

$sql = mysql_query("insert into users (idusers, member) values (NULL, '".mysql_real_escape_string($_POST[member])."')");

Upvotes: 1

Tom Macdonald
Tom Macdonald

Reputation: 6593

From php.net :

Retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT).

Is your id field set to AUTO_INCREMENT?

Also, use PDO or mysqli. mysql_* is deprecated.

Upvotes: 1

Ahmad
Ahmad

Reputation: 12737

You might want to add the ID tag just in-case your browser does not support the NAME tag Try this

<form action="save_member.php" method="POST">
            Enter your username: <input type="text" name="member" id="member"/>
            <input type="submit" value="Submit"/>
        </form>

Upvotes: -1

Related Questions