Posting inside the page in another form action? - php

I am currenty having a thesis project which is the voting system, and my problem is how can I count the vote if I click on the radio button after I click the next button how to updates the votes?. And guys is it possible that in my php page I can save my data but the form action is not the saving part I mean like this
I have a form codes:
//preview page is the page where I can see all tha candidates that I have choose.
<form id="msform" action="preview.php" method="POST">
<?php
$resultasa = $db->prepare("SELECT * FROM candposition");
$resultasa->execute();
for($i=0; $rowasa = $resultasa->fetch(); $i++){
$exrxrxrx=$rowasa['pos_name'];
if($exrxrxrx=='President'){
?>
<fieldset>
<table>
<tr><td><h2 class="fs-title"><?php echo $rowasa['pos_name'] ?></h2></td>
<td><input name="<?php echo $rowasa['pos_name'] ?>" type="hidden" value="0" /></td>
</tr>
<?php
$YearNow=Date('Y');
$dsds=$rowasa['posid'];
$results = $db->prepare("SELECT * FROM candidates,student,school_year,partylist where student.idno = candidates.idno AND school_year.syearid = candidates.syearid AND posid =:a AND candidates.partyid = partylist.partyid AND school_year.from_year like $YearNow ");
$results->bindParam(':a', $dsds);
$results->execute();
for($i=0; $rows = $results->fetch(); $i++){
?>
<td><img src="admin/candidates/images/<?php echo $rows['image']; ?>" height="160px;" width="155px;"></td>
<td><br>
<div style="text-align:center">
<input name="pres" style="margin-left:-19px;" type="radio" value="<?php echo $rows['candid'] . "-" ."&nbsp". $rows['lastname']. "&nbsp". $rows['firstname'] ?>"> <?php echo $rows['lastname'] ?>,
<?php echo $rows['firstname'] ?>
- <?php
echo $rows['party_name']?>
<?php
//I think this part will be having an update like
$sql = "UPDATE candidates
SET votes=votes+?
WHERE candid=? ";
$q = $db->prepare($sql);
}
?>
<?php
}
?>

Related

Make AJAX on add and remove items to avoid reload

I have 3 php files in doing addition and deletion of medicine details. medicines detail are used to order drugs for each patient who comes for treatment. In order not to be a hassle, how do I prevent my application from needing to be reloaded with AJAX? The 3 files are: services.php , insertDetailMedicines.php and deleteDetail.php .
services.php
$data = mysqli_query($conn, "SELECT MAX(id_service) AS ids FROM tbl_services");
$final_data = mysqli_fetch_array($data);
$id1 = $final_data['ids'];
$id2 = substr($id1,3,3); //MR for Medical Record
$id3 = $id2 + 1;
$id4 = 'MR'.sprintf('%03s' , $id3); // <-- Auto generating unique id
<form method="POST" action="services.php">
<input type="hidden" name="id_medical_record" value="<?php echo $id4 ?>">
<select name="medicineName" id="medicineName" required>
<option value="">- Choose -</option>
<?php
$medicines = mysqli_query($conn, "SELECT * FROM tbl_medicines ORDER BY id_medicines ASC");
$m = mysqli_fetch_array($medicines);
while($m = mysqli_fetch_array($medicines)){ ?>
<option value="<?php echo $m['id_medicine'] ?>">
<?php echo $m['medicine_name'] ?>
</option>
<?php } ?>
</select>
<input type="text" name="qty_medicines" id="qty_medicines" value="1" required />
<button type="submit" name="add" style="cursor: pointer">ADD</button>
</form>
<table> <!--this is to display the drugs that have been added-->
<?php
$show_details = mysqli_query($conn, "SELECT * FROM tbl_detail_medicines LEFT JOIN tbl_medicines USING (id_medicine)");
$num = 1;
if(mysqli_num_rows($show_details) > 0)
{
while ($detail = mysqli_fetch_array($show_details))
{
?>
<tr>
<td>
<?php echo $num++.'.'; ?>
</td>
<td>
<?php echo $detail['medicine_name'] ?>
</td>
<td>
<?php echo $detail['qty'] ?>
</td>
<td>
<a href="deleteDetail.php?delete=<?php echo $detail['id'] ?>">
<b> X </b>
</a>
</td>
</tr>
<?php
}}
?>
</table>
insertDetailMedicines.php
<?php
if (isset($_POST['add'])) {
$idMR = $_POST['id_medical_record'];
$medicineName = $_POST['medicineName'];
$qty_medicines = $_POST['qty_medicines'];
$insert_detail = "insert into tbl_detail_medicines (id,id_service,id_medicine,qty)
VALUES
(null,'$idMR','$medicineName','$qty_medicines')";
if (mysqli_query($conn,$insert_detail)) {
//echo "inserting success!";
}
}
?>
deleteDetail.php
<?php
require 'koneksi.php';
if(isset($_GET['delete'])){$delete = mysqli_query($conn, "DELETE FROM tbl_detail_medicines WHERE id = '".$_GET['delete']."' ");
header('location:services.php');
}
?>
apppearance

IMPLODE data by checkbox

i have this Interface where the user can add a flower
The user need to add a flower to the database and he can make them in different occasions a categories (the check boxes). Now to implode the data i made this code:
$occasions = implode( ';' , $_POST['reg_occassions'] );
$categories= implode( ';' , $_POST['reg_categories'] );
$query =
"INSERT INTO tbl_flower (flower_type, website_description,florist_description,name,image,enabled,florist_choice,categories,occasions)
VALUES ('$flowertype', '$websitedescription', '$floristdescription','$name', '$upfile', '$enabled','$floristchoice', '$categories','$occasions')";
In the database they save as follows :
id flower name occasions categories
1 Rose Birthday;Valentines Bouqet;flower arrangment
Now the user can edit a flower too.This is the interface
PROBLEM!:
i dont have an idea how can i make the checkboxes again and show him which one he checked and he can change the data. I need to know how can i show the checkboxes and which one he checked will be checked too .
Thank and I really appreaciate if someone can help me.
EDIT Answer: This is the interface of the checkboxes.
You can do something like:
$results = "Bouqet,flower arrangment"; //results from your database
$resultsArray = explode(",", $results); //split the comma
$checkValue = array("Bouqet", "flower arrangment", "other"); //your checkbox values
$html = "";
foreach($checkValue as $val)
{
if(in_array($val,$resultsArray ))
$html .= '<input type="checkbox" name="flower" value="'.$val.'" checked>'.$val.'<br>';
else
$html .= '<input type="checkbox" name="flower" value="'.$val.'">'.$val.'<br>';
}
echo $html;
Check Demo Here
EDIT: I am making this edit because of your comments, you need to do something like:(not tested)
<div class="table-responsive col-md-4">
<table>
<thead>Occasions</thead>
/**flower occassions **/
$flowerCategoriesQry "SELECT categories FROM tbl_flower where id = 1"; //you need to change the where clause to variable
$flowerResults = mysqli_query($conn, $flowerCategoriesQry)
$categoriesRow = $flowerResults->fetch_assoc();
$resultsArray = explode(",", $categoriesRow['categories']);
/**All Occassions **/
$query544="SELECT name FROM tbl_occasions";
$results544 = mysqli_query($conn, $query544);
while ($row = $results544->fetch_assoc()) { ?>
<div class="col-md-12">
<label class="checkbox" for="checkboxes">
<?php
if(in_array($row['name'],$resultsArray ))
{
?>
<input type="checkbox" name="flower" value="<?php echo $row['name'];?>" checked> <?php echo $row['name'];?> >
<?php }
else{
?>
<input type="checkbox" name="flower" value="<?php echo $row['name'];?>" ><?php echo $row['name'];?> >
<?php echo} $row['name']?> </label>
</div> <?php }?>
</table>
This is the answer. Thanks to the Flash!
session_start();
$conn = ConnectToSql();
?>
<div class="table-responsive col-md-6" style="border:1px solid blue">
<div class="col-md-6">Categories</div>
<div class="col-md-6">Occasions</div>
<hr>
<div class="col-md-6">
<?php
/**flower occassions **/
$flowerCategoriesQry= "SELECT categories FROM tbl_flower where id ='$_SESSION[flower]'";
$flowerResults = mysqli_query($conn, $flowerCategoriesQry) ;
$categoriesRow = $flowerResults->fetch_assoc();
$resultsArray = explode(";", $categoriesRow['categories']);
/**All Occassions **/
$query544="SELECT name FROM tbl_categories";
$results544 = mysqli_query($conn, $query544);
while ($row = $results544->fetch_assoc()) { ?>
<label class="checkbox" for="checkboxes">
<?php
if(in_array($row['name'],$resultsArray ))
{
?>
<input type="checkbox" name="edit_categories[]" value="<?php echo $row['name'];?>" checked> <?php echo $row['name'];?>
<?php
}
else{
?>
<input type="checkbox" name="edit_categories[]" value="<?php echo $row['name'];?>" ><?php echo $row['name']; } ?>
</label>
<?php
}
?>
</div>
click here for code

Adding new record to database prevents echoing other records

Hi i have created a forum website where logged in users can create topics using a form on each forum topic, however when I add a new record using the form it prevents itself and the previous topics from being displayed.
I have no idea why and would be greatful for any help.
I have my forum page which displays categories, topics(if category is selected) and replies (if topic is selected). Here is the code for my forum.php page
<div id="midclmn">
<?php
If (isset($_GET['topic'])){
// Show topic & replies
$queryreply = "SELECT a.reply_id,a.reply_text, a.reply_date, b.topic_title, b.topic_date,c.username AS reply_user, (SELECT username FROM users
WHERE user_id=b.user_id) AS topic_creator FROM forum_replies a
LEFT JOIN forum_topics b ON a.topic_id=b.topic_id
LEFT JOIN users c ON a.user_id=c.user_id
WHERE a.topic_id = '".$_GET['topic']."' ";
$result = mysql_query($queryreply) or die (mysql_error());
$row = mysql_fetch_array($result);
if(empty($row['reply_id'])){
echo "No replies have been posted in this Topic, be the first to have your say using form below.";} ?>
<table id="categorytable">
<tr><td><?php echo '<b>'.$row['topic_title'].'</b>';?></b><br></td></tr>
<tr><td><?php echo 'Topic published by '.$row['topic_creator'].' - ( '.$row['topic_date'].' )'.'';?><br><br></td></tr>
<tr><td><?php $row['reply_user'].' Replied with: ';?><br></td></tr>
<tr><td><?php echo $row['reply_text'].'<br><br><i>Published: '.$row['reply_date'].' by '.$row['reply_user'].'</i>';?></td></tr>
<?php
while ($row = mysql_fetch_array($result)){ ?>
<tr><td><?php $row['reply_user'].' Replied with: ';?><br></td></tr>
<tr><td><?php echo $row['reply_text'].'<br><br><i>Published: '.$row['reply_date'].' by '.$row['reply_user'].'</i>';?></td></tr>
<?php
}
?>
</table>
<?php
}elseif (isset($_GET['cat'])){
// Show topics in that category
$querytopic = "SELECT topic_id, topic_title,topic_description, topic_date, category_id FROM forum_topics WHERE category_id = '".$_GET['cat']."'";
$result = mysql_query($querytopic) or die (mysql_error());
$row = mysql_fetch_array($result);
if(empty($row['topic_id'])){
echo "No topics have been posted in this category, be the first to submit a topic using form below."; ?>
<table id="categorytable">
<tr><td><?php echo '<b>'.$row['topic_title'].'</b>';?></td></tr>
<tr><td><?php echo $row['topic_description'];?><br><br></td></tr>
<tr><td><?php echo "<b>Date Posted:</b> ".$row['topic_date'];?><br><br></td></tr>
<?php
while ($row = mysql_fetch_array($result)){ ?>
<tr><td><?php echo '<br>'.$row['topic_title'].'</b>';?></td></tr>
<tr><td><?php echo $row['topic_description'];?><br><br></td></tr>
<tr><td><?php echo "<b>Date Posted:</b> ".$row['topic_date'];?><br><br></td></tr>
<?php
}} ?></table><?php
if($_SESSION['loggedin'] === true){
$userid = $_SESSION['id'];
$catid = $_GET['cat'];
?>
<br>
<form method="post" action="topic_process.php" id="topicform">
<h3>Add New Topic</h3>
<input type="hidden" id="catid" name="catid" value=<?php echo $catid?> >
<input type="hidden" id="userid" name="userid" value=<?php echo $userid ?>>
<label for="topictitle">Topic Title: </label>
<textarea rows="2" cols="80" id="topictitle" name="topictitle" required ></textarea>
<br><br>
<label for="topicdescription">Topic Description: </label>
<textarea rows="10" cols="80" id="topicdescription" name="topicdescription" required ></textarea>
<input type= "hidden" id="topicdate" name="topicdate" value=<?php echo ''.date('Y-m-d').'' ?>>
<br><br>
<input type="submit" name="topicsubmit" id="topicsubmit" value="Create Topic">
</form>
<?php
}
?>
<?php
}else{
//Just display the list of categories
$querycategory = "SELECT category_id, category_title,category_description FROM forum_category";
$result = mysql_query($querycategory) or die (mysql_error());
$row = mysql_fetch_array($result); ?>
<table id="categorytable">
<tr><td><?php echo ''.$row['category_title'].'';?></td></tr>
<tr><td><?php echo $row['category_description'];?><br><br></td></tr>
<?php
while ($row = mysql_fetch_array($result)){ ?>
<tr><td><?php echo ''.$row['category_title'].'';?></td></tr>
<tr><td><?php echo $row['category_description'];?><br><br></td></tr>
<?php
}
?>
</table>
<?php }
?>
</div>
and here is the process page
session_start();
include "includes/connection.php";
echo $_POST['topictitle'];
echo $_POST['topicdescription'];
echo $_POST['userid'];
echo $_POST['catid'];
$query = "INSERT INTO forum_topics
(
category_id,
user_id,
topic_title,
topic_description,
topic_date
)
VALUES
(
'".$_POST['catid']."',
'".$_POST['userid']."',
'".$_POST['topictitle']."',
'".$_POST['topicdescription']."',
'".$_POST['topicdate']."'
)";
mysql_query($query) or die (mysql_error());
header('Location: /TEST/forum.php?'.$_POST['catid'].'');
?>
I am also struggling to record the topic_date as today's date I tried date('Y-m-d') for 2015-04-28 but it saved as 00-00-00.
I know its a lot of code to read but I'm really stuck and don't want to miss anything, also I know mysql_functions are deprecated but have been asked to use them by uni.
Im using PHPmyadmin as the database, which I know is rubbish!
Thanks
EDIT: I have set up the page so It displays all the topics stored in the database (topic_title, topic_description, user_id, topic_date). before I add a new topic to the database this works fine but afterwards all the topics including the new one disappear leaving just the form on the page. All the records still exist on the database and deleting the new record brings back all the topics displayed on the page.

