How to update Grid on PHP page as per data from MySQL - php

I have MySQL table which is presently displayed using a grid-table on the PHP page. I want the data to be updated:
Where a particular field of a record displayed on PHP is changed.
Whenever a new record is added to the table.
Delete grid-table row when the record is deleted in the MySQL Table.

Step by step method to Update a field:
//First the user will specify what field to Update
<form method="post">
<p>Field to Update:
<input type="text" name="pupdate"></p>
//Next the user will update the field
<p>Updated Name:
<input type="text" name="pname"></p>
<p><input type="submit" value="Add New"/></p>
</form>
<?php
//To connect to database(If you dont understand the next line I can explain it further)
require_once "db.php";
if ( isset($_POST['pupdate']) && isset($_POST['pname']))
{
$up = $_POST['pupdate'];
$u = $_POST['pname'];
//This will update the database with the new field
$sql = "UPDATE NAMEOFTABLE
SET pname='$u'
WHERE pname='$up'";
echo "<pre>\n$sql\n</pre>\n";
mysql_query($sql);
}
?>

Related

Check duplicates name ignore entry

I am using following code to insert entry in my database. But i can add same entry multiple time and i wanna that whenever i submit same entry in input type then message will show "Nickname already exists "
<div>
<form>
<div>name <input type="text" name="na"/></div>
<div>marks1 <input type="text" name="m1"/></div>
<div>marks2<input type="text" name="m2"/></div>
<div>marks3 <input type="text" name="m3"/></div>
<div> <input type="submit" name="save" value="save"/></div>
</form>
</div>
And Here is my php code:-
<?php
if(!empty($_GET['save'])){
$na=$_GET['na'];
$m1=$_GET['m1'];
$m2=$_GET['m2'];
$m3=$_GET['m3'];
$query="insert into student(name,marks1,marks2,marks3) values('$na','$m1','$m2','$m3')";
mysqli_query($connect,$query);
}
?>
Make a unique column in database for example unique name for every student. So when you enter new record first check it that either it is present in the database or not. If its there then you can show error to user
For example you can set a unique constraint like this
Do a select before inserting in your base to check if the username already exists.
For your input message showing that the username already exists you can use a hidden paragraph:<p id="informationText" hidden>This paragraph should be hidden.</p>
Then once your select request has returned that the tuple already exists do an ajax query to show up this paragraph: $('#informationText').removeAttr('hidden');
That's a simple way, there is other better way to do that you can do some research about JavaScript Form Validation
FIRST EDIT
Here is a code sample:
<?php
if( isset( $_GET['save'] ) ){
$na=$_GET['na'];
$m1=$_GET['m1'];
$m2=$_GET['m2'];
$m3=$_GET['m3'];
$query = "select * from student where name = ? ";
$result = mysqli_query( $connect,$query, array( &$na ) );
$row = mysqli_fetch_row( $result );
if (count($row) > 0) {
echo "<script>$('#informationText').text = "Username already exists";$('#informationText').removeAttr('hidden');</script>";
} else {
$query="insert into student(name,marks1,marks2,marks3) values('$na','$m1','$m2','$m3')";
mysqli_query($connect,$query);
}
}
?>

PHP, MYSQL checking if a user is owner of a page

