|
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
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 1 of 1 people found this answer helpful. Did you? Yes No
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 1 of 1 people found this answer helpful. Did you? Yes No 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 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'" no just change the $id from $_SESSION['id'] to the user id great its working, how would i go about putting mutiple users into the id though? look at the new code
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 2 of 2 people found this answer helpful. Did you? Yes No i still get an 'error' from the echo ok just try agian and if show error let me see the return query 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? ok: just let me see the out put of var_dump($_SESSION); and var_dump($_POST); im sorry, im unsure what that means? check the html please and try the code again to check with var_dump I GET THIS...
array(2) { ["id"]=> string(1) "1" } array(2) { ["tdanews"]=> string(14) " 1YTJGHNBGVC" }
|
© Advanced Web Core. All rights reserved

