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']);
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'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.
<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
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')";
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