I am trying to check if a user is the owner of a profile page so that i can display a text box for entering twitter similar posts.
The code
$id=mysql_query("SELECT id FROM users WHERE`username`='".$_GET['username']."'");
$ultimatum_form ='';
if(isset($_SESSION['id'])){
if($_SESSION['id']==$id){
$ultimatum_form = 'Write an ultimatum!(220 char max)<br/>
<form action="profile.php" method="post" enctype="multipart/form-data" name="ultimatum_form">
<textarea name="ultimatum_field" rows="3" style="width:97%;"></textarea>
</form>';
}
}
print "$ultimatum_form";
in my DB i have a table called "users", the table users has the columns "firs", "last", "username", "password", "email" and "id".
If i set $ultimatum_form outside of the session check it outputs the text-field and it works. The problem is that if i then go to another persons profile i can see the text-field and write posts for them.
You need to get data from the query:
$id=mysql_query("SELECT id FROM users WHERE`username`='".$_GET['username']."'");
$r = mysql_fetch_assoc($id);
$id = $r['id'];
then $id contains the row "id" value.
look into pdo/mysqli, mysql won't wrok in next php version.
here, better:
$id=mysql_query("SELECT id FROM users WHERE`username`='".$_GET['username']."'");
$r = mysql_fetch_assoc($id);
$id = $r['id'];
if(isset($_SESSION['id']))
{
if($_SESSION['id']==$id)
{
echo '
Write an ultimatum!(220 char max)<br/>
<form action="profile.php" method="post" enctype="multipart/form-data" name="ultimatum_form">
<textarea name="ultimatum_field" rows="3" style="width:97%;"></textarea>
</form>';
}
else
{
// Not users profile page
}
}
You can't exactly do what you're doing. You need to fetch the records.
$results = mysql_fetch_array(mysql_query("SELECT id FROM users WHERE`username`='".$_GET['username']."'"));
Then you can do:
if($_SESSION['id']==$results['id']){

PHP MySQL - How easy way to update the database when one or more fields value changed

I have a little problem on database update activity.
Case study:
I created a form with PHP editing, and perform queries to retrieve the value of a record that wants to be updated. Excerpts of the script:
<?php
$row = mysql_fetch_assoc(mysql_query("SELECT id, field_1, field_2 FROM mytable WHERE id = $editid"));
?>
...
<form action="" method="post">
FIELD 1 <input type = "text" name = "f1v" value = "<? Php echo $ row ['field_1'];?>" />
FIELD 2 <input type = "text" name = "f2v" value = "<? Php echo $ row ['field_2'];?>" />
<input type="submit" />
</form>
....
// When the form posted
if ($_POST)
{
$f1v = $ _POST['f1v'];
$f2v = $ _POST['f2v'];
mysql_query("UPDATE mytable SET field_1 = '$f1v', field_2 = '$f2v' WHERE id = $editid") or die ();
// Redirect form
}
In this case I want when the form submited, there are activities to check whether there is a change in one or more fields values. Its logic approximately like this:
if ($ _POST)
{
// Compare
if the submitted value is different from the existing value in the record
{
Updated record
}
else
{
Do not update record
}
// Redirect form
}
Do you have any easy way to do it? Thank you for your help.
Don't bother checking. Just make sure the entry is valid and throw it in.
Keep two hidden fields with current values of the fields. After submitting the form check whether submitted values are different from the hidden field values.

Selecting ID in while statement PHP

I'm attempting to make a function that will allow users to delete posts they have left. Basically I'm trying to select an ID and then pass it off to another function to delete it. The current code is deleting every comment, not just the comment linked to the ID like I'm attempting to.
Here are the table values
Table eventUpdates
ID - unique ID. Trying to use this value to delete the post
eventID - references an eventID from another table
eventReply - the "message" or response
eventUserID -ID of the user posting the message
username -username of the person
eventTimestamp -timestamp
So, currently every response is being deleted. I can't get it to delete just the ID that I send.
<table border ="1">
<?
$eventUpdates = mysql_query($sql);
while ($list = mysql_fetch_assoc($eventUpdates)) {
echo $updateID;
echo '<tr><td>';
echo $list['username'],"</br>", $list['eventTimestamp'],'<br/>';
if($list['eventUserID'] == $userid){ //used for deleting posts. Checks the session to make sure it's the user who made the post
?>
<form method="post">
<input type="submit" name="deleteUpdate" value="Delete Update">
</form>
<?
if(isset($_POST['deleteUpdate'])){
$updateID = $list['ID'];
$delete = new postprocessing;
$delete->deleteEventUpdate($updateID);
echo '<meta http-equiv="refresh" content="0;url=main.php">';
}
}
echo '</td><td>';
echo $list['eventReply'];
echo '</td></tr>';
}
Here is the function used to delete it
function deleteEventUpdate($ID){
mysql_query("delete from eventUpdates where ID = '$ID'");
}
This is because you haven't specified the ID you want to delete in the post array, so every comment they created "matches" the deletion criteria. The $_POST array looks like:
$_POST = array('deleteUpdate'='Delete Update');
so when you're looping through the list of events, every comment that the user 'owns' ($list['eventUserID'] == $userid) matches the criteria "isset($_POST['deleteUpdate'])":
You need to include a hidden input with the ID the specific comment you want to delete and match against it as well:
<form method="post">
<input type="submit" name="deleteUpdate" value="Delete Update">
<input name='post_id' value="<?php echo $list['ID'];?>" type='hidden' />
</form>
and then in the loop:
if(isset($_POST['deleteUpdate']) && isset($_POST['post_id']) && $_POST['post_id']==$list['ID']){...

update database by text box based on the checked box's

**Hi
i have been working on this php code .
I want to update the quantity that belongs to the checked box .
but the exact problem with my code is :if I have for example 3 checkboxs and when I enter the quantity for them all in the same time the updates are done correctly and if I only update the first quantity with out the other 2 the update is done correctly also ,But when I update the second or the third alone not in the same time it takes the quantity of the old value of the first :""(
so how can I change my code so I can update all the items or only the checked items.
here is my code to display the checkbox's with the quantity.
in the file manage_items.php :**
<?php
$DB_HOST ='localhost';
$DB_USER ='root';
$DB_PASSWORD='';
$DB_DATABASE='dks';
$con= mysql_connect($DB_HOST ,$DB_USER , $DB_PASSWORD);
if(!$con){
die('Failed to connect to server :'.mysql_error());
}
$db=mysql_select_db($DB_DATABASE);
if(!$db){
die("unable to select database");
}?>
$qry="SELECT * FROM catalog";
$result= mysql_query($qry);
if($result){
while($info = mysql_fetch_array($result))
{
print "<h3> cat:".$info['name']."</h3><div>";
$qryitem="SELECT * FROM item WHERE Id=". $info['Cid'];
$resultitem=mysql_query($qryitem);
if($resultitem){
?>
<form method="post" action="manage_item_action.php">
<?php
while($info=mysql_fetch_array($resultitem))
{
?>
<input type="checkbox" name="op[]" value="<?php echo $info['Id'];?>"/><?php echo $info['name'];?>
<label> Quantity <input type="text" name="Quantity[]" value="<?php echo $info['Quantity'];?>"/></label>
<br/>
<?php
}
}
else echo "There are no items.";
print "</div>";
}
}
?>
</div>
<input type="submit" value="update" name="submit"/>
</form>
and here is the excution of update
in the file manage_item_action.php
<?php
$DB_HOST ='localhost';
$DB_USER ='root';
$DB_PASSWORD='';
$DB_DATABASE='dks';
$con= mysql_connect($DB_HOST ,$DB_USER , $DB_PASSWORD);
if(!$con){
die('Failed to connect to server :'.mysql_error());
}
$db=mysql_select_db($DB_DATABASE);
if(!$db){
die("unable to select database");
}
$options=$_POST['op'];
$qun=$_POST['Quantity'];
$size =count($options);
for($i =0; $i<$size; $i++)
{
//$qryop="UPDATE item SET Quantity =".$qun."WHERE Id =".$options[$i]."';";
$resultop=mysql_query("update item set Quantity='".$qun[i]."'where Id='".$options[$i]."'");
}
if($resultop){
header("location: manage_items.php");}
else echo "there was an error"
?>
i dunno how to fix the problem !
but i think my main problem is that all the textbox's have the same name .
so when i send it to the other file for ecution it just take it as it is the last one .
help me please :$
*UPDATED*
*The Solution*
so I solved the problem of my code and its only needed the textbox to be disabled before checkin the checkbox so the array doesn't have any null index..only have the entered values without any nulls
:))
You need to give the 'Quantity' input field a unique name or cast it as an array for every checkbox listed, else when submitted the receiving form will take the last value for 'Quantity' as gospel.
That's because in your input form box, you have:
Quantity <input type="text" name="Quantity" ...
So, with each of the quantity inserted, the forwarded values are being updated. And the last value inserted into Quantity box will be taken in update. Instead, change this form input to this:
<!-- This will forward an array to php script -->
Quantity <input type="text" name="Quantity[]"...
and then, in your updation query, change to:
$resultop=mysql_query("UPDATE item SET Quantity='" . $qun[$i] . "'where Id='".$options[$i]."'");

Categories