By this code i can able insert the checkbox values to database. But i need add one column in phpmyadmin and in that column i need store the values like, if i select 2 checkbox, i need store that ckeckbox values in one column in another column i need store for selected checkbox as YES and unselected values as NO. But see i want both column
here is my code
<input type="checkbox" name="checkbox[]" value="Home" id="pageHome" onChange="toggleVisibility('home');" /><label for="pageHome"> Home</label><img id="home" src="images/icon-tick.png" style="visibility:hidden"/><br/>
<input name="checkbox[]" value="About_us" id="page_Aboutus" type="checkbox" onChange="toggleVisibility('aboutus');"><label for="page_Aboutus"> About Us</label><img id="aboutus" src="images/icon-tick.png" style="visibility:hidden" /><br/>
<input name="checkbox[]" value="Services" id="pageServices" type="checkbox" onChange="toggleVisibility('services');"><label for="pageServices"> Services</label><img id="services" src="images/icon-tick.png" style="visibility:hidden" /><br/>
<input name="checkbox[]" value="Products" id="pageProducts" type="checkbox" onChange="toggleVisibility('products');"><label for="pageProducts"> Products</label><img id="products" src="images/icon-tick.png" style="visibility:hidden"/><br/><br>
<input name="checkbox[]" value="Enquiry" id="pageEnquiry" type="checkbox" onChange="toggleVisibility('enquiry');"><label for="pageEnquiry"> Enquiry</label><img id="enquiry" src="images/icon-tick.png" style="visibility:hidden"/><br/><br>
<input name="checkbox[]" value="Contact_us" id="pageContact" type="checkbox" onChange="toggleVisibility('Contact');"><label for="pageContact">Contact Us</label><img id="Contact" src="images/icon-tick.png" style="visibility:hidden" /><br/>
php code
$required_pages = implode(',', $_REQUEST['checkBox']);
$sql="insert into request_quote(customer_name,organisation,phone_num,email,country,state,city,zipcode,project_type,website_url,website_purpose,website_keyword,Competitors,sample_websites,no_of_updation,required_pages,additional_page,other_details)
values('$customer_name','$organisation','$phone_num','$email','$country','$state','$city','$zipcode','$project_type','$website_url','$website_purpose','$website_keyword','$Competitors','$sample_websites','$no_of_updation','$required_pages','$additional_page','$other_details')";
mysql_query($sql) or die(mysql_error());
You can add a value='YES' attribute to each of your checkboxes.
Then make an array where you assume no checkbox has been checked. Because when we iterate later, only the checked checkboxes will be sent via $_REQUEST.
$checkBoxes['customer_name']="NO";
$checkBoxes['organisation']="NO";
$checkBoxes['phone_num']="NO";
$checkBoxes['email']="NO";
$checkBoxes['country']="NO";
$checkBoxes['state']="NO";
$checkBoxes['city']="NO";
$checkBoxes['zipcode']="NO";
$checkBoxes['project_type']="NO";
$checkBoxes['website_url']="NO";
$checkBoxes['website_purpose']="NO";
$checkBoxes['website_keyword']="NO";
$checkBoxes['Competitors']="NO";
$checkBoxes['sample_websites']="NO";
$checkBoxes['no_of_updation']="NO";
$checkBoxes['required_pages']="NO";
$checkBoxes['additional_page']="NO";
$checkBoxes['other_details']="NO";
// Only the checked checkboxes wil be itterated below.
// This will mean the $value would be YES no matter what.
// So I write it literal for SQL-Injection-security
foreach ($_REQUEST['checkbox'] as $checkboxName=>$value){
$checkBoxes[$checkBoxName]="YES";
}
And in your SQL-Query, replace the variables with items from the array.
I.E.
$sql="insert into request_quote(customer_name)
values('".$checkBoxes['customer_name']."')";
Security tip:
Also, directly inserting user input can be a huge security risk. Use mysql_real_escape_string($value) on all user-input-values you plan to use in a SQL-query.
You should look into $_POST. W3Schools will help you out: http://www.w3schools.com/php/php_forms.asp.
You have to use 3 tables, and relationate each other
table 1 -> customers (id, name, etc)
table 2 -> required_pages (id, name) - here you will add your options from checkbox (Home, About_us, etc)
table 3 -> customers_required_pages (id, customer_id, required_page_id)
When saving data, you will have to save sequentialy:
Your customer:
$sql = "INSERT INTO customers (field1, field2, fieldn...)"
$qry = mysql_query($sql);
Retrieve your customer Id:
$sql = "SELECT LAST_INSERT_ID() AS customerId";
$qry = mysql_query($sql);
$customerId = mysql_fetch_assoc($qry);
Save the relationship between customers and required_pages
<?php
foreach ($_REQUEST['checkBox'] as $oneCheckbox => $checkboxValue) {
$sql = "INSERT INTO customers_required_pages (customer_id, required_page_id) VALUES ({$customerId['customerId']}, '{$checkboxValue}')";
}
?>
When retrieving your data, you will select values from table2 using left join with table3, this way you will be able to know wich values where or not selected from the checkbox list
$sql = "SELECT rp.id, rp.name, IF(crp.customer_id IS NULL, 'NO', 'YES') AS checked FROM required_pages rp LEFT JOIN customers_required_pages crp ON rp.id = crp.required_page_id";
As an extra, consider use MySQLi or PDO instead of mysql as your database library, since the use of mysql lib has been discouraged
I was having the problem to store the multiple checkbox into the mysql table, but now it's working fine with this code:
<html>
<head>
<title>Registration Page</title>
</head>
<body>
<?php
require("config.php");
?>
<?php
if(isset($_POST['submit'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$gender = $_POST['gender'];
$hobbies = implode(",",$_POST['hobbies']);
echo $hobbies;
$country = $_POST['country'];
// echo $hobbies;
//foreach($hobbies as $hobbies1){
//echo $hobbies1;
//$implode = implode(',',$hobbies1);
$sql = "INSERT INTO `userinfo`(`fname`,`lname`,`gender`,`hobbies`,`country`)
values('$fname','$lname','$gender','$hobbies','$country')";
//}
//$sql = "INSERT INTO `userinfo`(`fname`,`lname`,`gender`,`hobbies`,`country`)
// values('$fname','$lname','$gender','$hobbies1','$country')";
$query = mysql_query($sql) or die(mysql_error());
if
($query){
echo "<h1>Data inserted .. Happy coding</h1>";
}else{
echo "<h1>Please check the problem</h1>";
}
}
//}
?>
<form name="registration" method="post" action="">
<table border="1" cellpadding="2" cellspacing="2" align="center">
<tr>
<td>First Name:</td><td><input type="text" name="fname" required=""></td>
</tr>
<tr>
<td>Last Name:</td><td><input type="text" name="lname" required=""></td>
</tr>
<tr>
<td>Gender:</td><td><input type="radio" name="gender" value="male" required="">Male <input type="radio" name="gender" value="female" >Female</td>
</tr>
<tr>
<td>Hobbies:</td><td><input type="checkbox" name="hobbies[]" value="tennis">Tennis <br>
<input type="checkbox" name="hobbies[]" value="cricket">Cricket <br>
<input type="checkbox" name="hobbies[]" value="dance">Dance7 <br>
<input type="checkbox" name="hobbies[]" value="sketching">Sketching
</td>
</tr>
<tr>
<td>Country:</td>
<td>
<select name="country">
<option >----select----</option>
<option>India</option>
<option>Pakistan</option>
<option>UAE</option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Register me"></td>
</tr>
</table>
</form>
</body>
</html>
Related
The data inserts into the first table, but the code for getting the ID numbers doesn't seem to work, and the data is not inserted into the next two tables.
The code runs and the Thank you message appears thanking the person for submitting their details.
There are three pages of code. The connect code is in one file. The processing code file and the form file.
I won't include the connect code here, because it works.
Here is the form code:
enter code here
<form method="post" action="formprocess3.php">
<table>
<tr>
<td>Customer Details</td>
<td>Appointment Preference</td>
<td>Cupcake Details</td>
</tr>
<tr>
<td>First Name
<input name="FirstName" type="text" id="FirstName" maxlength="20" value="<?php if (isset($_POST['FirstName'])) echo $_POST ['FirstName']; ?>"/>
</td>
<td>Appointment Date
<input name="AppointmentDate" type="date" id="AppointmentDate" maxlength="10" value="<?php if (isset($_POST['AppointmentDate'])) echo $_POST['AppointmentDate']; ?>"/>
</td>
<td>Size
<select name="CupcakeSize" id="CupcakeSize" type="radio" maxlength="5" value="<?php if (isset($_POST['CupcakeSize'])) echo $_POST['CupcakeSize']; ?>"/>
<option></option>
<option>Small</option>
<option>Large</option>
</select></td>
</tr>
<tr>
<td>Surname
<input name="Surname" type="text" id="Surname" maxlength="20" value="<?php if (isset($_POST['Surame'])) echo $_POST['Surname']; ?>"/></td>
<td>Appointment Time
<select name="AppointmentTime" type="radio" maxlength="20" value="<?php if (isset($_POST['AppointmentTime'])) echo $_POST ['AppointmentTime']; ?>"/>
<option></option>
<option>9.30am -10.30am</option>
<option>11am - 12pm</option>
<option>1.30pm - 2.30pm</option>
<option>3pm - 4pm</option>
<option>4.30pm - 5.30pm</option>
<option>7pm - 8pm</option>
</select>
</td>
<td>Quantity
<input type="text" name="Quantity" id="Quantity"/></td>
</tr>
<tr>
<td>Email address
<input name="EmailAddress" type="email" id="Email" maxlength="20" value="<?php if (isset($_POST['EmailAddress'])) echo $_POST['EmailAddress']; ?>"/></td>
<td>Taster
<input name="Taster" type="checkbox" id="Taster"/>
</td>
<td maxlength="1" type="radio" value="<?php if (isset($_POST['Taster'])) echo $_POST['Taster']; ?>"/>
<td>Frosting
<select name="CupcakeFrosting" id="CupcakeFrosting" type="radio" maxlength="10" value="<?php if (isset($_POST['CupcakeFrosting'])) echo $_POST['CupcakeFrosting']; ?>"/>
<option></option>
<option>Strawberry</option>
<option>Chocolate</option>
<option>Vanilla</option>
<option>Coffee</option>
<option>Orange</option>
<option>Blue</option>
<option>Pink</option>
<option>Green</option>
<option>Red</option>
<option>Purple</option>
</select></td>
</tr>
<tr>
<td>Postcode
<input name="Postcode" type="text" id="Postcode" style="width: 130px; height: 20px" class="auto-style24" maxlength="10" value="<?php if (isset($_POST['Postcode'])) echo $_POST['Postcode']; ?>"/></td>
<td>Cake wanted by
<input name="CakeWantedBy" type="date" id="CakeWantedBy" maxlength="10" value="<?php if (isset($_POST['CakeWantedBy'])) echo $_POST['CakeWantedBy']; ?>"/>
</td>
<td>
<select name="CupcakeFlavour" id="Flavour" type="radio" maxlength="10" value="<?php if (isset($_POST['CupcakeFlavour'])) echo $_POST['CupcakeFlavour']; ?>"/>
<option></option>
<option>Banana</option>
<option>Caramel</option>
<option>Carrot</option>
<option>Chocolate</option>
<option>Vanilla</option>
<option>Red Velvet</option>
<option>Oreo</option>
<option>Coffee</option>
<option>Decide with taster £20</option>
</select></td>
</tr>
<tr>
<td>
<input name="MobileNumber" type="text" id="MobileNumber" maxlength="20" value="<?php if (isset($_POST['MobileNumber'])) echo $_POST['MobileNumber']; ?>"/>
</td>
<td>
<span class="auto-style24">Occasion
<select name="Occasion" type="radio" id="Occasion" maxlength="20" value="<?php if (isset($_POST['Occassion'])) echo $_POST['Occassion']; ?>"/>
<option></option>
<option>New baby</option>
<option>Birthday</option>
<option>Wedding</option>
<option>New Job</option>
<option>Christmas</option>
<option>Easter</option>
<option>Valentines</option>
<option>Congratulations</option>
<option>Anniversary</option>
<option>Other</option>
</select></td>
</tr>
</table>
</form>
The code for inserting the form data into the three database tables:
<html>
<head>
<title>Form Process Message</title>
</head><body>
<?php #
// This script performs an INSERT query to add a record to the users table.
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// open the database...
require ('mysqli_connect.php');
// Make the query:
// Customer details
$t = $_POST[Title];
$fn = $_POST[FirstName];
$sn = $_POST[Surname];
$e = $_POST[EmailAddress];
$ht = $_POST[HomeTelephone];
$mn = $_POST[MobileNumber];
$hn = $_POST[HouseNumberName];
$s = $_POST[Street];
$tw = $_POST[Town];
$c = $_POST[County];
$pc = $_POST[Postcode];
// Cake details
$ct = $POST[CupcakeType];
$cn = $_POST[CupcakeNumber];
$cf = $_POST[CupcakeFrosting];
$o = $_POST[Occassion];
// Preferred Appointment
$ad = $_POST[AppointmentDate];
$at = $_POST[AppointmentTime];
$ta = $_POST[Taster];
$cwb = $_POST[CakeWantedBy];
$q = "INSERT INTO customerdetails(Title, FirstName, Surname, EmailAddress, HomeTelephone, MobileNumber, HouseNumberName, Street, Town, County, Postcode) VALUES ('$t','$fn', '$sn', '$e', '$ht', '$mn', '$hn', '$s', '$tw', '$c', '$pc')";
//execute query
$r = #mysqli_query ($dbc, $q);
//get customer id for preferred appointment
$ci = my_sqli_insert_id($dbc);
$q1 = "INSERT INTO cakedetail(CupcakeType, CupcakeNumber, CupcakeFrosting, Occassion) VALUES ('$ct','$cn', '$cf', '$o')";
//execute query
$r1 = #mysqli_query ($dbc, $q1);
//get cakedetail id for preferred appointment
$cdi = my_sqli_insert_id($dbc);
$q2 = "INSERT INTO preferredappointment(AppointmentDate, AppoitmentTime, Taster, CakeWantedBy, EmailAddress) VALUES ($ci, $cdi, '$ad','$at', '$ta', '$cwb', '$e')";
//execute query
$r2 = #mysqli_query ($dbc, $q2);
// Run the query.
if ($r) {
// If it ran OK.
// Print a message:
echo '<h1>Thank you!
<br />
Your request is now registered.
<br />
Back to the Gallery page</h1>';
}
else {
// If it did not run OK.
// Public message:
echo '<h1>System Error</h1>
<p class="error">You could not be registered due to a system error. We apologise for any inconvenience.</p>
Back to the Gallery page';
// Debugging message:
echo '<p>' . mysqli_error($dbc) . '<br /><br />
Query: ' . $q . '</p>';
}
//close the dbc
mysqli_close($dbc);
}
?>
</body>
</html>
There are three database tables called cakeorder, customerdetails and preferred appointment.
I don't think the multiple table insert works with earlier versions PHP, which is what I was using to start with, but I am now using xampp 5.5.24 and PHP 5.5.24.
I stripped out most the formatting of the html, so I may have left a hanging tag somewhere here, but there isn't one on the actual web page.
I am not very proficient in PHP, so a lot of this is put together from looking through this website.
Any help would be gratefully received.
Thank you
Thank you for your feedback. It's not for professional use, so I am not so worried about vulnerability it is just trying to get the stuff to work. As I said I don't know much about php code hence the mistake of using my_sqli_insert_id. It may be better to create a stored procedure, but I am just learning the basics at the moment.
my form data im listed my table sample 10 records.
<td class='table-checkbox'><input type="checkbox" name="product[]" value="1" class='selectable-checkbox'></td>
<td><div class="input-mini">
<input type="text" id="spinnn" name="quantity[]" value="5" class='spinner'/>
</div>
</td>
</tr>
.....
.....
my php code
$carino = $_POST['carino'];
$quantity = $_POST['quantity'];
$product = $_POST['product'];
foreach($product as $product){
foreach($quantity as $quantity ){
$sql = "INSERT INTO mytable (product,quantity,cno) VALUES ('$product','$quantity','$carino')";
mysql_query($sql);
}}
i want to be insert this data my table. but my foreach is wrong how can i do ?
product is unique
my table
product - quantity - cno
1 5 2
2 10 2
http://pastie.org/private/nwrrinnxlrumt6p3kuyq#11,13-14,19
This:
<?php
if(isset($_POST['submit'])) {
$carino = "2";
$adet = $_POST['adet'];
$urunno = $_POST['urunno'];
$total = count($_POST['adet']);
echo '<hr>';
foreach ($urunno as $checked)
{
$value = $checked - 1;
echo "Value of Urunno: $checked: Adet: $adet[$value] <br>";
echo "INSERT INTO member_interests
(`urun`,`adet`,'carino')
VALUES
('$checked','$adet[$value]','$carino')<br>";
}
}
?>
<tr>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<td class='table-checkbox'><input type="checkbox" name="urunno[]" value="1">Product One</td>
<input type="text" id="spinnn" name="adet[]" class='spinner'/>
<td class='table-checkbox'><input type="checkbox" name="urunno[]" value="2">Product Two</td>
<input type="text" id="spinnn" name="adet[]" class='spinner'/>
<td class='table-checkbox'><input type="checkbox" name="urunno[]" value="3">Product Three</td>
<input type="text" id="spinnn" name="adet[]" class='spinner'/>
<td><div class="input-mini">
<input type="submit" name="submit" value="run" />
</form>
</div>
</td>
</tr>
</tbody>
</table>
</div>
Output:
Value of Urunno: 2: Adet: 2
INSERT INTO member_interests (`urun`,`adet`,'carino') VALUES ('2','2','2')
Value of Urunno: 3: Adet: 3
INSERT INTO member_interests (`urun`,`adet`,'carino') VALUES ('3','3','2')
The logic can be as follows (think of the code yourself, or search on the internet):
Make sure that all POST arrays are the same length (they should be)
Loop over the count from 0 to the length of the arrays
Insert the value at that point in each of the arrays into the database.
A quick tip: you are very susceptible to SQL injection. If this is production code, either use prepared queries, or use a PHP database wrapper like PDO to do it for you. The mysql_... functions are deprecated.
Try this one
<?php
//add the database connection
?>
<form name="myform" action="">
<table>
<?php
$length=//how many time below check box repeat in this page
for($i=0;$i<$length;$i++){?>
<tr>
<td class='table-checkbox'>
<input type="checkbox" name="urunno[]" value="<?php echo $i;?>" class='selectable-checkbox'>
</td>
<td>c1k</td>
<td>2147483647</td>
<td>520</td>
<td>435345345 A.Ş.</td>
<td>
<div class="input-mini">
<input type="text" id="spinnn" name="adet<?php echo $i;?>" class='spinner'/>
</div>
</td>
</tr>
<?php
}?>
</table>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_post['submit']))
{
$carino = $_POST['carino']; //here this element not present in question, i am also follow this
$adet = $_POST['adet'];
$urunno = $_POST['urunno'];
$length=sizeof($urunno);
for($i=0;$i<$length;$i++)
{
$urun1=$urunno[$i];
$adet1=$adet[$urun1];
$sql = "INSERT INTO table (urunno,adet,cno) VALUES ('$urun1','$adet1','$carino')";
mysql_query($sql);
//the above code insert sql work in every time //otherwise use batch insert
//refrer this link http://stackoverflow.com/questions/12960569/mysql-bulk-insert-via-php
}
}?>
I have a classrooms in schools and when I click on a certain classroom, I want to add students into it but my actual code is doing something stupid. It adds a student but i can see the student in all classrooms, not just in the one that i added him into. So when Im in classroom number 1, I see a form in there, I can add a student there, ... see how it works here:
here is the code: http://www.xxxx.xx/projekt/
here is my code in file trieda.php
<table align="center"><tr><td>
<form action="vlozit2.php" method="post">
Meno: <input type="text" name="meno" placeholder="Janko" maxlength="15" required>
Priezvisko: <input type="text" name="priezvisko" placeholder="Hruška" maxlength="20" required>
<input type="hidden" name="id_triedy" value="<?= $trieda['id_triedy'] ?>" />
<input type="submit" name="submit" value="Pridať študenta do triedy">
</form>
</td></tr></table>
<?php
$result = mysqli_query($prip,"SELECT * FROM student ORDER BY meno");
while($student = mysqli_fetch_array($result))
{
echo "<br /><table cellspacing='1' cellpadding='1' class='tabulka1' align='center'><tr>";
echo "<td width='200'><a href='student.php?id_triedy=".$trieda['id_triedy']."".id_student=".$student['id_student']."'>".$student['meno']." ".$student['priezvisko']."</a></td>";
?>
<td width='300px' align='right' bgcolor="#fbfbfb">Zmazať</td>
</tr></table>
<?php
}
?>
here is vlozit2.php (a code that works for the form to add a student)
if(isset($_POST['submit']))
{
//meno a priezvisko
$student = $_POST['meno'];
$student = $_POST['priezvisko'];
$trieda = $_POST['id_triedy'];
//connect to the database
include 'config.php';
//insert results from the form input
$sql = "INSERT INTO student (meno, priezvisko, id_triedy) VALUES('$_POST[meno]', '$_POST[priezvisko]', '$_POST[id_triedy]')";
$add = "<table align='center'>
<tr>
<td> Študent bol úspešne pridaný do triedy. </td>
</tr>
<tr>
<td><a href='./trieda.php'><strong>Späť</strong></a></td>
</tr>
</table>";
$not_add = "<table align='center'>
<tr>
<td> Študent s týmto menom a priezviskom už je v tejto triede. </td>
</tr>
<tr>
<td><a href='./trieda.php'><strong>Späť</strong></a></td>
</tr>
</table>";
if (mysqli_query($prip, $sql)) {
echo $add;
}else{
echo $not_add;
}
mysqli_close($prip);
}
?>
Try to replace your part of code with these snipets:
1) in trieda.php
<form action="vlozit2.php?id_triedy=<?php echo $_GET["id_triedy"];?>" method="post">
Meno: <input type="text" name="meno" placeholder="Janko" maxlength="15" required>
Priezvisko: <input type="text" name="priezvisko" placeholder="Hruška" maxlength="20" required>
<input type="submit" name="submit" value="Pridať študenta do triedy">
</form>
2) in vlozit2.php
$student = $_POST['meno'];
$priezvisko = $_POST['priezvisko'];
$id_trieda = $_GET['id_triedy'];
and
$sql = "INSERT INTO student (meno, priezvisko, id_triedy) VALUES( '{$student}', '{$priezvisko}', {$id_trieda} )";
Hopefully you store your id_trieda as INT type.
In your vlozit2.php file is nothing about inserting of class id. So put
<input type="hidden" name="classId" value="<?= $trieda['id'] ?>" />
to your form and in vlozit2.php get this value from $_POST['classId'] and insert it with other students data or anywhere you want to have it.
So I'm using a multiple checkboxs in a form and query the database with no problems. Now I want to add a dropdown list and combine it with the query with the checkboxs. When I run this I get a
Warning: Invalid argument supplied for foreach()
Here is my form
<form name="search_form" method="post" action="event_list">
<table>
<tr>
<td>Venue</td>
<td><input type="checkbox" name="check_list[]" value="AAC"><label >AAC</label><br />
<input type="checkbox" name="check_list[]" action="" value="AKC" id="check_list[]"/><label >AKC</label><br />
<input type="checkbox" name="check_list[]" action="" value="ASCA"/><label >ASCA</label><br />
<input type="checkbox" name="check_list[]" action="" value="CKE"/><label >CKE</label><br />
<input type="checkbox" name="check_list[]" action="" value="CPE"/><label >CPE</label><br />
<input type="checkbox" name="check_list[]" action="" value="DOCNA"/><label >DOCNA</label><br />
<input type="checkbox" name="check_list[]" value="NADAC"><label >NADAC</label><br />
<input type="checkbox" name="check_list[]" action="" value="TDAA"/><label >TDAA</label><br />
<input type="checkbox" name="check_list[]" action="" value="UKC"/><label >UKC</label><br />
<input type="checkbox" name="check_list[]" action="" value="UKI"/><label >UKI</label><br />
<input type="checkbox" name="check_list[]" action="" value="USDAA"/><label >USDAA</label><br />
</td>
</tr>
<tr>
<td style="text-align: left;">State:</td>
<td style="text-align: left;">
<select name="state" id="state" onChange="setCities();">
<option name="drop_down[]" value="Alabama">Alabama</option>
<option name="drop_down[]" value="Texas">Texas</option>
</select>
</td>
</tr>
<td align="right"><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>
Here is my PHP
<?php
$options = Array();
$options2 = Array();
foreach ($_POST['check_list'] as $check)
$options[] = 'venue=' . "'$check'";
foreach ($_POST['drop_down'] as $drop)
$options2[] = 'state=' . "'$drop'";
$sql = "SELECT * FROM venue_event_all";
if (count($options))
$sql .= " WHERE " . implode(' OR ', $options);
if (count($options2))
$sql .= " AND " . implode(' OR ', $options2);
$result=mysql_query($sql) or die(mysql_error());
?>
In a <select> input, the value of <option> is sent paired with the name of the select to POST/GET.
Setting a name on the option itself will not do anything. The dropdown value will still go with the name of the select.
You can check it by using this
echo $_POST['state'];
So, use this value to set into your query.
Edit : Obligatory - Please don't use MySQL as it is deprecated and not maintained by PHP any more. Use MySQLi, or PDO. Also, please sanitize your inputs before using them in the query. This is what happens when you don't.
foreach will show you error, if array is empty or is not set, so you need to check it first:
if(isset($_POST[0]) and count($_POST) > 0)
{
foreach...
}
I won't just rant about sql injection but give you a solution.
The only way to have your query safe with sensible amount of code is safeMysql library
(Based on Jeffrey code, not tested, but use in production freely):
<?php
include 'safemysql.class.php';
$db = new SafeMysql();
$options = array();
if (!empty($_POST['check_list'])) {
$options[] = $db->parse('venue IN (?a)', $_POST['check_list']);
}
if (!empty($_POST['drop_down'])) {
$options[] = $db->parse('state IN (?a)', $_POST['drop_down']);
}
$sql = "SELECT * FROM venue_event_all";
if ($options) {
$sql .= " WHERE " . implode(' AND ', $options);
}
$data = $db->getAll($sql);
I get the values from same row, but many values with fetch array. I did it, but with buttons, as many as how many values there are. If I have 5 rows, then I should have five submits, but I want one submit button.
Here is my code:
$result2 = mysql_query ("select * from price where dom='$cat'",$db);
$myrow2= mysql_fetch_array($result2);
<form action="priceupdatetes.php" method="post">
<?php
do {
echo <<<here
<td><input name="etiket[$myrow2[id]]" type="text" value="$myrow2[etiket]"/></td>
<td><input name="pricestandart[$myrow2[id]]" type="text" value="$myrow2[pricestandart]"/></td>
<td><input name="number[$myrow2[id]]" type="text" value="$myrow2[number]"/></td>
<td><input name="totalunper[$myrow2[id]]" type="text" value="$myrow2[totalunper]" disabled="disabled"/></td>
<td><input name="discount[$myrow2[id]]" type="text" value="$myrow2[discount]"/></td>
<td><input name="totalwithper[$myrow2[id]]" type="text" value="$myrow2[totalwithper]" disabled="disabled"/></td>
</tr>
here;
}
while($myrow2= mysql_fetch_array($result2)) ;
?>
<input NAME="id[]" TYPE=hidden value="<?php foreach($myrow2[id] as $mid) {print $mid;} ?> "/>
<input name="submit" type="submit" value="Submit"/><br>
</form>
HERE is UPDATEPAGE.php:
if (isset($_POST['etiket'])) {$etiket = $_POST['etiket']; }
if (isset($_POST['pricestandart'])) {$pricestandart = $_POST['pricestandart'];}
if (isset($_POST['number'])) {$number = $_POST['number']; }
if (isset($_POST['discount'])) {$discount = $_POST['discount']; }
if (isset($_POST['id'])) {$id = $_POST['id']; }
$totalunper=$pricestandart*$number;
$percent=$discount/100;
$totalwithper1=$totalunper*$percent;
$totalwithper=$totalunper-$totalwithper1;
foreach($id as $team_id)
{
$result = mysql_query("UPDATE price SET etiket='$etiket[$team_id]',pricestandart='$pricestandart[$team_id]',number='$number[$team_id]',totalunper='$totalunper[$team_id]',discount='$discount[$team_id]',totalwithper='$totalwithper[$team_id]' WHERE id='$team_id'"); }
How can I get values with different id's and update them?
<input NAME="id[]" TYPE=hidden value="<?php foreach($myrow2[id] as $mid) {print $mid;} ?> "/>
is wrong. $myrow2[id] is not an array. Inside your while-loop, you should add:
<input NAME="id[]" TYPE="hidden" value="$myrow2[id]"/>