Jump to content

Select and Edit with Option fields


Xager

Recommended Posts

I have a news posting system on my website, its pretty basic but its all I need. What I have as fields are id, title, author, post, date. I have them all displaying properly as I want them, but now what I want to do is add an options (drop down menu) field aswell. Now I have added newstype ENUM 'field1','field2','field3','field4' NOT NULL DEFAULT field1 to my news database and I want to do is add a dropdown to my addnews.php form so that I can input these fields.

 

addnews.php:

<?php
   if (isset($_POST['submitted'])) {
   include ('mysql_connect.php');
   if (empty($_POST['title'])) {
   echo '<p><font color="red">You need to enter a title.</font></p>';
   } else {
   $title = $_POST['title'];
   }
    
   if (empty($_POST['name'])) {
   echo '<p><font color="red">You need to enter a name.</font></p>';
   } else {
   $name = $_POST['name'];
   }
     
   if (empty($_POST['message'])) {
   echo '<p><font color="red">You need to enter a message.</font></p>';
   } else {
   $message = $_POST['message'];
   }
     
   if ($title && $name && $message) {
   $query = "INSERT INTO news_posts (title, author, post, date) VALUES ('$title', '$name', '$message', NOW())";
   $result = @mysql_query($query);
     
   if ($result) {
   echo '<p><font color="red">News was added!</font></p>';
   } else {
   echo '<font color="red"><p>News could not be added! Please try again.</p></font>';
   }
   } else {
   echo '<p><font color="red">Please fill in the appropriate information</font></p>';
   }
   }
   ?>
     
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<p><b>News Title :</b><br />
<input type="input" name="title" size="25" maxlength="60" value="<?php if(isset($_POST['title'])) echo $_POST['title']; ?>" /></p>
          
<p><b>Name :</b><br />
<input type="input" name="name" size="15" maxlength="35" value="<?php if(isset($_POST['name'])) echo $_POST['name']; ?>" /></p>

<p><b>Message :</b><br />
<textarea rows="7" cols="55" name="message"><?php if(isset($_POST['message'])) echo $_POST['message']; ?></textarea></p>
     
<p><input type="submit" name="submit" value="Add News" /></p>
<input type="hidden" name="submitted" value="TRUE" /></p>
</form>

 

Any ideas on how / where to add this said drop down field? I have never done one before and dont know enough PHP to figure out how.

 

Also, I have a Edit form. I also would need some help with getting it up and running with a drop down field.

 

edit_news.php:

<?php
include('mysql_connect.php');
if ((isset($_GET['id'])) && (is_numeric($_GET['id'])) ) {
$id = $_GET['id'];
} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) {
$id = $_POST['id'];
} else {
echo 'Please choose a news post to edit.';
exit();
}
if (isset($_POST['submitted'])) {
$errors = array();
if (empty($_POST['title'])) {
$errors[] = 'You forgot to enter a title.';
} else {
$title = $_POST['title'];
}
if (empty($_POST['name'])) {
$errors[] = 'You forgot to enter an author.';
} else {
$name = $_POST['name'];
}
if (empty($_POST['message'])) {
$errors[] = 'You forgot to enter a message';
} else {
$message = $_POST['message'];
}
if (empty($errors)) {
$query = "UPDATE news_posts SET title='$title', author='$name', post='$message' WHERE id=$id";
$result = mysql_query($query);
if ($result) {
echo "News Post Has Been Updated!";
} else {
echo "News post could not be updated.";
}
} else {
echo 'News post could not be updated for the following reasons -<br />';
foreach ($errors as $msg) {
echo " - $msg<br />\n";
}
}
} else {
$query = "SELECT title, author, post, id FROM news_posts WHERE id=$id";
$result = mysql_query($query);
$num = mysql_num_rows($result);
$row = mysql_fetch_array ($result, MYSQL_NUM);
$title = $row['0'];
$name = $row['1'];
$message = $row['2'];
if ($num == 1) {
echo '<h3>Edit News Post</h3>
<form action="?id=edit_news&num='.$id.'" method="post">
<p><b>News Title :</b> <input type="text" name="title" size="25" maxlength="255" value="'.$title.'" /></p>
<p><b>Name :</b> <input type="text" name="name" size="15" maxlength="255" value="'.$name.'" /></p>
<p>Message :</b> <br /><textarea rows="5" cols="40" name="message">'.$message.'</textarea></p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" /></p>
<input type="hidden" name="id" value="'.$id.'" />';
} else {
echo 'News post could not be edited, please try again.';
}
}
?>

 

My website is www.xager.net to see a working version of what i have now.

 

Thank you very much for any responces. Looking forward to your input.

 

Regards,

Xager

Link to comment
Share on other sites

Ok, maybe I wasnt quite clear.

when I input new items i use my addnews.php form with Title (text field), Author (text field) and Content (text field). What I would like to do is insert a drop down menu, as in <select...><option>Option1</option><option>Option2</option></select>. How do I go about this with coherence with my script from previous post.

 

Thanks for any replies.

Regards,

Xager

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.