Mysql Database Isn't Updating - php

Okay so users on my game have a page. Other users can comment on the page. I made a script to where the user can set it to where people can comment on their wall or if it's disabled. If I manually change the database it works, but I have it setup with radio buttons and it's not updating. Here's the form.
<form action="settings" method="post">
Comments: <br />
Enabled:
<input type="radio" name="change_wall" id="change_wall" value="no" checked="checked" />
<br />
Disabled:
<input type="radio" name="change_wall" id="change_wall" value="yes" />
<br />
<input type="submit" value="Change" />
</form>
Below this is the SQL of the database updating.
<?php
if ( isset ( $_POST['change_wall'] ) )
{
$change_wall = mysql_real_escape_string($_POST['change_wall']);
if ($cash >= 5000) {
$sql = "UPDATE users SET disable_wall='".$change_wall."' , cash=(cash - 5000) WHERE id='".$id."'";
$res = mysql_query($sql);
echo
'<table width="800" align="center" class="SettingsTable">
<tr>
<td>You sucessfully changed your comment settings!</td>
</tr>
</table>
<br />';
}
else {
echo
'<table width="800" align="center" class="SettingsTable">
<tr>
<td>You don\'t have enough cash to change your comment settings!</td>
</tr>
</table>
<br />';
}
}
?>
Here's the coding for the user's page to display their comments or if it's disabled.
<?php
if ($disable_wall = 0) {
include 'users_wall.php';
}
elseif ($disable_wall = 1) {
echo
'<table width="800" align="center" class="DisabledWall">
<tr>
<td>' . $userp['name'] . '\'s ' . 'Comments Disabled!</td>
</tr>
</table>';
}
?>

first of all, be sure that the sql query is executed - $cash is really >= 5000 and $id variable consists proper value
then, what type does the table field 'disable_wall' have? varchar or enum? not int?
if instructions above didnt help, type this after last line of your script:
die("error: ".mysql_error());
submit the form and watch, what error happened while you are updating the table

Because you need a boolean (yes/no) response, you can simply set them to 0 or 1, to be safe.
To answer your question, do
$sql = "UPDATE users SET disable_wall='1' , cash=(cash - 5000);
And to disable the wall
$sql = "UPDATE users SET disable_wall='0' , cash=(cash - 5000);
Better yet, use a boolean column for it.
Also, ensure that $id is a valid value, and currently has at least a row in the table.
More importantly, use PDO or MySQLi; MySQL extensions are already deprecated.
Hope this helps.

Related

Add and Remove Friend Button

