Have an account? Sign in
Login  Register  Facebook
update a row for all users from one id
i have a login system in place, i decided it may be a good idea to have a news section, which i can update from my admin login.

I created a row called 'tdanews'. now i have manually inserted some info into it, and echo'd it into the users login, it works fine, now i have tried to make a script so i can submit a form from my admin page to updte it, but i cant get it to work, any advice?

my code..
session_start();
    if (!isset($_SESSION['id'])) {
        echo 'Please <a href="login.php">log in</a> to access your account';
        exit();
    }
    include_once "connection.php";
    // Place Session variable 'id' into local variable
    $id = $_SESSION['id'];
    // Process the form if it is submitted
    if ($_POST['newtdanews']) {
        $tdanews = $_POST['tdanews'];
        $sql = mysql_query("UPDATE thedriving3 SET tdanews='$newtdanews' WHERE id='$id'");
        echo 'Your account info has been updated, visitors to your profile will now see the new info.';
        exit();
    }else
    echo "error";// close if post
?>
<?php
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM members WHERE id='$id' LIMIT 1");
    while($row = mysql_fetch_array($sql)){
    $tdanews = $row["tdanews"];
    }

thanks
Started: September 16, 2011 Latest Activity: September 16, 2011 php mysql update
3 Answers
now follow this if you want to make admin panal for update the users
1- make a php page users.php
<?php
session_start();
include_once "connection.php";

    if (isset($_SESSION['id'])) {
        // exit if you is not the admin
        if($_SESSION['id'] <> 1){
            exit('you is not the admin');    
        }
    }else{         
        exit('Please <a href="login.php">log in</a> to access your account');
    }

    // get the user if from the url
    $id = intval($_GET['id']);
    // Process the form if it is submitted
    if (isset($_POST['tdanews'])) {
        $tdanews = $_POST['tdanews'];
        $query = "UPDATE thedriving3 SET tdanews='$tdanews' WHERE id='$id'";
        $sql = mysql_query($query);
        if($sql){
            echo "the account info with $id has been updated.";    
        }else{
            echo 'error:'.$query;    
        }
        exit();
    }

    $sql = mysql_query("SELECT * FROM members WHERE id='$id' LIMIT 1");
    $row = mysql_fetch_array($sql)
       
?>
<form method="post" enctype="text/plain">
this for the user:<?php $row['name'] //i'm now sure its called name please make sure' ?>
<label for="tdanews">the text:</label>
<textarea id="tdanews" name="tdanews">

Posted: MacOS
In: September 16, 2011

that works & changes the admin field!,

how would i make it change all users field? ie the whole row rather than the users? would i need to change the WHERE?

thanks

Posted: rgby
In: September 16, 2011

it change the admin because the $_SESSION['id'] is 1 and admin id is 1 to change any other try to work on $_SESSION['id'] this mean $_SESSION['id'] = the user who will change; and if you want to change any other user you should add this user id as a post value and change the code
September 16, 2011

i have tried to alter 'id' to the other users id numbers but it still only updates the admin id. should i perhaps create another row eg update with the field value 'yes' then on the syntax i change it to "update members set tdanews='tdanews' where update='$update'"
September 16, 2011

no just change the $id from $_SESSION['id'] to the user id
September 16, 2011

great its working, how would i go about putting mutiple users into the id though?
September 16, 2011

look at the new code
September 16, 2011

see first you have:
1- error in
if (isset($_POST['newtdanews']))
it should be
if (isset($_POST['tdanews']))
now try this code
<?php
session_start();
include_once "connection.php";

    if (!isset($_SESSION['id'])) {
        exit('Please <a href="login.php">log in</a> to access your account');
    }

    // Place Session variable 'id' into local variable
    $id = intval($_SESSION['id']);
    // Process the form if it is submitted
    if (isset($_POST['tdanews'])) {
        $tdanews = $_POST['tdanews'];
        $query = "UPDATE thedriving3 SET tdanews='$tdanews' WHERE id='$id'";
        $sql = mysql_query($query);
        if($sql){
            echo 'Your account info has been updated, visitors to your profile will now see the new info.';    
        }else{
            echo 'error:'.$query;    
        }
        
        
        exit();
    }

// Query member data from the database and ready it for display
    $sql = mysql_query("SELECT * FROM members WHERE id='$id' LIMIT 1");
    while($row = mysql_fetch_array($sql)){
    $tdanews = $row["tdanews"];
    }
?>

Posted: MacOS
In: September 16, 2011

i still get an 'error' from the echo
September 16, 2011

ok just try agian and if show error let me see the return query
September 16, 2011

it now just comes with a blank page, could it be due to me asking where id=$id, could i change it to something else as surely id=$id will only change the admins field?
September 16, 2011

ok: just let me see the out put of var_dump($_SESSION); and var_dump($_POST);
September 16, 2011

im sorry, im unsure what that means?
September 16, 2011

check the html please and try the code again to check with var_dump
September 16, 2011

I GET THIS... array(2) { ["id"]=> string(1) "1" } array(2) { ["tdanews"]=> string(14) " 1YTJGHNBGVC" }
September 16, 2011

Your Answer

xDo you want to answer this question? Please login or create an account to post your answer