When my insert runs it will write the text in the text box but if I choose a radio button selection other than "other" nothing is inserted into the table.
If I remove the "other"code the radio button selected is written to the table.
here is the code
<input type="radio" name="job" value="PHP Programmer">PHP Programmer
<input type="radio" name="job" value="SQL Programmer">SQL Programmer<br>
<input type="radio" name="job">Other <input type="text" name="job" >
<h3>* If yes, check which ISO standard(s) you are accredited?</h3>
<input type="radio" name="iso_standard" value="17020">17020
<input type="radio" name="iso_standard" value="17025">17025
<br />
<input action="submit" type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['job']) && isset($_POST['iso_standard']))
{
$job = mysqli_real_escape_string($db, $_POST['job']);
$iso_standard =mysqli_real_escape_string($db, $_POST['iso_standard']);
$sql="insert into tbl_test_insert(iso_cert, iso_standard)
values ('$job', '$iso_standard')";
if(!mysqli_query($db, $sql))
{
die('Error: ' .mysqli_error($db));
}
echo "1 record added";
}
else
{
echo "You didn't choose all the options!
}
?>
You are overwriting the value of job with the text input field. It does not mather if it has any value or not.
Please rename your <input type="text" name="job" > to for example otherJob and you should be fine
edit:
change <input type="radio" name="job">Other <input type="text" name="job" >
to <input type="radio" name="job" value="other">Other <input type="text" name="otherJob">
change $job = mysqli_real_escape_string($db, $_POST['job']);
to
$job = mysqli_real_escape_string($db, $_POST['job']);
$otherJob = mysqli_real_escape_string($db, $_POST['otherJob']);
if ($job == 'other') {
$jobField = $otherJob;
} else {
$jobField = $job;
}
and change $sql="insert into tbl_test_insert(iso_cert, iso_standard)
values ('$job', '$iso_standard')";
to $sql="insert into tbl_test_insert(iso_cert, iso_standard)
values ('$jobField', '$iso_standard')";
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
I am trying to show specific data if two variables are set in a form.
$retired = "";
$stolen = "";
if(isset($_POST['submit'])){
$retired = $_GET['showretired'];
$stolen = $_GET['showstolen'];
}
I have tried the bellow:
<?php }elseif(isset($_GET['showretired']) && (isset($_GET['showstolen'])){ ?>
Which does not work
<?php }elseif(isset($retired, $stolen)){
Which works when not set
<?php }elseif(isset($_GET['showretired'], $_GET['showstolen'])) {
This one only shows the retired part.
I am unsure of the best way to do this.
Here is the form:
<form>
<label>Show Retired Column </label><input type="checkbox" name="showretired">
<label class="space">Show Stolen Column </label><input type="checkbox" name="showstolen">
<input class="space" type="submit" name="submit" value="Refine">
</form>
This is how I am using the variable in the same file.
<?php }elseif(isset($_GET['showretired'], $_GET['showstolen'])) {
?>
<tr>
<th>Retired</th>
<th>Stolen</th>
</tr>
<?php }else{ ?>
<?php
if(isset($_POST['submit'])){
if(isset($_POST['showretired'])) {
$retired = $_POST['showretired'];
}
if(isset($_POST['showstolen'])) {
$stolen = $_POST['showstolen'];
}
if(isset($retired)){
echo $retired;
}
if(isset($stolen)) {
echo $stolen;
}
}
?>
<form action="" method="POST">
<label>Show Retired Column </label><input type="checkbox" name="showretired" value="The retired field">
<label class="space">Show Stolen Column </label><input type="checkbox" name="showstolen" value="The showstolen field">
<input class="space" type="submit" name="submit" value="Refine">
</form>
No method on the form means it will default to GET. Change the conditional and check your checkboxes.
<?php
if(isset($_GET['submit'])){
$foo = isset($_GET['foo']) ? true : false;
$bar = isset($_GET['bar']) ? true : false;
var_dump($foo);
var_dump($bar);
}
?>
<form>
<input type="checkbox" name="foo">
<input type="checkbox" name="bar">
<input type="submit" name="submit">
</form>
if(isset($_GET['submit'])){
if(isset($_GET['showretired']) && $_GET['showretired'] !="" && isset($_GET['showstolen']) && $_GET['showstolen'] !="" )
{
echo $retired." ".$stolen;
}
}
and form
<form>
<label>Show Retired Column </label><input type="checkbox" name="showretired" value="showretired" checked="checked">
<label class="space">Show Stolen Column </label><input type="checkbox" name="showstolen" value="showstolen" checked="checked">
<input class="space" type="submit" name="submit" value="Refine">
</form>
Hope it will help
I'm having trouble with the array results in my php. My first problem is this :
It shows everything even if the checkbox isn't checked. My second problem is that it won't insert the values to the database even though it's connected to the database (as you can see in the previous screenshot, it says "connected successfully").
This is my html form:
<form method="POST">
<input type="hidden" name="item[]" value="cupcake">
<input type="text" name="items" value="cupcake" readonly><br>
<b>Price :</b> <span name="price" value="3.00">$17.00</span><br>
Quantity: <input tabindex="1" name="quantity[]" min="0" max="5" type="number" class="quantity" value="1" /><br>
<input tabindex="1" name="checkbox[]" type="checkbox" value="17" /><span>Add to Cart</span></label></div></div></td><br>
<input type="hidden" name="item[]" value="cake">
<input type="text" name="items" value="cake" readonly><br>
<b>Price :</b> <span name="price" value="20.00">$20.00</span><br>
Quantity: <input tabindex="1" name="quantity[]" min="0" max="5" type="number" class="quantity" value="1" /><br>
<input tabindex="1" name="checkbox[]" type="checkbox" value="20" /><span>Add to Cart</span></label></div></div></td><br>
<input type="submit" name="insertBT"><br>
</form>
PHP:
if(isset($_POST['insertBT']))
{
class db_conn
{
public function create_conn($servername, $username, $password, $db)
{
global $conn;
$conn = new mysqli ($servername, $username, $password, $db);
}
public function check_conn()
{
global $conn;
if($conn->connect_error)
{
die ("Connection Failed : " . $conn->connect_error);
}
else
{
echo ("Connected Successfully <br>");
}
}
public function insert()
{
if(isset($_POST['checkbox'])) {
foreach($_POST['checkbox'] as $check) {
$check = implode(',', $_POST['checkbox']);
$name = implode(',', $_POST['item']);
$quantity = implode(',', $_POST['quantity']);
}
echo $check . "<br>";
echo $name . "<br>";
echo $quantity . "<br>";
mysql_query("INSERT INTO purchases(Product, Quantity, Price) VALUES('$name', '$quantity','$check')");
}
}
}
$obj1 = new db_conn;
$obj1->create_conn("localhost","root","", "dbtest");
$obj1->check_conn();
$obj1->insert();
}
You shouldn't be using implode. That puts a comma-separated list of everything in the form into each row that you insert, and repeats this for every box that's checked. You should just insert one item in each row, by indexing the arrays.
However, when you have a checkbox in a form, it only submits the ones that are checked. The result of this is that the indexes of the $_POST['checkbox'] array won't match up with the corresponding $_POST['item'] and $_POST['quantity'] elements. You need to put explicit indexes into the checkbox names so you can relate them.
<form method = "POST">
<input type = "hidden" name = "item[]" value = "cupcake">
<input type = "text" name = "items" value = "cupcake" readonly><br>
<b>Price :</b> <span name = "price" value = "3.00">$17.00</span><br>
Quantity: <input tabindex="1" name="quantity[]" min="0" max="5" type="number" class="quantity" value="1" /><br>
<input tabindex="1" name="checkbox[0]" type="checkbox" value="17" /><span>Add to Cart</span></label></div></div></td><br>
<input type = "hidden" name = "item[]" value = "cake">
<input type = "text" name = "items" value = "cake" readonly><br>
<b>Price :</b> <span name = "price" value = "20.00">$20.00</span><br>
Quantity: <input tabindex="1" name="quantity[]" min="0" max="5" type="number" class="quantity" value="1" /><br>
<input tabindex="1" name="checkbox[1]" type="checkbox" value="20" /><span>Add to Cart</span></label></div></div></td><br>
<input type = "submit" name = "insertBT"><br>
</form>
Then your PHP code can be like this:
$stmt = $conn->prepare("INSERT INTO purchases (Product, Quantity, Price) VALUES (?, ?, ?)");
$stmt->bind_param("sis", $name, $quantity, $price);
foreach ($_POST['checkbox'] as $i => $price) {
$name = $_POST['name'][$i];
$quantity = $_POST['quantity'][$i];
$stmt->execute();
}
BTW, putting the prices in your HTML seems like a bad idea. Nothing stops the user from modifying HTML using the web inspector before they submit the form, so they could lower the price. You should get the prices from the database when processing the form.
Also, notice that in your original code you opened the database connection using MySQLi, but then you tried to do the insert using mysql_query instead of $conn->query(). You can't mix APIs like that; myql_query can only be used when you open the connection with mysql_connect.
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'm really struggling with getting checkboxes to work, I've looked up tutorials
and have found no help... I'm not sure how to set up my table in MAMP for checkboxes and how to insert it...
If some one could help that would be just fantastic guys...
HTML:
<form action="mainpage2.php" method="POST">
Search : <input type="text" name="firstname" id="name" />
<input type="submit" name="confirm" value="Submit" />
<input type="checkbox" name="tick[]" value="male" />
<input type="checkbox" name="tick[]" value="female" />
<input type="checkbox" name="tick[]" value="alien" />
</form>
PHP:
if(isset($_POST['confirm'])) {
$subject = $_POST['firstname'];
$subjec = $_POST['tick'];
$result = mysql_query("INSERT INTO chipsticks (Name,sports) VALUES ('$subject'),('subjec');",$database);
You will need to flatten the array in order to store it in database.
if(isset($_POST['confirm']))
{
$subject = $_POST['firstname'];
$subjec = $_POST['tick'];
$sports = '';
if(is_array($subjec) && count($subjec)>0)
{
$sports = implode(',',$subjec);
}
$result = mysql_query("INSERT INTO chipsticks (Name,sports) VALUES ('$subject','$sports')",$database);
}
Instead of implode, you can also use serialize