insert proccess whith looping in php - php

firstly sorry my english is bad
then
i have problem
i try to make lopping insert into table but i have problem i can't make array for inputs
to can insert in table with looping from another table
maybe u can't understand me but look to the code and u will understand my problem
$select = mysql_query("SELECT * FROM table_name ");
?>
<form method="post" action="">
<?
while($row =mysql_fetch_array($select))
{
if($_POST['add'])
{
$updpol = mysql_query("insert into table_name2 (yes,no,maybe,g_id)
values
('".$_POST['yes']."','".$_POST['no']."','".$_POST['maybe']."','".$row['id']."')
")
}
else{
?>
<input type="checkbox" name="yes" value="1" />
<br />
<input type="checkbox" name="no" value="1" />
<br />
<input type="checkbox" name="maybe" value="1" />
<?
}
?>
<input type="submit" name="add_poll" value="submit" />
</form>

in your php code , your checking if:
if($_POST['add'])
While there's no input named add in your form.
I believe you meant:
if($_POST['add_poll'])
In order to have multiply instances of inputs , you can do something like:
<input type="checkbox" name="yes[]" value="1" />
<br />
<input type="checkbox" name="no[]" value="1" />
<br />
<input type="checkbox" name="maybe[]" value="1" />
So , now , when posted:
$_POST['yes'] is an array of all the checkboxes.
And as an array , it starts with $_POST['yes'][0] and continues $_POST['yes'][1] and so on.
Therefore:
$select = mysql_query("SELECT * FROM table_name ");
?>
<form method="post" action="">
<?
$i = 0;
while($row =mysql_fetch_array($select))
{
if($_POST['add_poll'])
{
$updpol = mysql_query("insert into table_name2 (yes,no,maybe,g_id)
values
('".$_POST['yes'][$i]."','".$_POST['no'][$i]."','".$_POST['maybe'][$i]."','".$row['id']."')
");
$i++;
}
else{
?>
<input type="checkbox" name="yes[]" value="1" />
<br />
<input type="checkbox" name="no[]" value="1" />
<br />
<input type="checkbox" name="maybe[]" value="1" />
<?
}
?>
<input type="submit" name="add_poll" value="submit" />
</form>

Related

Get variable on Wordpress to PHP

I'm trying to get a value of a form in Wordpress to PHP. The form is like this and it is displaying fine in the preview:
<form action=".../name.php" method="get">
<input type="checkbox" name="form_ques_4" value=0 />
<input type="checkbox" name="form_ques_4" value=1 />
<input type="checkbox" name="form_ques_4" value=2 />
<input type="submit" name="formSubmit" value="submit" />
</form>
If the user selected option 2, the value is 1 and this will later be used as the input in a MySQL database. As I have read in other posts, I should get value with the php line.
$a = $_GET["form_ques_4"];
I have tested some other simple outputs for the .php and there is no problem with the "form action" of the wordpress. I also tried using single and double quotes for the "GET" with no result.
Try to change the names of your checkboxes, if you want a user multiple choice:
<form action=".../name.php" method="get">
<input type="checkbox" name="form_ques_1" value="0" />
<input type="checkbox" name="form_ques_2" value="1" />
<input type="checkbox" name="form_ques_3" value="2" />
<input type="submit" name="formSubmit" value="submit" />
</form>
otherwise, if you want the user makes only one choice use type="radio"
<form action=".../name.php" method="get">
<input type="radio" name="form_ques_4" value="0" />
<input type="radio" name="form_ques_4" value="1" />
<input type="radio" name="form_ques_4" value="2" />
<input type="submit" name="formSubmit" value="submit" />
</form>
EDIT
yes, as AZinkey says, you can also use
<form action=".../name.php" method="get">
<input type="checkbox" name="form_ques[]" value="0" />
<input type="checkbox" name="form_ques[]" value="1" />
<input type="checkbox" name="form_ques[]" value="2" />
<input type="submit" name="formSubmit" value="submit" />
</form>
then get the results in php
$checked = $_GET['form_ques'];
for($i=0; $i < count($checked); $i++){
echo $checked[$i] . "<br/>";
}
Quote your value attribute like value="0" and update name to "form_ques_4[]"
<input type="checkbox" name="form_ques_4[]" value="0" />

Displaying a column that a user created in a form

Im trying to display a single column from where a user created a form, i have a table and user sessions set up. I need it so that only that column is displayed for the user that created it.
This is the form
<form action="core/process.php" method="post" id="registration" >
<input type="hidden" name="formID" value="Product_Tracker" />
<input type="hidden" name="id_user" value="<?php echo $_SESSION['name_of_user']; ?>" />
<p>Name of product:<input type="text" name="Name of Product" class="input" />
<p>Please select the tests that were done on the product.</p>
<p>In Circuit Test (ICT): Yes: <input type="radio" name="ICT" value="yes" /> No: <input type="radio" name="ICT" value="no" /></p>
<p>Visual Inspection: Yes: <input type="radio" name="Visual Inspection" value="yes" /> No: <input type="radio" name="Visual Inspection" value="no" /></p>
<p>XRAY: Yes: <input type="radio" name="XRAY" value="yes" /> No: <input type="radio" name="XRAY" value="no" /></p>
<p>Automated Optical Inspection (AOI): Yes: <input type="radio" name="AOI" value="yes" /> No: <input type="radio" name="AOI" value="no" /></p>
<!--<p>Checkbox1 <input type="checkbox" name="checkbox" value="checkbox1" /> Checkbox2: <input type="checkbox" name="checkbox" value="checkbox2" /></p>-->
<input type="submit" value="Submit" />
<p>
<a href='access-controlled.php'>Back</a>
</p>
</form>
</div>
</body>
</html>
<?php VDEnd(); ?>
Ive tried this but it doesnt work,
$con = mysql_connect("","","");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("database",$con);
$result = mysql_query("SELECT id_user, Name_of_product FROM Product_Tracker WHERE id_user=$_SESSION['name_of_user']");
while ($row = mysql_fetch_assoc($result)) {
echo $row['Name_of_Product'];
echo "<br />";
this did it;
mysql_select_db("database",$con);
$id=$_SESSION['name_of_user'];
$result = mysql_query("SELECT id_user, Name_of_product FROM Product_Tracker WHERE id_user='$id'");
while ($row = mysql_fetch_array($result))
{
echo $row['Name_of_product'] . "<br/>";
}
mysql_query($query);
mysql_close($con);
?>
have you tried this
$id=$_SESSION['name_of_user'];
$result = mysql_query("SELECT id_user, Name_of_product FROM Product_Tracker WHERE id_user='$id'");

PHP var_dump only outputting value of bottom checkbox

Hi and thanks for reading my question. I am using a simple form to get some input :
<p>Select your favorite two countries below:</p>
<form id="world" name="world" action="/order.php" method="post">
<input type="checkbox" name="countries" value="USA" /> USA<br />
<input type="checkbox" name="countries" value="Canada" /> Canada<br />
<input type="checkbox" name="countries" value="Japan" /> Japan<br />
<input type="checkbox" name="countries" value="China" /> China<br />
<input type="checkbox" name="countries" value="France" /> France<br />
<input type="submit" value="Order">
</form>
I want to make sure order.php is geting all of the choices selected, so order.php only contains the following code :
<pre>
<?php var_dump($_POST);?>
</pre>
Unfortunately, it is only outputting whatevre is the bottom-most checkbox that is checked.
The output is like this :
array(1) {
["countries"]=>
string(6) "Canada"
}
If i try the following code for output :
<?php
foreach($_POST as $key=>$post_data){
echo "You posted:" . $key . " = " . $post_data . "<br>";
}
?>
I get this output :
You posted:countries = Canada
Can anyone tell me where i am going wrong and how i can retrieve all of the data, for every box that is ticked ?
Thank you.
You gave the same name to your checkboxes, and PHP will overwrite previously parsed name submissions with the current value. You need to use the array-notation hack:
<input type="checkbox" name="countries[]" value="Canada" /> Canada<br />
^^
which then makes $_POST['countries'] an array of all the values submitted.
echo "You posted: " . implode(',', $_POST['countries']);
<p>Select your favorite two countries below:</p>
<form id="world" name="world" action="/order.php" method="post">
<input type="checkbox" name="countries[]" value="USA" /> USA<br />
<input type="checkbox" name="countries[]" value="Canada" /> Canada<br />
<input type="checkbox" name="countries[]" value="Japan" /> Japan<br />
<input type="checkbox" name="countries[]" value="China" /> China<br />
<input type="checkbox" name="countries[]" value="France" /> France<br />
<input type="submit" value="Order">
</form>
Change it to above, this will store all your checkboxes results for you!

Issue inserting multiple rows using PHP/SQL

This is the form in which I am using to try and create multiple rows to insert into a database.
<input name="input[]" type="checkbox" value="0" id="input_0" /> Directional<br /> <br/>
<input name="input[]" type="checkbox" value="1" id="input_1" /> Technical <br />
<br />
<input name="input[]" type="checkbox" value="2" id="input_2" /> Reference <br />
<br />
<input name="input[]" type="checkbox" value="3" id="input_3" /> Research <br />
<br />
<input name="input[]" type="checkbox" value="4" id="input_4" /> Phone <br /></h3>
<input type="submit" name="button" id="button" value="Submit Tally" style="height: 25px; width: 100px">
</Form>
Current issue of inserting multiple rows into mysql database from a checkbox form using implode. Would a loop work to fix this issue? I can see the array (#,#,#,#) when check boxes are selected
$type = $_POST['input'];
// $sid = $_SERVER['REMOTE_ADDR'];
$user = $_POST['user'];
if(count($type) > 0)
{
$type_string = implode(',', $type);
}
$sql = "INSERT INTO tally (tid, sid, uid, date, time, catid)
VALUE (NULL, 1, '$user', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, '$type_string')";
mysql_query($sql) or die(mysql_error());
echo "Success";
print_r($type_string);
A loop would certainly work, after you created some array from that $type variable you receive as input, and feed this into your query one at the time.
But I hope you're not going to use this code in some website just like this. As maiotano84 already commented, the way you create your query is a little dangerous. It is very easy to do some unwanted things with your data. At least you should put your variables through a function like
string mysql_real_escape_string ( string $unescaped_string [, resource $link_identifier = NULL ] )
Here is some more info on this function frm the php.net site.
compute the value of $type as below:
$i=0;
foreach ($_POST as $v) {
if(isset($v['input'.$i])) {
$type = $v;
$i++;
}
}
<Form action="" method="POST">
<input name="input[]" type="checkbox" value="0" id="input_0" /> Directional<br /> <br/>
<input name="input[]" type="checkbox" value="1" id="input_1" /> Technical <br />
<br />
<input name="input[]" type="checkbox" value="2" id="input_2" /> Reference <br />
<br />
<input name="input[]" type="checkbox" value="3" id="input_3" /> Research <br />
<br />
<input name="input[]" type="checkbox" value="4" id="input_4" /> Phone <br /></h3>
<input type="submit" name="button" id="button" value="Submit Tally" style="height: 25px; width: 100px">
</Form>
<?php
$type = $_POST['input'];
// $sid = $_SERVER['REMOTE_ADDR'];
$user = $_POST['user'];
for($i=0;$i<count($type);$i++){
$sql = "INSERT INTO tally (tid, sid, uid, date, time, catid)
VALUE (NULL, 1, '$user', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, ".$type[$i].")";
}
?>

Using Checkbox Values in PHP

I have this page (code below) which contains a series of 5 checkboxes, all representing different values, and i need to use them to query a database.
<form id="form1" name="form1" method="post" align="center" action="section_search_results1.php">
<p>Select The <b>Section</b> You Wish To Search In Below... </p>
<p>
<label for="section"></label>
<label for="section2"></label>
<label>
<input type="checkbox" name="SectionSelect" value="Functional" id="SectionSelect_0" />
Functional</label>
<label>
<input type="checkbox" name="SectionSelect" value="Technical" id="SectionSelect_1" />
Technical</label>
<label>
<input type="checkbox" name="SectionSelect" value="Commercial" id="SectionSelect_2" />
Commercial</label>
<label>
<input type="checkbox" name="SectionSelect" value="Implementation" id="SectionSelect_3" />
Implementation</label>
<label>
<input type="checkbox" name="SectionSelect" value="Innovation" id="SectionSelect_4" />
Innovation</label>
<br />
</p>
<p>Enter the <b>Keyword(s) or Keyphrase</b> Below...</p>
<p>
<label for="kword"></label>
<input type="text" name="kword" id="kword" placeholder="Enter Keyword(s)" />
<br />
</p>
<p>
<input type="submit" name="submit" id="submit" value="Search" /> | | <input type="reset" name="clear" id="clear" value="Clear" />
</p>
</form>
as you can see each has its own value which is the term used to query the database. the php query code on the results page is as follows
<?php
$kword = $_POST["kword"];
$section = $_POST["SectionSelect"];
function boldText($text, $kword) {
return str_ireplace($kword, "<strong>$kword</strong>", $text);
}
// Connects to your Database
mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
mysql_real_escape_string($kword);
$data = mysql_query("select company_name, section_name, question, answer from company, rfp, section, question_keywords
where company.company_id = rfp.company_id
and rfp.rfp_id = question_keywords.rfp_id
and question_keywords.section_id = section.section_id
and section_name like '%$section%'
and keywords like '%$kword%';")
or die(mysql_error());
?>
ultimately what i want this to do is to query the database with the query to potentially have where clauses for each of the checkbox values? for example i want to select x where y like 'Technical' AND y like 'Functional' and so on...
Any help would be great
thanks
Set up your checkbox names as array-keys to use them as an array with values of all selected checkboxes:
<input type="checkbox" name="SectionSelect[0]" value="Functional" />
<input type="checkbox" name="SectionSelect[1]" value="Technical" />
echo $_POST['SectionSelect'][0]; // should print "Functional"
echo $_POST['SectionSelect'][1]; // should print "Technical"

Categories