Jump to content

Recommended Posts

Hey guys,

 

Once my user has logged in I would like a welcome message to be displayed once the Admin has set this up.

At the moment if the user logs in and there is no welcome message set by the Admin there is a message displayed "No welcome message set yet".

 

However I need a button which is only available to Admins to create a message if there isnt one or edit the message if they would like to change something.

I have created the code so that if the user logged in is Admin they will see the option Edit Update (this is just text at the moment, no link)

 

How would I go about making it possible to create/edit the message?

 

My code so far:

<?php

$club_id = mysql_query("SELECT club_id FROM users WHERE user_id = '{$_SESSION['user_id']}'")
    OR DIE(mysql_error());
    $row = mysql_fetch_assoc($club_id);
    $club_id = $row['club_id'];
    
    $clubname = mysql_query("SELECT clubn FROM club WHERE club_id = '$club_id'")
        OR DIE(mysql_error());
    $row = mysql_fetch_assoc($clubname);
    $clubname = $row['clubn'];
    
    $message = mysql_query("SELECT welcome FROM welcome WHERE club_id = '$club_id'")
        OR DIE(mysql_error());
    $row = mysql_fetch_assoc($message);
    $welcome = $row['welcome'];

if (isset($welcome)) {   
    echo  $welcome;
} else {
$welcome = 'No welcome message set yet';
echo $welcome;
}

?>
</td>
</tr>
<tr>
<td>
<?php 
require_once ('../mysql_connect.php');
if (isset($_SESSION['user_id'])){
$sql = "SELECT member_type FROM users WHERE user_id = '{$_SESSION['user_id']}' LIMIT 1";
if ($result = mysql_query($sql)) {
if (mysql_num_rows($result)) {
$row = mysql_fetch_assoc($result);
if ($row['member_type'] == "Admin") {?>
Edit Update
<?php }}}} ?>
</td>
</tr>
</table>

 

Cheers

 

Elliot

Link to comment
https://forums.phpfreaks.com/topic/74235-solved-edit-message-in-php/
Share on other sites

just have a page with a textarea and get the admin to write the message in there then when they submit the form it updates your table and on edit just fill the textarea with the previous message then do the same as above.

 

<?php

// connect to db

$query = // some query
$row = mysql_fetch_assoc($query) or die (mysql_error());
$message = $row['fieldname'];
echo "<form method=\"post\" action=\"{$PHP_SELF}\">";
echo "<textarea name=\"message\">{$message}</textarea>";
echo "<input type=\"submit\" />";
echo "</form>";
if($_POST)
{
   $message = $_POST['message'];
   $update = "UPDATE table SET fieldname='$message' WHERE something='something'" or die (mysql_error());
   mysql_query($update) or die (mysql_error());
   echo "Message Updated!";
}
?>

 

Something along those lines, I think ;)

 

~ Chocopi ;D

Thanks for the help in the code however im still kinda learning my way with PHP.

 

When you say "some query" what do you mean by that?

 

My tables are:

- users: user_id, club_id, member_type, username, first_name, last_name, email, password, registration_date

- club: club_id, clubn, webtype

- welcome: welcome_id, club_id, welcome

 

Cheers

oh rite yea, sorry i meant to change that

 

It is the query you use to get the message from the database. so the code should be something like this:

 

<?php

// connect to db

$query = mysql_query("SELECT welcome FROM welcome WHERE club_id='$club_id'") or die(mysql_error());
$row = mysql_fetch_assoc($query) or die(mysql_error());
$message = $row['welcome'];
echo "<form method=\"post\" action=\"{$PHP_SELF}\">";
echo "<textarea name=\"message\">{$message}</textarea>";
echo "<input type=\"submit\" />";
echo "</form>";
if($_POST)
{
   $message = $_POST['message'];
   $update = "UPDATE welcome SET welcome='$message' WHERE club_id='$club_id'" or die(mysql_error());
   mysql_query($update) or die(mysql_error());
   echo "Message Updated!";
}
?>

 

I think those are the queries you would want.

  First one selects the message you want to edit from the database and then puts that into the textarea.

  Second one gets the posted data after the user submits the form and inserts it into the database.

 

Hope that clears it up for you ;D

 

~ Chocopi

Thanks alot for your help.

 

I got 1 error:

An error occured in script c:\program files\easyphp1-8\www\html\main.php on line 63: Undefined variable: PHP_SELF

 

Also, this works providing there is a message already set in the database, however originally when a new club is registered there is no message in the database just a simple output message, how would I go about creating a new blank entry from which the admin can type whatever they wish as the message?

 

Figured it would be just as easy to set the message to "No message has been set yet" on the registration page?

 

Any ideas on the error though?

 

Cheers

 

 

ok try replacing

 

$PHP_SELF

 

with

 

$_SERVER['PHP_SELF']

 

Also, if there is no record in the database then it should fill the textarea with nothing. So it should still update the database with whatever you put into the textarea.

 

But you could try replacing

 

$row = mysql_fetch_assoc($query) or die(mysql_error());
$message = $row['welcome'];

 

with

 

while ($row = mysql_fetch_assoc($query))
{
   $message = $row['welcome'];
   $check = 1;
}

 

and then replace

 

<?php
if($_POST)
{
   $message = $_POST['message'];
   $update = "UPDATE welcome SET welcome='$message' WHERE club_id='$club_id'" or die(mysql_error());
   mysql_query($update) or die(mysql_error());
   echo "Message Updated!";
}
?>

 

with

 

<?php
if($_POST)
{
   $message = $_POST['message'];
   if($check == 1)
   {
      $update = "UPDATE welcome SET welcome='$message' WHERE club_id='$club_id'" or die(mysql_error());
      mysql_query($update) or die(mysql_error());
      echo "Message Updated!";
   } else
      if($check != 1)
      {
          $insert = "INSERT INTO welcome (welcome) VALUES ('$message')"
          mysql_query($insert) or die(mysql_errror());
          echo "New Message Created!";
      }
}
?>

 

I think that might work ::)

 

~ Chocopi

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.