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
Related
I have a form with multiple fields: Textinputs, Checkboxes, Radios.. and I want to submit it to a MySQL database. When I comment the checkboxes HTML, and the corresponding php code, everything is working fine, and everything is submitted and saved in the DB. If I try to submit the checkbox-form and I uncomment it, nothing get submitted, and clicking on the submit-button doesn't make any effect.
How can I submit the value of the checkbox-field to the MySQL-Database as a string, with the values separated with a semi-colon? For ex. if the checkbox fields are: Ab, Cd, De, Fg - and "Ab" and "De" are checked, the following string gets submitted: "Ab;Cd"
Here is a part of my HTML-form:
<div class="row">
<div class="col-sm-4">
<fieldset class="form-group">
<label for="plattform">Platform</label>
<form id="formId">
<input type="checkbox" name="check_list[]" value="Android">Android
<input type="checkbox" name="check_list[]" value="iPhone">iPhone
<input type="checkbox" name="check_list[]" value="iPad">iPad
<input type="checkbox" name="check_list[]" value="Windows Phone">Windows Phone
</form>
<!-- <input type="checkbox" name="check_list[]" value="Android">
<input type="checkbox" name="check_list[]" value="iPhone">
<input type="checkbox" name="check_list[]" value="iPad">
<input type="checkbox" name="check_list[]" value="Windows Phone"> -->
</fieldset>
</div>
<div class="col-sm-4">
<fieldset class="form-group">
<label for="featured">Featured</label>
<div>
<input type="radio" name="featured" required>True</input>
</div>
<div>
<input type="radio" name="featured" required checked>False</input>
</div>
</fieldset>
</div>
here is a sample of my php-file:
<?php /* Attempt MySQL server connection. Assuming you are running
MySQL server with default setting (user 'root' with no password) */
/* Database connection start */
$servername = "localhost";
$username = "serverName_Here";
$password = "password_Here";
$dbname = "dbName_Here";
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
// Check connection
if($conn === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$stName = mysqli_real_escape_string($conn, $_POST['sName']);
$lgName = mysqli_real_escape_string($conn, $_POST['lName']);
$desc = mysqli_real_escape_string($conn, $_POST['desc']);
$Platform = '';
if(!empty($_POST['check_list'])) {
$counter = 0;
foreach($_POST['check_list'] as $check) {
if ($counter < 1) {
$Platform = $check;
} else {
$Platform = $excludePlatform + ';' + $check;
}
counter++;
}
}
$Platform = mysqli_real_escape_string($conn, $_POST['check_list']);
$sql = "INSERT INTO tableName_Here (stName, lgName, details_description, Platform) VALUES ('$stName', '$lgName', '$desc', '$Platform')";
if(mysqli_query($conn, $sql)){
echo "Records added successfully.";
} else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
} // close connection
mysqli_close($conn); ?>
You need to have the checkboxes within the form tags.
The easiest way is to put the form opening and closing tabs above and below the rest of the field code. The submit buttons should be within the form as well.
On the backend checkbox values will come through as an array. You can then do something like this if you want to save them comma separated.
$values = implode(', ', $_POST['check_list']);
First of all,Why you haven't used POST as method in form tag also you haven't used submit button.In that way you can access value of checkboxes in php like
$value=$_POST['check_list[]'];
Try this whole example,
Table Structure
CREATE TABLE IF NOT EXISTS `games` (
`id` int(12) NOT NULL AUTO_INCREMENT,
`game_name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
<?php
include_once("yourconfig.php"); //include your db config file
extract($_POST);
$check_exist_qry="select * from games";
$run_qry=mysql_query($check_exist_qry);
$total_found=mysql_num_rows($run_qry);
if($total_found >0)
{
$my_value=mysql_fetch_assoc($run_qry);
$my_stored_game=explode(',',$my_value['game_name']);
}
if(isset($submit))
{
$all_game_value = implode(",",$_POST['games']);
if($total_found >0)
{
//update
$upd_qry="UPDATE games SET game_name='".$all_game_value."'";
mysql_query($upd_qry);
}
else
{
//insert
$ins_qry="INSERT INTO games(game_name) VALUES('".$all_game_value."')";
mysql_query($ins_qry);
}
}
?>
<form method="post" action="">
Games You Like: <br/>
<input type="checkbox" name="games[]" value="1" <?php if(in_array(1,$my_stored_game)){echo "checked";}?>><label>Football</label><br>
<input type="checkbox" name="games[]" value="2" <?php if(in_array(2,$my_stored_game)){echo "checked";}?>><label>Basket Ball</label><br>
<input type="checkbox" name="games[]" value="3" <?php if(in_array(3,$my_stored_game)){echo "checked";}?>><label>Pool</label><br>
<input type="checkbox" name="games[]" value="4" <?php if(in_array(4,$my_stored_game)){echo "checked";}?>><label>Rugby</label><br>
<input type="checkbox" name="games[]" value="5" <?php if(in_array(5,$my_stored_game)){echo "checked";}?>><label>Tennis</label><br>
<input type="checkbox" name="games[]" value="6" <?php if(in_array(6,$my_stored_game)){echo "checked";}?>><label>Cricket</label><br>
<input type="checkbox" name="games[]" value="7" <?php if(in_array(7,$my_stored_game)){echo "checked";}?>><label>Table Tennis</label><br>
<input type="checkbox" name="games[]" value="8" <?php if(in_array(8,$my_stored_game)){echo "checked";}?>><label>Hockey</label><br>
<input type="submit" name="submit" value="submit">
</form>
this is just basic example and query i have added in this example, you can learn from this basic example and i think this is very useful for you... if useful than give correct answer for this solution
<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
Hello i'm at linking my form data to my mysql database server so far so good i have a little problems here
my prcoes.php. code :
$db_selected = mysql_select_db (DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' / mysql_error());
}
$value = $_POST['name'];
$value2 = $_POST['surname'];
$value3 = $_POST['email'];
$value4 = $_POST['phone'];
$value4 = $_POST['activity'];
$value5 = $_POST['ltype'];
With checkboxes $_POST['ltype'];i get only Array on Mysql as result ?
I get the right values with this code by sending form data to my email :
'LType : ' . implode(',', $_POST['ltype']). "\n" .
Any help will be welcomed , thanks in advance
Update here my chekbox code :
<div class="thumb1" >
<label for="word" ><img class="img" src="images/my1.jpg" /></label>
<input type="checkbox" class="chk" name="ltype[]" id="word" value="word" /><hr> <p><strong>Word Mark Logo</strong></p>
</div>
<div class="thumb1" >
<label for="letter"><img class="img" src="images/my2.jpg" /></label>
<input type="checkbox" class="chk" name="ltype[]" id="letter" value="letter" /><hr> <p><strong>Letter Mark Logo</strong></p>
</div>
<div class="thumb1">
<label for="emblerm"><img class="img" src="images/my3.jpg" /></label>
<input type="checkbox" class="chk" name="ltype[]" id="emblerm" value="emblerm" /><hr> <p><strong>Emblerm Logo</strong></p>
</div>
Why don't you:
$value5 = implode(',', $_POST['ltype']);
if you want the array back just explode the database value.
Depends on how you define the checkbox if
<form action='XXX.php' method='POST'>
Football: <input type="checkbox" name="sports[]" value="football" />
Baseball: <input type="checkbox" name="sports[]" value="baseball" />
</form>
And on the XXX.php it return as array
if ( $_POST['sports'] ) {
$arySports = $_POST['sports'];
foreach( $arySports AS $value ) {
echo $value ."<br>";
}
}
If you did
Subscribe Now <input type="checkbox" name="subscribe" />
And on the XXX.php it return as on
If you did
Subscribe Now <input type="checkbox" name="subscribe" value="now" />
Then it return string "now". So try with this and see what you want for your checkbox
<form action="XXX.php" method="POST">
<input type='checkbox' name='A'/>
<input type='checkbox' name='B' value='this is array'/>
<input type='checkbox' name='B' value='the second one'/>
<input type='checkbox' name='C[]' value='A'/>
<input type='checkbox' name='C[]' value='B'/>
<input type='checkbox' name='C[]' value='C'/>
<input type='checkbox' name='D' value='string'/>
</form>
on the XXX.php do
print_r( $_POST['A'] );
echo "<br>";
print_r( $_POST['B'] );
echo "<br>";
print_r( $_POST['C'] );
echo "<br>";
print_r( $_POST['D'] );
echo "<br>";
I have a comment BOX, which has 5 fields, one is name, email, RATE, comment, articleid.
Rate field is a radio type, which has 5 radio buttons with value 1,2,3,4,5. IF someone click on rate my product and it should save the rated value in databse. I'm using RATE as INT in database, It stores 0 in it, If i use RATE as TEXT in database, it stores "on" in database. It is not storing rating values like 1,2,3,4,5.
My form Code
<form action="manage_comments.php" method="post">
<span class="rating">
<input type="radio" class="rating-input" id="rate" name="rate" value="1">
<label for="rating-input-1-5" class="rating-star"></label>
<input type="radio" class="rating-input" id="rate" name="rate" value="2">
<label for="rating-input-1-4" class="rating-star"></label>
<input type="radio" class="rating-input" id="rate" name="rate" value="3">
<label for="rating-input-1-3" class="rating-star"></label>
<input type="radio" class="rating-input" id="rate" name="rate" value="4">
<label for="rating-input-1-2" class="rating-star"></label>
<input type="radio" class="rating-input" id="rate" name="rate" value="5">
<label for="rating-input-1-1" class="rating-star"></label>
</span>
<input type='hidden' name='articleid' id='articleid' value='<?php echo $_GET["id"]; ?>' />
<input type="submit" name="submit" value="Publish Now"></p>
</form>
My php Code
<?php
if( $_POST )
{
$con = mysql_connect("localhost","asfi","asfi");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("aw-tech", $con);
$post_rate = (isset($_POST['rate'])) ? $_POST['rate'] : '';
$articleid = (int)isset($_GET['id']);
if(!is_numeric($articleid))
die('invalid article id');
$sql="INSERT INTO `aw-tech`.`comment` (cid, name, email, website, comment, timestamp, rate, articleid) VALUES (NULL, '$_POST[name]', '$_POST[email]', '$_POST[website]', '$_POST[comment]', CURRENT_TIMESTAMP, '$post_rate', ".$articleid.")";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Comment Saved";
mysql_close($con);
}
?>
Secondly My articleid saves always 0.. my page id is .php?id=49 , it is 49 but if i made comment on that page, It saves my article ID always 0.
articleid & rate Both are INT in database, I have used them as TEXT too in database but didn't work
I think I found your problem. Change form declaration to:
<form action="manage_comments.php" method="post">
Remove row:
<input type='hidden' name='articleid' id='articleid' value='<?php echo $_GET["id"]; ?>' />
the Then PHP code from:
$post_rate = (isset($_POST['rate'])) ? $_POST['rate'] : '';
$articleid = (int)isset($_GET['id']);
to:
$post_rate = (isset($_POST['rate'])) ? $_POST['rate'] : 0;
$articleid = (int)isset($_GET['id']) ? $_GET['id'] : 0;
And for debug add this after the $sql variable and insert into a comment here:
print_r($_GET);
print_r($_POST);
echo $sql;
Hope it works.
when you post the form in action you put : manage_comments.php
so the "id" be lost!
in action you can put : manage_comments.php?id=<?php=$_GET[id]?>
or
after submit form
use $_POST[articleid] not $_GET['id']
i think it supposes to be
$post_rate = (isset($_POST['rate'])) ? $_POST['rate'] : '';
$articleid = (int)isset($_POST['articleid']);
I am trying to insert values from a form into my mysql database. I have single value forms + array values from the form. How can I insert the array values into my database with the single form values attached to all rows?
The HTML:
Start:<br>
<input type="text" name="start" id="start">
End:<br>
<input type="text" name="end" id="end">
<input type="text" name="item[]" placeholder="Manufacturer #" /><br>
<input type="text" name="description[]" placeholder="Description" /><br>
<input type="text" name="item[]" placeholder="Manufacturer #" /><br>
<input type="text" name="description[]" placeholder="Description" /><br>
<input type="text" name="item[]" placeholder="Manufacturer #" /><br>
<input type="text" name="description[]" placeholder="Description" /><br>
The PHP:
if (isset($_POST['submit']))
{
$link = mysql_connect('localhost', 'user', 'pass');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb") or die(mysql_error());
echo 'Connected successfully';
$query = "INSERT INTO agreement (start, end, item_number, item_description) VALUES ";
foreach($_POST['item'] as $i => $item)
{
// Get values from post.
$item = mysql_real_escape_string($item);
$description = mysql_real_escape_string($_POST['description'][$i]);
$start = mysql_real_escape_string($_POST['start'][$i]);
$end = mysql_real_escape_string($_POST['end'][$i]);
// Add to database
$query = $query." ('$start','$end','$item','$description') ,";
}
$query = substr($query,0,-1); //remove last char
$result = mysql_query($query);
}
I will be changing the code to mysqli/pdo, I know that the current code is unsecure.
Any help is appreciated, thanks!
Just use $_POST['start'] and $_POST['end'] without array access since they will be the same each time. You can use array access for item and description.