Well, my question is: I've been working on a simple project which basically saves info into a database through a form.
I'll post the codes here and explain what it is needed.
addnews.php
Code: Select all
<!-- Formulario HTML -->
<table border='0' cellpadding='2' cellpadding='2'>
<form name='news' method='POST'>
<tr>
<td>Title: </td><td><input type='text' name='title'></td>
</tr><br>
<tr>
<td>Author: </td><td><input type='text' name='author'></td>
</tr>
</table><br>
<textarea name='text' cols='90' rows='20' wrap='hard'></textarea><br>
<input type='submit' name='submit' value='Add new'><br>
</form>
<!-- Intrucciones PHP -->
<?php
$date= time();
if ($submit)
{
include ("dbconnect.php"); // We connect to the db and select the table.
mysql_query("INSERT INTO $table (title, date, text, author) VALUES ('$title', '$date', '$text', '$author')");
mysql_close();
echo "The information was successfuly saved."; // This message will be printed if everything went alright so far.
}
else
{
echo "There was an error saving your info in the database, try again later."; // This message will be printed if there was an error.
}
?>Then I made this script to output the info.
news.php
Code: Select all
<html>
<h2>Database information</h2>
<p></p>
</html>
<?php
include ("dbconnect.php");
$result= mysql_query("SELECT * FROM $table ORDER BY date DESC");
$num= mysql_num_rows($result);
$i=0;
while ($i < $num)
{
$row= mysql_fetch_array($result);
echo date("d/m/Y", $row['date']) ." - ". $row['title'] ."<br />". $row['text'] ."<br />~ ". $row['author'];
$i++;
}
mysql_close();
?>The problem is that I don't know how or where to specify the 'id' field to each new I add. "Why do I want to do this?" It's the only way I see I can make an edit or delete option for this system. What I mean is, I'd like to add an option to edit a new already stored in the database, but my problem is that I don't know how to select a new before I edit.
For example, If I have 3 news, id=1,2,3. I'd like to edit the new id=2. How do I do this? I know how to code it but I don't know where to put or where should I specify the id for each new. This is the same for DELETE a new.
Please, someone help me. I'm just stuck with this!
Thank you very much.
PD: I'm sorry for my bad english.