php increment field value in mysql - php

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<ul>
<input type="radio" name="team" id="1" value="Real Madrid" size='5'><font size='5'><u> Real Madrid<br>
<input type="radio" name="team" id="2" value="Chelsea"> Chelsea<br>
<input type="radio" name="team" id="3" value="Milan"> Milan<br>
<input type="button" id="submit" name="submit" value="VOTE" >
</form>
this is my button in html
if(isset($_POST['submit'])){
$selected_radio = $_POST['id'];
$query = "UPDATE favourite_team SET likes = likes + 1 WHERE id = '" . $selected_radio . "'";
$q = mysqli_query($conn, $query);}
this is my code in php

Name is 'team'. Not, 'id'. Use this code. Give id value to value attribute
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<ul>
<input type="radio" name="team" value="1" size='5'><font size='5'><u> Real Madrid<br>
<input type="radio" name="team" value="2" > Chelsea<br>
<input type="radio" name="team" value="3" > Milan<br>
<input type="button" id="submit" name="submit" value="VOTE" >
</form>
<?
if(isset($_POST['submit'])){
$selected_radio = $_POST['team'];
$query = "UPDATE favourite_team SET likes = likes + 1 WHERE id = '" . $selected_radio . "'";
$q = mysqli_query($conn, $query);}
?>

You need to write $_POST['team']. Try this code:-
$selected_radio = $_POST['team'];
$query = "UPDATE favourite_team SET likes = likes + 1 WHERE id = '" . $selected_radio . "'";
$q = mysqli_query($conn, $query);
Check this link for more detail.

you need to change the value of the radio to be the id this mean value='1'
and need to change
$selected_radio = $_POST['id']; to
$selected_radio = $_POST['team'];
and change input type to submit type='submit'
<input type='submit' id='submit' name='submit' value='VOTE'>
i hope this will fix your problem

Related

How i can get value from radio button form in php?

Question.php
<?php
include 'Pre-function.php'
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="CSS/Start.css">
</head>
<body>
<div class="nav">
Home
News
Contact
</div>
<div class="question">
<div class="A4">
<form action="Answer.php" method="post">
<?php getQuestion($conn); ?>
<input type="submit" name="Submit" value="Submit">
</form>
</div>
</div>
</body>
</html>
Its html page to ask question
Pre-function.php
<?php
include 'conn.php';
function getQuestion($conn) {
$query = "SELECT * FROM question ";
$result = mysqli_query($conn, $query);
if($result){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$question_id = $row['question_id'];
$question_body = $row['question_body'];
$option_a = $row['option_a'];
$option_b = $row['option_b'];
echo '
<h2 class="qtitle">'.$question_body.'</h2>
<label for='.$question_body.'>Yes</label>
<input type="radio" name="'.$question_id.'" value="Yes">
<input type="hidden" name="option_a" value="'.$option_a.'">
<label for="'.$question_body.'">No</label>
<input type="radio" name="'.$question_id.'" value="No">
<input type="hidden" name="option_b" value="'.$option_b.'">
<input type="hidden" name="submitted" value="submitted"><hr>';
}
}
}
?>
Basically this form asked question whether yes or no using radio button. $option_a == 'Yes' and $option_b == 'No'. The question is like this "Are you have fever ?". So when i submit the value did not pass to the 'Answer.php' page.
'Answer.php' page.
<?php
include 'conn.php';
if(isset($_POST['Submit']) && !empty($_POST['Submit'])){
echo $_POST['option_a'];
echo 'succeed';
}
else{
echo 'no data';
}
?>
In this page have error undefined_index value but still echo succeed.
Your HTML code should look like the following:
<input type="radio" name="whatevername" value="Yes">
<input type="hidden" name="whatevername" value="No">
You can use PHP to insert whatever values you want but you need the radio button name to be the same.
Then if you want to echo that in PHP you'd use:
echo $_POST['whatevername']; //same name you used in the form
You are passing value as name for radio buttons
name="'.$option_a.'"
This will result you in name ="Yes"
And you are trying to fetch echo $_POST['option_a']; where option_a is not defined.
Try this
<input type="radio" name="'.$question_id.'" value="Yes">
<input type="hidden" name="option_a" value="'.$option_a.'">
Same for other radio button
Try this one :
<?php
include 'conn.php';
function getQuestion($conn) {
$query = "SELECT * FROM question ";
$result = mysqli_query($conn, $query);
if($result){
echo '<div class="A4">';
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$question_id = $row['question_id'];
$question_body = $row['question_body'];
$option_a = $row['option_a'];
$option_b = $row['option_b'];
echo '
<div class="A4">
<h2 class="qtitle">'.$question_body.'</h2>
<form action="Answer.php" method="post">
<label for='.$question_body.'>Yes</label>
<input type="radio" name="radioQuestions[]" value="'.$question_id.'-'.$option_a.'">
<label for="'.$question_body.'">No</label>
<input type="radio" name="radioQuestions[]" value="'.$question_id.'-'.$option_b.'">
<input type="hidden" name="submitted" value="submitted"><hr>';
}
echo'
<input type="submit" name="Submit" value="Submit">
</form>
</div>';
}
}
?>
<?php
include 'conn.php';
if(isset($_POST['submitted']) && !empty($_POST['submitted'])){
$questionAndOptions = $_POST['radioQuestions'];
foreach ($questionAndOptions as $questionAndOption) {
$arrQuestionAndOption = explode("-", $questionAndOption);
echo $arrQuestionAndOption[0]; //question
echo $arrQuestionAndOption[1]; //option
}
echo 'succeed';
}
else{
echo 'no data';
}
?>