php mysql checkbox and textboxes values insert into database

I have a db table called 'ice_flavours' with the columns: 'id', 'flavours', 'date', 'selected'.
and another table called 'ice_today' like: 'id', 'flavours', 'date', 'selected'.
Now, on the left of the page I want to call from 'ice_flavours' and print the entries in a form together with a checkbox.
On the right of the page it shall say SHOWCASE where I want the todays selected flavours to be shown and they shall just stay in there until the end of today´s date and then automatically be discarded. Also when they were selected to show in SHOWCASE, i want them to not show on the left side.
So, when I select the checkbox next to a flavour, the values 'id', 'flavours', 'date', 'selected' ought to be entered into 'ice_today' but for some reason just always the last row of the ice_flavours table is entered and i get the message "Duplicate entry '0' for key 'PRIMARY'"
Maybe this can all be done by just using a single db table but I havent figured it yet. Can someone help please
edit.php:
<?php ob_start();
include_once("dbinfo.inc.php");
include_once("config.php");
if(isset($_POST['submit'])){
$selected = $_POST['selected'];
$id = $_POST['id'];
$flavour = $_POST['flavour'];
$date = $_POST['date'];
if($_POST["submit"] == "submit") {
for($i=0;$i<sizeof($selected);$i++) {
if(!empty($flavour)) {
echo"";
echo "<pre>";
print_r($_POST);
echo "</pre>";
$query="INSERT INTO ice_today VALUES('$id','$flavour','$date','$selected[$i]')";
mysql_query($query) or die(mysql_error());
}
}
echo "Entry added";
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Edit</title>
</head>
<body>
<form name="form1" id="form1" method="POST" action="edit.php">
<table border="1" width="200px">
<td>
<table border="1" width="200px">
<?php
$result = mysql_query("SELECT * FROM ice_flavours WHERE ('selected' != '1') ORDER BY flavour ASC");
$results = mysql_num_rows($result);
if ($results > 0)
{
$num=mysql_numrows($result);
$i=0;
while ($i < $num)
{
$id=mysql_result($result,$i,"id");
$flavour=mysql_result($result,$i,"flavour");
?>
<tr>
<td align="center" valign="middle">
<?php echo $flavour; ?>
<input type="text" name="id" id="id" value="<?php echo $id; ?>">
<input type="text" name="flavour" id="flavour" value="<?php echo $flavour; ?>">
<input type="text" name="datum" id="datum" value="<?php echo date('Y-m-d'); ?>">
<input type="checkbox" name="selected[]" id="selected" value="<?php echo '1'; ?>">
</td></tr>
<?php
$i++;
}
?>
<input type="submit" value="submit">
<?php } ?>
</td>
<td>
<table border="1" width="200px">
<tr><td align="center" valign="middle">
SHOWCASE
</td></tr>
<?php
include_once("dbinfo.inc.php");
include_once("config.php");
$result2 = mysql_query("SELECT * FROM ice_today ORDER BY flavour ASC");
$results2 = mysql_num_rows($result2);
if ($results2 > 0)
{
$num2=mysql_numrows($result2);
$j=0;
while ($j < $num2)
{
$id=mysql_result($result2,$j,"id");
$flavour=mysql_result($result2,$j,"flavour");
?>
<tr><td align="center" valign="middle">
<?php echo $flavour; ?>
</td></tr>
<?php
$j++;
}
}
echo '</td></tr></table>';
?>
</body>
</html>
<?php ob_end_flush(); ?>
Please take a look at the MySQL Reference Manual. You need to specify each column in your INSERT statement:
$query="INSERT INTO ice_today(id, flavours, date, selected) VALUES('$id','$flavour','$date','$selected[$i]')";
I also suggest you to not use the original MySQL-extension anymore, as it is deprecated as of PHP 5.5.x. When using another MySQL extension (MySQLi or PDO for instance: mysqli or PDO - what are the pros and cons?) you can also use prepared statements to become safe against SQL injections (which you aren't right now).
POST-values can be manipulated and as you just feed them into your query via string concatenation, you're not safe against them.
If the field for ID is an autoincrement field, you need to leave it off the insert because the db server will determine the value itself. You should list the fields being inserted, and leave that one off:
$query="INSERT INTO ice_today (flavours, date, selected) VALUES('$flavour','$date','$selected[$i]')";
Listing the fields you are inserting to also prevents insert statements from breaking when you add new fields later. So its good practice to start now.
There are lots of issues in your code.
You text boxes having same name with different values if there are two flavors.
Therefore flavor or id will be always from last row.
You can do something like this.
1.Replace your text boxes with this,
<input type="text" name="id_<?php echo $id; ?>" value="<?php echo $id; ?>">
<input type="text" name="flavour_<?php echo $id; ?>" value="<?php echo $flavour; ?>">
<input type="text" name="datum_<?php echo $id; ?>" value="<?php echo date('Y-m-d'); ?>">
<input type="checkbox" name="selected[]" value="<?php echo $id; ?>">
2.Replace your top if block (i.e. after submit)
if (isset($_POST['submit']) && $_POST["submit"] == "submit") {
$selected = $_POST['selected'];
for($i=0; $i < sizeof($selected); $i++) {
$id = $selected[$i];
$flavour = $_POST['flavour_' . $id];
$date = $_POST['date_' . $id];
$query="INSERT INTO ice_today VALUES('$id','$flavour','$date','$id')";
mysql_query($query) or die(mysql_error());
}
}

How to read/send post data with php and hold a variable in it

I have this code in a loop in my code, The loop makes one submit button for every member found. I need each button to have the members name stored in it, in a way it can be sent though post when that button is clicked. Im not sure if this is possible with post but i was trying a way i do it with URLS. Does anyone know how to do this?
<input type="submit" value="Attack" name="Attack?name=<?php echo $Member_name; ?>" />
<?php
if(isset($_POST['Attack'])){
$sql = "SELECT * FROM users WHERE name='".mysql_real_escape_string($_GET['name'])."'";
$query = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_object($query);
}
Here is the whole code i was trying to store it in a hidden form but it only grabs the last member found and wont get others.
<?php
$sql = "SELECT name, rank FROM users ORDER BY rank DESC"; // Searches the database for every one who has being last active in the last 5 minute
$query = mysql_query($sql) or die(mysql_error());
$count = mysql_num_rows($query);
$i = 1;
while($row = mysql_fetch_object($query)) {
$Member_name = htmlspecialchars($row->name);
$Member_level = htmlspecialchars($row->rank);
?>
<td><?php echo $i; ?></td>
<td><?php echo $Member_name; ?></td><td><?php echo $Member_level; ?></td><td>
<input type="hidden" name="thename" value="<?php echo $Member_name; ?>">
<input type="submit" value="Attack" name="Attack" />
</td>
<?
if($i != $count) { // this counts the amount of people that are online and display the results.
echo "</tr><tr>";
}
$i++;
}
?>
<?php
if(isset($_POST['Attack'])){
$sql = "SELECT * FROM users WHERE name='".mysql_real_escape_string($_POST['thename'])."'";
$query = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_object($query);
$profile_id = htmlspecialchars($row->id);
$profile_userip = htmlspecialchars($row->userip);
$profile_name = htmlspecialchars($row->name);
$profile_money = htmlspecialchars($row->money);
$profile_gang = htmlspecialchars($row->gang);
$profile_exp = htmlspecialchars($row->exp);
$profile_profile = htmlspecialchars($row->profile);
$profile_rank = htmlspecialchars($row->rank);
$profile_health = htmlspecialchars($row->health);
$profile_defence = htmlspecialchars($row->defence);
$profile_stanima = htmlspecialchars($row->stanima);
?>
OK, assuming everything else is working ok, and you are retrieving data.
Change this:
<input type="hidden" name="thename" value="<?php echo $Member_name; ?>">
<input type="submit" value="Attack" name="Attack" />
To this:
<form method="POST" action="">
<input type="hidden" name="name" value="<?php echo $Member_name; ?>">
<input type="submit" value="Attack" name="Attack" />
</form>
And also in your PHP, change this line:
$sql = "SELECT * FROM users WHERE name='".mysql_real_escape_string($_GET['name'])."'";
To:
$sql = "SELECT * FROM users WHERE name='".mysql_real_escape_string($_POST ['name'])."'";
This isn't the best way to do this, you will be generating loads of HTML elements depending how many users you have, but it should solve you problem (providing everything else is working and receiving data).
HTML 5 & Javascript would be perfect for this and is something you should look into.

Categories