i have this code which permits me to retrieve data from a checkbox, there isn't any mysql included yet, so can you help me modify this?
The form is like this:
<form action="checkbox.php" method="post">
<input type="checkbox" name="checkbox[]" value="hostess_name">
<input type="checkbox" name="checkbox[]" value="hostess_familyname_en">
<input type="checkbox" name="checkbox[]" value="hostess_id">
<input type="checkbox" name="checkbox[]" value="hostess_firstname_en">
<br>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
The values i inserted are supposed to be database fields, then i have this checkbox.php file which reads the values selected.
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
echo "Connected to MySQL<br />";
?>
<?
/* and in your checkbox.php you do this: */
if(isset($_POST['Submit']))
{
echo "<pre>"; print_r($_POST);
}
$checked = mysql_real_escape_string(implode(',', $_POST['checkbox']));
echo $checked;
?>
How can assign to checkbox values database fields?
What i'm trying to do is to store the values and export them into a query using implode..
Help me..
Your POST variable ($_POST['checkbox']) is actually already an array. First, to figure out what you are actually working with, do this:
echo '<pre>';
print_r ($_POST['checkbox']);
echo '</pre>';
Then view your script and have a look at the output. Chances are you'll see an array with some keys and values. Using that you can decide how to proceed.
If it were me I would do something like the following to accomplish your task:
$sql = "SELECT `table_id_column`, `another_column` ";
foreach ($_POST['checkbox'] as $key => $value) {
$sql .= ", `$value`";
}
$sql .= " FROM `hostess` ORDER BY `another_colmn` ASC";
Please keep in mind that allowing an SQL statement to be modified in this manner is very bad practice. You'll want to introduce some security into this before putting it on a production environment.
Luke
Use
foreach($_POST[checkbox] as $key => $value {
echo PHP_EOL . "Key is => " . $key . " Value is => " . $value . PHP_EOL;
}
Try this and see the output, you'll see yourself how to proceed further.
Related
I'm new to php and MySQL (working through "Head First: PHP & MySQL"). I'm trying to grab a MySQL table column name from an array and store it in the "value" for an input field of a form. For some reason, I only end up with a blank page. When I simply type in some text into the 'value', the page turns out fine.
I've checked every line of code and have narrowed it down to the input value. The database connection works and I can echo the column name in the 'while' loop, but not in the 'value'.
Here's the code:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?> ">
<?php
$dbc= mysqli_connect('host', 'user', 'pass', 'elvis_store')
or die ('Error connecting to the database');
$query= "SELECT * FROM elvis_emails";
$result= mysqli_query($dbc, $query)
or die ('Error querying database');
while ($row= mysqli_fetch_array($result)){
echo '<input type="checkbox" value="'$row['Id']'" name="todelete[]" />';
echo $row['Id'];
echo ' ' . $row['first_name'];
echo ' ' . $row['last_name'];
echo ' ' . $row['email'];
echo '<br />';
}
mysqli_close($dbc);
?>
<input type="submit" name="submit" value="Remove" />
</form>
Thank you!
instead of,
echo '<input type="checkbox" value="'$row['Id']'" name="todelete[]" />';
try like this
echo '<input type="checkbox" value="'.$row["Id"].'" name="todelete[]" />';
What is really breaking the code is the missing of proper concatenation.
It's good practice to avoid printing HTML elements with PHP echo statement. Rather, the best approach in this case would be:
<?php while ($row = mysqli_fetch_array($result)){ ?>
<input type="checkbox" value="<?=$row['Id']?>" name="todelete[]" />
<?php
...
}
mysqli_close($dbc);
?>
If by anyways you do need to print a piece of code that is relying on HTML tags and PHP variables you can make use of HEREDOC instead of using echo to print multiple lines of code.
Also, make sure that you are naming the keys to the row array the
same as your query return values.
Trying to get this to allow for more then 1 check box to be selected.
Form
<fieldset>
<legend>Rooms</legend>
<ol>
<li>
<label for =youthCafe>Youth Cafe</label>
<input type="checkbox" name="roomid[]" value="1" ><br>
<label for =inkwellMain>Inkwell Main</label>
<input type="checkbox" name="roomid[]" value="2"><br>
<label for =inkwellSmall>Inkwell Small</label>
<input type="checkbox" name="roomid[]" value="3"><br>
<label for =kitchen>Kitchen</label>
<input type="checkbox" name="roomid[]" value="4"><br>
<label for =outsideCatering>Outside Catering</label>
<input type="checkbox" name="roomid[]" value="5"><br>
</li>
</ol>
</fieldset>
PHP
mysql_select_db('eydg');
$query = "insert into orders (customerNo)
values ($customerNo)";
$result = mysql_query($query);
$query = "select * from orders where customerNo = '$customerNo'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$bookingNo= $row['bookingNo'];
if ( isset($_POST['roomid']) ){
foreach( $_POST['roomid'] as $value ){
$query = "insert into bookings (bookingNo,roomNo)
values ('$bookingNo','$value')";
$result = mysql_query($query);
}
}
At the moment it will only allow for 1 selection to be added to the database. They all work on their own but if more then 1 is selected then it only inserts the 1st one.
Really cant see what is wrong with it.
Thanks
Sorry, but I can't comment yet.
How many checkboxes have you checked?
Most browsers do not submit unchecked checkboxes.
Also, please provide output of var_dump($_POST) or var_dump($_POST['roomid'])
Note :
the mysql extension is deprecated as of PHP 5.5.0, and will be removed
in the future. Instead, the MySQLi or PDO_MySQL extension should be
used.
You code is ok for me.
I advise you to use xdebug to watch your code running, this will clearly tell you where is the trouble.
But a first step can be to see what this code returns :
foreach( $_POST['roomid'] as $key => $value ){
echo "\n<br>\n<br>array key=" . $key . ' : value=' . $value;
$query = "insert into bookings (bookingNo,roomNo) values ('$bookingNo','$value')";
echo "\n<br>query" . ' : ' . $query;
$result = mysql_query($query, $link);
if(!$result){
echo "\n<br><h1>Error : " . mysql_errno($link) . ": " . mysql_error($link) . '</h1>';
}
}
Here $link is the var holding you connection. So replace this variable before running this code.
I am unable to insert the data entered in the text box into MYSQL database. I am trying to insert inputs from multiple textboxes into the database.
<?php
include('questionDB.php');
if(isset($_POST['submit'])){
$questionID = $_POST['id'];
$answer = mysql_real_escape_string(htmlspecialchars($_POST['answer']));
mysql_query("INSERT INTO answers(question_id, answer_body) VALUES ($questionID, $answer)");
}
?>
<form name="auctionQuestion" method="post">
<?php
$auctionSurvey = "SELECT question_id, survey_id, question_body FROM questions
WHERE survey_id='1'";
$aucResult = mysql_query($auctionSurvey) or die (mysql_error());
while($auctionRow = mysql_fetch_assoc($aucResult)){
echo $auctionRow['question_body']. "<input type=\"text\" name=\"answer\"><BR>";
?>
<input type="hidden" name="id" value="<?php echo $auctionRow['question_id'] ?>">
<?php
}
?>
<input type="submit" name="submit" value="Submit">
</form>
Use either:
VALUES ('$questionID', '$answer')
or:
VALUES ('" . $questionID . "', '" . $answer . "')
instead of VALUES ($questionID, $answer)
First thing to do is to generate the correct HTML, so that you will get back arrays of variables.
I think it will be something like this
echo $auctionRow['question_body'] . "<input type=\"text\" name=\"answer[]\"><BR>";
Similar for the other fields.
Then, when you get the post in, use print_r($_POST) to see exactly what you have got. Go from there.
A friend told my you can grab stuff from a general $_POST array, but I'm not getting it.
What I have are some values with unique row IDs pulled from a MySQL DB. I have them displayed so they can be updated if necessary en masse. I've given each input form a unique name by concat'ing the MySQL value, but I'm unsure how to grab those values in the script that accepts the $_POST values.
<form action"./" method="POST">
<?php
while($select_row = mysql_fetch_array($select_query))
{
echo $select_row['name'];
echo "</br>";
echo "Week ".$select_row['week'];
echo "</br>";
echo "Hours: ";
?>
<input class="options" type="text" name="hours<?php echo $select_row['submission_id']?>" value="<?php echo $select_row['hours']; ?>" />
<?php
echo "</br></br>";
?>
<input type="submit" value="Submit Hours">
</form>
<?php
}
?>
As you can see, each input will get a name like hours13 or hours144. Without knowing what they will be named beforehand, how can I extract them from $_POST?
Just iterate through the collection:
foreach($_POST as $key=>$value)
{
if($value != '')
{
echo "$key: $value\n";
}
}
while ($row= mysql_fetch_array($result, MYSQL_ASSOC))
{ $id=$row[id];
$html=<<<html
<tr><td>
<input style="float:left" type="checkbox" name="mycheckbox" value="$id">
<span style="float:left">$row[content]</span>
<span style="color:black;float:right">$row[submitter]</span></td></tr>
html;
echo $html;
}
Since the HTML code is generated dynamically, I don't know how long the array "mycheckbox" is and I don't know what checkboxes are ticked or unticked(This is determined by users). How to retrieve the values of ticked checkboxes using PHP?
The way you have it now, mycheckbox will get overwritten and act more like a radio button.
You probably want:
<input style="float:left" type="checkbox" name="mycheckbox[]" value="$id">
PHP will push the checked values into an array: $_GET['mycheckbox']
<?php
$values = $_GET['mycheckbox'];
$count = count($values);
echo 'Selected values are: <br/>';
foreach($values as $val) {
echo $val . '<br/>';
}
echo 'Total Length is: ' . $count . '<br/>';