I am with some difficulties on create an Add Friend and Remove Friend button.
For example if the 'accepted' row in mysql of table friends_request is equals to 1 then it should appears the 'Remove Friend' button, otherwise it should appears the 'Add Friend' button.
Here is the code of the buttons I did, which is wrong.
<?php
$selfriendsBtn = "SELECT fr.username
FROM (select from_username AS username
FROM friends_request
WHERE to_username = '".$_GET['u']."' AND accepted = 1
UNION ALL
SELECT to_username AS username
FROM friends_request
WHERE from_username = '".$_GET['u']."' AND accepted = 1) AS fr
JOIN users AS u ON u.username = fr.username LIMIT 5";
$resultfriend_add_rmv = $sql->query($selfriendsBtn);
$rowAdd_RmvFriend = mysqli_fetch_assoc($resultfriend_add_rmv);
$accepted = $rowAdd_RmvFriend['accepted'];
if($user != $_GET['u']) {
if($accepted == 1) {
?>
<table>
<tr>
<td>
<form action="profile.php?u=<?php echo $_GET['u']; ?>&deleted=<?php echo $user; ?>" method="POST">
<input type="submit" name="rmvfriend" value="Remove Friend" />
</form>
</td>
<td>
<form action="profile.php?u=<?php echo $_GET['u']; ?>&a=compose" method="POST">
<input type="submit" name="msg" value="Message" />
</form>
</td>
</tr>
</table>
<?php
} else {
?>
<table>
<tr>
<td>
<form action="profile.php?u=<?php echo $_GET['u']; ?>&requested=<?php echo $user; ?>" method="POST">
<input type="submit" name="addfriend" value="Add Friend" />
</form>
</td>
<td>
<form action="profile.php?u=<?php echo $_GET['u']; ?>&a=compose" method="POST">
<input type="submit" name="msg" value="Message" />
</form>
</td>
</tr>
</table>
<?php
}
}
?>
The SELECT tables I pick up from the friends.php page and implemented it on here.
I don't know why it's not working. Should I use loop or not? Well, in my opinion I think a loop it would work very well on this.
You're looking for a value that isn't present in the result set:
$accepted = $rowAdd_RmvFriend['accepted'];
The only value you select from the database is the username:
SELECT fr.username
FROM ...
So $accepted will never be equal to 1. Thus, this will always be false:
if ($accepted == 1)
So the else block (the "Add Friend" button) will always be shown.
You should also select the accepted value from your query so you can use it in your logic. This is mostly a guess based on your query so far, but it looks like you'd need to add it in three places:
SELECT fr.username, fr.accepted -- here
FROM (select from_username AS username, accepted -- here
FROM friends_request
WHERE to_username = ? AND accepted = 1
UNION ALL
SELECT to_username AS username, accepted -- here
FROM friends_request
...
Keep in mind that nobody here really knows your data, so you may need to tinker with the query a little bit in your database management tools to get it just right.

why if (isset($_POST['accept'])) not working?

i have small problem and i don't see any mistakes in my code and server also don't returns any errors but clouse if (isset($_POST['accept'])) doesn't recognize that button was clicked.And is it some way to get value of button id?Please Help.
include '../../config.php';
db_connect();
$zapytanie = "SELECT * FROM messages ORDER BY message_id DESC";
$r = mysql_query ($zapytanie) or die(mysql_error());
$war="L".$_SESSION['lekarz_id'];
echo $war;
if (isset($_POST['accept']))
{
echo "something";
}
else{
while ($row = mysql_fetch_array ($r))
{
if($war == $row['message_recipient'] AND !$row['message_title']=='LP' ){
echo $war;
print "<table border=2 width=98% align=center>
<tr><td><font size=1><p align=left>Od: {$row['message_recipient']}</td> <td><p align=right>Wysłana: {$row['date_of_posting']}</align></font></td></tr>
<tr><td><p align=left><font size=1>Do:{$row['message_sender']}</td><td><b><p align=right>Temat:{$row['message_title']}</align></b></td></tr>
<tr><td colspan=2><br /> {$row['message_text']}</align></font></td></tr>
</table><hr />";
}
else if($war == $row['message_recipient'] AND $row['message_title']=='LP'){
echo "
<form>
<table border=2 width=80%>
<tr>
<td>Od:Wysłana: {$row['date_of_posting']}</td>
<td><p align=center>Wysłana: {$row['date_of_posting']}</align></font></td>
</tr>
<tr>
<td><p align=center><font size=5>Do:{$row['message_sender']}</td><td><b><p align=right>Temat:{$row['message_title']}</align></b>
</td>
</tr>
<tr>
<td ><br /> <font size=5>{$row['message_text']}</align></font>
</td>
</tr>
<tr>";
$id = $row['message_sender'];
$id=substr($id, -1);
print'
<td>
<form action="messages.php" method="post">
<input type="submit" name="accept" value="accept" id=.{$id}./>
</form>
</td>
<td> </td>
</tr>
</table>
</form><hr />
';
}
}
db_close($db);
}
?>
You didn't explicitly set the form action to POST. As a result your form values willbe sent via GET.
Change:
<form>
to
<form method="POST">
Or look for for your value in the $_GET superglobal.
if (isset($_GET['accept']))
You also have a form embedded within a second form. Removing the outer form would also solve your problem.
Check for $_REQUEST['accept']. This combines $_GET, $_POST and $_COOKIE. It will contain the form value regardless of submission method.
http://www.php.net/manual/en/reserved.variables.request.php
I also have to mention, that it's not a good idea to use submit button value to send parameter. What if you need to write "Accept" instead of "accept" on your button?
if ($_POST['accept']=='accept') will fail for such button. Better practice is to use hidden field with name="accept" (<input type="hidden" name="accept" value="any_value" />) and let submit button to do what it has to do - to submit form.
Also it makes your form reusable
phpfidle

how to insert values from checkbox to MYSQL Database?

<table width="200" border="0">
<tr>
<td><p>
<?php while($row1 = mysql_fetch_array($res)){ ?>
<label>
<input type="checkbox" name="module" value="<?php echo $row1['MID']?>" id="<?php echo $row1['MID']?>">
<?php echo $row1['ModuleName']?></label>
<br>
<?php }?>
</p></td>
</tr>
</table>
I use this code to outout the checkboxes,and the checkboxes appear allright.
I m not sure about how,the data can be saved to the MYSL table.
the table i drew is in the form:
student_module{studentID,ModuleID}
what is the code i should write to enter the data to that table.
One student can enroll to many modules.
Using ajax functionality:
By clicking on checkbox collect the value of studentID and ModuleID and pass to the ajax method and then in your server end page, you can update db record.
Without ajax functionality
Post the form when user clicking on checkbox or put a save button below to submit the form on a PHP page and then you can collect all your values and update db accordingly.
<input type="checkbox" name="chk1[]" value="" ><?php echo $row1['ModuleName']?>
if(isset($_POST['submit']))
{
$checkbox1 = $_POST['chk1'];
$selected_checkbox = "";
foreach ($checkbox1 as $checkbox1)
{
$selected_checkbox .= $checkbox1 . ", ";
}
$selected_checkbox = substr($selected_checkbox, 0, -2);
// now here in your insert query take mid='".$selected_checkbox."';
}

PHP Update query not receiving variable already defined in page

Hi I'm trying to update a single field from a HTML form, for some reason one of the session variables I am passing to the update query is not being accepted. I have already echoed the variable in the page so am fairly certain it exists in memory.
NB, I know my code is horrifically insecure but I'm learning PHP and once I've got the basics working Ill go over it and bring it upto best practice standards.
E2A: If I do var_dump($filename); before trying to run the query it returns string(6) "356/18", after the query it returns NULL. I'm not unsetting the variable anywhere so where could it be going!
Here is my form:
<form method="post" action="">
<p>Your username is: <?php echo $_SESSION['userid'] ?> Your company ID is: <?php echo $companyid['id']?></p>
<h3>Please enter note for file: <?php echo $filename; ?></h3>
<table width="200" cellpadding="5">
<tr>
<th width="18%" align="right" nowrap>Add Note: </th>
<td width="82%" nowrap>
<input type="text" name="note" />
</td>
</tr>
<tr>
<td colspan="2" width="100%" nowrap>
<input type="submit" value="Submit" name="Submit" />
</td>
</tr>
</table>
</form>
Here is my UPDATE query:
$sql = "UPDATE fields SET Notes = ('".mysql_real_escape_string(stripslashes($_REQUEST['note']))."')
WHERE companyId='".$companyid['id']."' AND fileNumber ='".$filename."'";
if($result = mysql_query($sql)) {
echo "<h1>Thank you</h1>Your information has been entered into our database<br><br>";
echo $sql;
echo $filename;
} else {
echo "ERROR: ".mysql_error();
}
} else {
echoing $sql produces the following:
UPDATE fields SET Notes = ('asdasda') WHERE companyId='11' AND fileNumber =''
and here is the bit where I instantiate the POST vars.
include "header.php";
$checkFiles = "checkFiles.php";
// Catches form input from previous page and stores it into session variable called filename for future reference;
$_SESSION['filename']=$_POST['filename'];
$filename = $_SESSION['filename'];
//User id stuff from previous page too;
$userid = $_SESSION['userid'];
$id = mysql_query("SELECT id FROM users WHERE DXNumber='".$userid."'");
// Returns pointer so fetch it as an array and insert it into variable $companyid for later use;
$companyid = mysql_fetch_array($id);
You need to include session_start() on the top of each file.
Just do:
AND fileNumber ='".$_SESSION[filename]."'";
In your update query.
If that doesn't work, make sure that a value for $_SESSION[filename] is being set.
<h3>Please enter note for file: <?php echo $filename; ?></h3>
Create a input box
<input type="text" name="filename" value="<?php echo $filename; ?>"/>
Then filename value will be pass to $_POST array

Retrieve data from mysql and update them accordingly

I want to retrieve data from my MySQL database and display it as a table (I don't mind any style, but preferred as a table). Then the data should display with a radio button or a normal button which update that specific row and changing a single column in that row to Active (Status making Active).
I have started it, but I still have issues with adding a radio button or a button. Then I thought of displaying the information in a table which inside a form. This form will have each radio buttons, when the person select that radio button and press submit. The data should be updated in the MySQL database.
Can someone guide me about this? I'm a bit confused. I'm confused because I have to add a form and in that form I have to add a table and all this stuff should be in a php file.
on formpost.php page you can do something like this for handling the list array you received from the previous page:
<?
$list = $_POST['list'];
foreach ($list as $key => $list_php)
{
$query1 = "SELECT * FROM `stu_entry` WHERE `stu_entry`.`Student_No` = '$list_php' AND `stu_entry`.`Out_Time` = '0000-00-00 00:00:00'";
$result1 = mysql_query($query1);
$row1=mysql_fetch_array($result1);
//whatever you want to do
}
?>
I forgot to tell you something in the previous post. In the form page, lets say you have 100 rows with 100 checkboxes. It would be nasty to manually check or uncheck all of them. As an alternative you could append a little script at the end of the page to handle the checking/ unchecking process for you:
<script>
function checkAll(field)
{
for (i = 0; i < field.length; i++)
field[i].checked = true ;
}
function uncheckAll(field)
{
for (i = 0; i < field.length; i++)
field[i].checked = false ;
}
function countChecks(form){
var t=0;
var c=form['list[]'];
for(var i=0;i<c.length;i++){
c[i].checked?t++:null;
}
return t;
}
</script>
<input type="button" name="CheckAll" value="Check All"
onClick="checkAll(document.formpost['list[]'])">
<input type="button" name="UnCheckAll" value="Uncheck All"
onClick="uncheckAll(document.formpost['list[]'])">
<i>with selected: </i>
<select name="submit_mult" id="submit_mult">
<option value="todo1">Action 1 </option>
<option value="todo2">Action 2 </option>
</select>
<input type="button" name="go" value="Go" onClick='if(countChecks(this.form)>0) {if(confirm("are you sure you want to \""+document.getElementById("submit_mult").options[document.getElementById("submit_mult").selectedIndex].text+" "+countChecks(this.form)+" items selected"+"\"")) {document.getElementById("referenceToSomeHiddenElement").value=document.getElementById("submit_mult").options[document.getElementById("submit_mult").selectedIndex].value;document.getElementById("formpost").submit();}} else alert("you have not selected any item!!")' >
Tell me if you need more help on this.
I first specify a MySQL command which in return gives me an array. I use a while statement to do something to each item in the array. The array is populated with the field name and the field contents.
$mysql = mysql_query("SELECT * FROM table");
while ($array = mysql_fetch_array($mysql))
{
$output .= '<form>Row: ' . $array['fieldname'] . ' <input type="button" value="Update" /></form>';
}
This extracts all rows from the table and adds a form to the variable $output for every result. I'm unsure of how you want to update your database, so you will have to look into it. You can use GET and POST methods.
Go ahead and try something like this. And tell me if that worked:
<p><b>your page name</b></p>
<form name="formpost" id="formpost" action="formpost.php" method="POST">
<table border="2" cellpadding="5" cellspacing="1" style="border-collapse: collapse" bordercolor="#999999" width="800">
<tr>
<td><b>Sno.</b></td>
<td><b>Field name1</b></td>
<td><b>Field name2</b></td>
<td><b>Field name3</b></td>
<td><b>Field name4</b></td>
<td><b>Field name5</b></td>
<td><b>Field name6</b></td>
<td><b>Field name7</b></td>
</tr>
<?
$sorting="";
if($orderby!="" && $direction!="") $sorting=" ORDER BY $orderby $direction";
$query = "SELECT * FROM table WHERE something = 'something' $sorting";
$result = mysql_query($query);
?>
<tr>
<?
$i=0;
while ($row = mysql_fetch_array($result))
{
$i++;
?>
<td><input type="checkbox" name="list[<?echo $i?>]" id="list[]" value="<?echo $row['some_primary_field_id']?>"><?echo $i?></td>
<td><?echo $row['Field1']?></td>
<td><?echo $row['Field2']?></td>
<td><?echo $row['Field3']?></td>
<td><?echo $row['Field4']?></td>
<td><?echo $row['Field5']?></td>
<td><?echo $row['Field6']?></td>
<td><p align="center">
<?
echo "<input title='button' name='change this row' value='Some Button' type='button' onClick=\"if(confirm('Press OK to change status to confirm')) {document.getElementById('someid').value='".$row['Field1']."';document.getElementById('formpost').submit();}\"/>";
?>
</td>
</tr>
<input type='hidden' name="hiddenlist[<?echo $i?>]" id="hiddenlist[]" value="<?echo "some boundry condition ".$row['Field2']?>">
<?
} //end of while
?>
</table>
</form>

Categories