Jump to content

[SOLVED] Adding something to the database with an ID


jonoc33

Recommended Posts

Hi guys

I have a message of the day system where the admin submits a message of the day and adds it to the database. When you add to the database I want to give it an ID that goes 0-1-2-3-4 etc. when you add it.

 

<?php
include("config.php");
$con = mysql_connect("localhost","jonoc33","*****");
if (!$con)
  die('Could not connect: ' . mysql_error());
mysql_select_db("revolutiongamerz_net_-_squads", $con);
$sql="INSERT INTO alpha_motd (motd) VALUES ('" . $_POST["motd"] . "')";
$rs = mysql_query($sql) or die ("Problem with the query: $sql <br>" . mysql_error);
echo "<center>Message of the Day Added.";
echo "<center><a href=admin.php>Back</a>";

?> 

That is the code which adds it to the database. Notice it didnt add anything to ID, which I need someone to fix up for me.

 

Then, I have a php script which shows what the message of the day is. But what I need it to do is show the most recent one, so the higher the ID the more recent it is. How would I do that? The script which shows what the FIRST record in the database is is this:

 

<?

$results = mysql_query("SELECT motd FROM alpha_motd");
if($values = mysql_fetch_array($results)){
     echo $values['motd'];
 }
 ?>

 

Can someone fix up both for me? Thanks

Link to comment
Share on other sites

n~ link=topic=165647.msg728446#msg728446 date=1193725858]

Do you have Id field in the table, if you dont have you can add one and make it auto increment so when you add new records the id will be assigned automatically

 

 `id` INT( 9 ) NOT NULL AUTO_INCREMENT PRIMARY KEY 

 

later you can fetch Ascending or descending order

ID is working now, how would I fetch the first record in Descending order (the one with the highest id)?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.