|
Posted by pangea33 on 11/22/06 23:13
so many sites so little time wrote:
> ok so i am having problems if you look at the script below you will
> see that it the query has 4 values to insert but the actual values only
> contain title entry and now() for the date. well i have made the
> database and the blog_id is a primary auto interger what ever table
> bascly look below the the insert code block to find the code block
> that makes the table in the database,
>
>
> // Define the query.
> $query = "INSERT INTO blog_entries (blog_id, title, entry,
> date_entered) VALUES ('{$_POST['title']}', '{$_POST['entry']}',
> NOW())";
>
> // Execute the query.
> if (@mysql_query ($query)) {
> print '<p>The blog entry has been added.</p>';
> } else {
> print "<p>Could add the entry because: <b>" . mysql_error() . "</b>.
> The query was $query.</p>";
> }
>
> ---------------------------------------------------------------------------------------------------------------------------------------------------
>
>
> // Define the query.
> $query = 'CREATE TABLE blog_entries (
> blog_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
> title VARCHAR(100) NOT NULL,
> entry TEXT NOT NULL,
> date_entered DATETIME NOT NULL
> )';
>
> // Run the query.
> if (@mysql_query ($query)) {
> print '<p>The table has been created.</p>';
> } else {
> die ('<p>Could not create the table because: <b>' . mysql_error() .
> '</b>.</p><p>The query being run was: ' . $query . '</p>');
> }
>
> ---------------------------------------------------------------------------------------------------------------------------------------------
> >>>>blog_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,<<<<
> basicly that right above is what the table looks like now though if i
> go and run the insert script it says that the collumns dont match the
> values ect. but how can i get it to utilize the blog id table?
> what do i enter as a value?
Since blog_id is an auto_incrementing field, you shouldn't explicitly
refer to it. MySQL will handle it, then you can get the auto created
value. Try this...
$query = "INSERT INTO blog_entries (title, entry, date_entered) VALUES
('{$_POST['title']}', '{$_POST['entry']}', NOW())";
mysql_query($query);
printf("Last inserted record has id %d\n", mysql_insert_id());
http://us3.php.net/mysql_insert_id
Navigation:
[Reply to this message]
|