post pre checked checkbox on form submit PHP MYSQL

how can i submit a pre checked checkbox on submit?
right now i have
$checked= 'checked="checked"';
<input type="checkbox" name="entity_id['.$row['entity_id'].']" value="Yes" '.$checked.'>
on submit, this is not getting posted, only when checked/unchecked by hand.
my $checked is actually coming from a for-each mysql query.
any help is appreciated.
UPDATE
<form method="post">';
if($hass_lights == 'Yes'){$user->getLights($userid);}
exit('
<br><b>Google is requiring access to your basic profile information.</b><br><br>
<input type="submit" class="btn btn-text" style="height:40px; width:100px" name="authorized" value="Allow" /> <input type="submit" class="btn btn-text" style="height:40px; width:100px" name="authorized" value="Deny" />
</form>
');
function getLights($userID){
$stmt = $this->db->prepare("SELECT * FROM hass_entities WHERE id = :id AND devicetype= 'light' ORDER BY friendly_name ASC");
$stmt->bindParam(':id',$userID);
$stmt->execute();
$userData = $stmt->fetchAll();
echo '
<div class="container">
<button type="button" class="btn btn-info" data-toggle="collapse" style="width:120px" data-target="#lights">Lights</button>
<br/><div id="lights" class="collapse"><br/><table border=0>';
foreach( $userData as $row ) {
if($row['enabled'] == 'Yes'){
$checked = 'checked="checked"';
}else{
$checked = '';
}
echo '<tr><td><label id="'.$row['entity_id'].'">'.$row['friendly_name'].' </label></td><td><input type="checkbox" name="entity_id['.$row['entity_id'].']" value="Yes" '.$checked.'></td></tr>';
}
echo '</table></div></div><br/>';
}
-Dennis
Try this:
<input type="checkbox" name="entity_id['.$row['entity_id'].']" value="Yes" checked>
Your input field is been written in an echo statement? Would be good to see the entire form, but here is your code working:
<form method=POST>
<?php
var_dump($_POST);
$row = [];
$row['entity_id'] = 1;
$checked= 'checked="checked"';
echo '<input type="checkbox" name="entity_id['.$row['entity_id'].']" value="Yes" '.$checked.'>';
?>
<button type="submit">submit</button>
</form>

Displaying mysql data through hidden field values

I am trying to display mysql records through hidden field values, but nothing displays.
A little help!
Here's the code;
Html:
<form name="form11" method="post" action="hpdata.php" enctype="multipart/form-data">
<input name="pro" id="pro" type="hidden" value= "CMS" />
<input name="piror" id="piror" type="hidden" value= "P1" />
<input name="stat" id="stat" type="hidden" value= "In Progress" />
<input type="submit" name="submit" id="submit" class="groovybutton" value="...">
</form>
PHP:
<?php
$project = $_POST["pro"];
$pirority = $_POST["piror"];
$status = $_POST["stat"];
mysql_connect ("one", "two", "three");
mysql_select_db ("wsms");
$rest = mysql_query("SELECT * FROM sheet WHERE project='$project' AND
pirority='$pirority' AND status='$status'");
while($row = mysql_fetch_array($rest))
{
echo $row['id'] . " " . $row['date']; echo "<br>";
}
?>
Put isset into your php code
Example
<?php
if(isset($_POST['submit'])){
echo $project = $_POST["pro"]."<br>";
echo $pirority = $_POST["piror"]."<br>";
echo $status = $_POST["stat"];
/* mysql_connect ("one", "two", "three");
mysql_select_db ("wsms");
$rest = mysql_query("SELECT * FROM sheet WHERE project='$project' AND
pirority='$pirority' AND status='$status'");
while($row = mysql_fetch_array($rest))
{
echo $row['id'] . " " . $row['date']; echo "<br>";
}*/
}
?>
<form name="form11" method="post" action="" enctype="multipart/form-data">
<input name="pro" id="pro" type="hidden" value= "CMS" />
<input name="piror" id="piror" type="hidden" value= "P1" />
<input name="stat" id="stat" type="hidden" value= "In Progress" />
<input type="submit" name="submit" id="submit" class="groovybutton" value="...">
</form>
Output
CMS
P1
In Progress
First of all check that if the data is coming in the post or not:
<?php
echo "<pre>";
print_r($_POST);
exit;
?>
If yes than remove the print code i provided , and use extract($_POST); at the top of your PHP code. You query will become like this:
$rest = mysql_query("SELECT * FROM sheet WHERE project='$pro' AND
pirority='$piror' AND status='$stat'");

PHP and SQL Save checkboxes as array

I have written the below to read in data from a radio button (var1) and some checkboxes (var2)
require('config.php');
$con = mysql_connect("localhost", $db_username, $db_password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_name, $con);
$var1 = $_GET['var1'];
$var1 = mysql_real_escape_string( $var1 );
$var2 = $_GET['var2'];
$var2 = mysql_real_escape_string( $var2 );
$running_total = 0;
$last_update = "SELECT * FROM $db_tablename ORDER BY id DESC LIMIT 1";
$result = mysql_query($last_update);
$insert_query = "INSERT INTO $db_tablename (var1, var2) ";
$insert_query .= sprintf( "VALUES('%s', '%s')", $var1, $var2);
$insert_result = mysql_query($insert_query);
if(!$insert_result) {
die('insert query failed' . mysql_error());
}
mysql_close($con)
The input html looks like...
<input type="radio" name="var1" value="value1">Test 1</input>
<input type="radio" name="var1" value="value2">Test 2</input>
<input type="radio" name="var1" value="value3">Test 3</input>
<input type="radio" name="var1" value="value4">Test 4</input>
<input type="checkbox" name="var2" value="checkbox1">CB 1</input>
<input type="checkbox" name="var2" value="checkbox2">CB 1</input>
<input type="checkbox" name="var2" value="checkbox3">CB 1</input>
<input type="checkbox" name="var2" value="checkbox4">CB 1</input>
Everything works as expected and but as var2 allows for multiple checkboxes to be selected I would like to save multiple values. I realise I need to do this as an array but I cant work it out.
Can anyone help or have a solution or example they can point me to?
it would be var2[] as the name and view the results as
foreach($_POST['var2'] as $checkedItem)
{
echo $checkedItem;
}
The html:
`
<input type="radio" name="var1" value="value1">Test 1</input>
<input type="radio" name="var1" value="value2">Test 2</input>
<input type="checkbox" name="var2[]" value="check" /> CB 1
<input type="checkbox" name="var2[]" value="check" /> CB 2
`
and the SQL :
Array $_POST['var2'] contains the checkboxes.
Change the name of var1 to "var1[]", and change the name of var2 to "var2[]"...
$var1 = $_GET['var1'];
$var2 = $_GET['var2'];
echo count($var1);
echo count($var2);
Gl mf

Sending id through select menu using PHP

here is my problem, I want to send id number through select menu using PHP.
Here is the code:
<form name="update" method="post" action="ex_update.php?id=<?php echo ((int)$_POST['get_id']); ?>">
<p><strong>Enter Name:</strong>
<input type="text" name="name">
<br />
ID:
<label for="select"></label>
<select name="get_id">
<?php
$query = "SELECT * FROM test";
$run = mysql_query($query);
while($output = mysql_fetch_array($run)){
echo "<option value=\"{$output['id']}\">{$output['id']}</option>";}
?>
</select>
</p>
<p>
<input type="submit" name="submit" value="Update!">
</p>
</form>
I have tried but when I submit the id in the URL equals to zero. how can I send id to the URL??
here is the ex_update.php >>>
<?php
$connect = mysql_connect("localhost","root","");
$sel_database = mysql_select_db("test");
$id = (int)$_GET["get_id"];
$name = mysql_real_escape_string( $_POST["name"] );
$query = "UPDATE test SET name='{$name}' WHERE id=={$id}";
if($run = mysql_query($query)){
}else{mysql_error();}
?>
Thanks in advance
You can use the form GET method
<form name="update" method="GET" action="ex_update.php">
You can access that select box value using $_GET['get_id'] in ex_update.php
Here is your First Page
Note action of form...
<form name="update" method="post" action="ex_update.php">
<p><strong>Enter Name:</strong>
<input type="text" name="name">
<br />
ID:
<label for="select"></label>
<select name="get_id">
<?php
$query = "SELECT * FROM test";
$run = mysql_query($query);
while($output = mysql_fetch_array($run)){
echo "<option value=\"{$output['id']}\">{$output['id']}</option>";}
?>
</select>
</p>
<p>
<input type="submit" name="submit" value="Update!">
</p>
</form>
And here you can find ex_update.php. Note: $id = (int)$_POST["get_id"];
<?php
$connect = mysql_connect("localhost","root","");
$sel_database = mysql_select_db("test");
$id = (int)$_POST["get_id"];
$name = mysql_real_escape_string( $_POST["name"] );
$query = "UPDATE test SET name='{$name}' WHERE id={$id}";
if($run = mysql_query($query)){
}else{mysql_error();}
?>

Categories