how to make a checkbox selected automatically? - php

i want to check the checkbox automatic, i tried with below code but its not checking the checkbox
HTML
<?php
$dbHost = 'localhost'; // usually localhost
$dbUsername = 'xxxx';
$dbPassword = 'xxxxxxxxxxx';
$dbDatabase = 'xxxxxxxxxx';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");
$variable=$_POST['chk1'];
//print $variable;
$checkbox=implode(',',$variable);
//print $checkbox;
$sql_selectcpnn="SELECT * from clientnetworkpricehistory where id in ($checkbox)";
$querycpnn = mysql_query($sql_selectcpnn);
//print$sql_selectcpnn;
echo "<table style='margin:0px;width:364px'>
<tr>
<th>DateTime</th>
<th>By(employee)</th>
</tr>";
while($row = mysql_fetch_array($querycpnn))
{
$clientid=$row['clientid'];
//print$clientid;
$net=$row['net_id'];
//print$net;
$id=$row['id'];
// print$id;
echo "<tr>";
echo "<td>" .date('d.m.Y H:i', $row['datetime']) . "</td>";
echo "<td>" . $row['user'] . "</td>";
echo"<input type='checkbox' name='chk1[]' value= '$id' checked='checked'/>";
echo "</tr>";
}
echo "</table>";
?>
can anyone tell me what would be my code to show a checkboxs as selected.thanks

Try-
echo "<input type='checkbox' name='chk1[]' checked='checked' value= '$id' />";

Related

How to enter multiple data at once for students in php

It is showing all the students in database and textbox with them. But when I enter marks of every student it picks only the first record and save only marks.
I want:
all name of student should print on page with text-box in which I will enter marks of each student.
I want to save the record of every student with their marks.
--
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT * FROM stunr WHERE stucou = '".$_POST["cls"]."'";
mysql_select_db('college-db');
$retval = mysql_query( $sql, $conn );
if($retval === FALSE) {
die(mysql_error());
}
$row = mysql_fetch_array($retval);
echo "<div style='margin-top:120px; margin-left:180px;'>";
$cla=$row['stucou'];
echo "<h3>Class"."<b> $cla</b>"."</h3>";
echo"</div>";
echo "<div style='margin-top:50px; margin-left:180px;'>";
while($row = mysql_fetch_array($retval)) {
echo"<form method='post' action='mst-marks-insert.php'>";
$clsnm = $row['stufname'];
echo"$clsnm";
echo "<br>";
echo"<input type='text' name='mas' class='textbox' placeholder='Enter Marks'/><br><br>";
}
echo "<input type='text' name='tot' />";
echo"<input type='submit' />";
echo"</form>";
echo"</div>";
I would start with moving the beginning form tag before the while loop.
echo"<form method='post' action='mst-marks-insert.php'>";
while($row = mysql_fetch_array($retval))
{
Do something like this:
Move out the beginning form before while loop
Make field "mas" an array
This could look like this
<?php
mysql_select_db('college-db');
$retval = mysql_query($sql, $conn);
if ($retval === FALSE) {
die(mysql_error());
}
$row = mysql_fetch_array($retval);
echo "<div style='margin-top:120px; margin-left:180px;'>";
$cla = $row['stucou'];
echo "<h3>Class" . "<b> $cla</b>" . "</h3>";
echo"</div>";
echo "<div style='margin-top:50px; margin-left:180px;'>";
echo "<form method='post' action='mst-marks-insert.php'>";
while ($row = mysql_fetch_array($retval)) {
$clsnm = $row['stufname'];
echo "$clsnm";
echo "<br>";
echo "<input type='text' name='mas[$clsnm]' class='textbox' placeholder='Enter Marks'/><br><br>";
}
echo "<input type='text' name='tot' />";
echo "<input type='submit' />";
echo "</form>";
echo "</div>";
?>
In your "mst-marks-insert.php" the $_POST var then looks something like this:
<?php
var_dump($_POST['mas']);
array (
'clsnm1' => 'test',
'clsnm2' => 'test2',
)
?>
So you could do something like this (it's just an example what would be possible, because I don't know how your SQL table looks like):
<?php
foreach ($_POST['mas'] as $clsnm => $mas) {
mysql_query("INSERT INTO `mastable` (`clsnm`, `mas`) VALUES ('".mysql_real_escape_string($clsnm)."', '".mysql_real_escape_string($mas)."')");
}
?>

Undefined Index inside a While PHP

I get an error of Undefined index, what is the reason?
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['CandidateName'] . "</td>";
echo "<td>" . $row['Position'] . "</td>";
echo "<td><input type='radio' name='candidateid' value='".$row['candidateID']."' >";
echo "<td>" . $row['NumberofVotes'] . "</td>";
$candidateid=$row['CandidateID'];
}
Here is the error
Array ( [0] => 1 [CandidateID] => 1 [1] => Jejomar Binay [CandidateName] => Jejomar Binay [2] => President [Position] => President [3] => [NumberofVotes] => ) Array ( [0] => 2 [CandidateID] => 2 [1] => Mar Roxas [CandidateName] => Mar Roxas [2] => President [Position] => President [3] => 1 [NumberofVotes] => 1 )
I will show you now the whole code and the its working now, my output here is to add 1 in number of votes when the radio button is working. it has no error but when i selected the first radio button it only updates the second data.
<html>
<center>
<font size="2" face = "century gothic">
<?php
$con=mysqli_connect("localhost","root","","election2016");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM candidate_info");
echo "<table border='1'>
<tr>
<th>Candidate Name</th>
<th>Position</th>
<th>Vote</th>
<th>Number of Votes</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['CandidateName'] . "</td>";
echo "<td>" . $row['Position'] . "</td>";
echo "<td><input type='radio' name='candidateid' value='".$row['CandidateID']."' >";
echo "<td>" . $row['NumberofVotes'] . "</td>";
$candidateID=$row['CandidateID'];
}
echo "</table>";
mysqli_close($con);
?>
<br>
<br>
<form method = "post" action = "<?php $_PHP_SELF ?>">
<input type="text" name="candidateid" value="<?php echo $candidateID;?>">
<input name = "update" type = "submit" id = "update" value = "update">
</form>
</center>
</font>
</html>
<?php
if(isset($_POST['update'])) {
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$candidateid = $_POST['candidateid'];
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$candidateid = $_POST['candidateid'];
$sql = "UPDATE candidate_info SET numberofvotes = 1 WHERE candidateid = '$candidateid'" ;
mysql_select_db('election2016');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);
}
?>
The field "candidateid" should be integer data type, but you are enclosed this field value with ''(single quotes) in the update query?
$sql = "UPDATE candidate_info SET numberofvotes = 1 WHERE candidateid = '$candidateid'";
if it is an integer datatype then you should remove the single quote
$sql = "UPDATE candidate_info SET numberofvotes = 1 WHERE candidateid = $candidateid";
and in MySQL every field names are case sensitive, so as you told the field names are
candidateid, candidatename, position, numberofvotes
so, you should use these names when you retrieving the values as well
<?php
if(isset($_POST['update'])) {
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$candidateid = $_POST['candidateid'];
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$candidateid = $_POST['candidateid'];
$sql = "UPDATE candidate_info SET numberofvotes = numberofvotes + 1 WHERE candidateid = '$candidateid'" ;
mysql_select_db('election2016');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);
}
?>
<html>
<center>
<font size="2" face = "century gothic">
<?php
$con=mysqli_connect("localhost","root","","election2016");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM candidate_info");
?>
<form method = "post" action = "<?php $_PHP_SELF ?>">
<?php
echo "<table border='1'>
<tr>
<th>Candidate Name</th>
<th>Position</th>
<th>Vote</th>
<th>Number of Votes</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['candidatename'] . "</td>";
echo "<td>" . $row['position'] . "</td>";
echo "<td><input type='radio' name='candidateid' value='".$row['candidateid']."' >";
echo "<td>" . $row['numberofvotes'] . "</td>";
}
echo "</table>";
mysqli_close($con);
?>
<br>
<br>
<input name = "update" type = "submit" id = "update" value = "update">
</form>
</center>
</font>
</html>
there is no key in your array $row as 'candidateid' please do var_dump($row); and see what is the key name, or if these are just same as your column name in your DB table, check the name of it.
Points To Be Noted :
No Need to create separate <form></form> for sending candidateid for updating numberofvotes.
If you are submitting in same page then avoid multiple database connection.
Your database table candidate_info field name is not matching with what you wrote in <table><tr></tr></table>. So, use exact column name what is there in database table.
Put your complete <table></table> inside <form></form>.
Since you are looking for single candidate value to get get updated, so radio button is helpful. If multiple candidate value need to be updated, then you have to use checkbox with name as array type.
Updated Code:
<html>
<center>
<font size="2" face = "century gothic">
<?php
$con=mysqli_connect("localhost","root","","election2016");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<form method="post" action = "<?php $_PHP_SELF ?>">
<?php
$result = mysqli_query($con,"SELECT * FROM candidate_info");
echo "<table border='1'>
<tr>
<th>Candidate Name</th>
<th>Position</th>
<th>Vote</th>
<th>Number of Votes</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['candidatename'] . "</td>";
echo "<td>" . $row['position'] . "</td>";
echo "<td><input type='radio' name='candidateid' value='".$row['candidateid']."' ></td>";
echo "<td>" . $row['numberofvotes'] . "</td>";
$candidateID=$row['candidateid'];
}
echo "</table>";
mysqli_close($con);
?>
<br>
<br>
<input name = "update" type = "submit" id = "update" value = "update">
</form>
</center>
</font>
</html>
<?php
if(isset($_POST['update'])) {
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$candidateid = $_POST['candidateid'];
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$candidateid = $_POST['candidateid'];
$sql = "UPDATE candidate_info SET numberofvotes = 1 WHERE candidateid = '$candidateid'" ;
mysql_select_db('election2016');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);
}
?>

How to Update a Data in MySQL using Radio Button?

Hello Everyone Good Afternoon
Can I ask a question? but before that here is my code.
<html>
<center>
<font size="2" face = "century gothic">
<?php
$con=mysqli_connect("localhost","root","","election2016");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM candidate_info");
echo "<table border='1'>
<tr>
<th>Candidate Name</th>
<th>Position</th>
<th>Vote</th>
<th>Number of Votes</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['CandidateName'] . "</td>";
echo "<td>" . $row['Position'] . "</td>";
echo "<td><input type='radio' name='candidateid'/>";
echo "<td>" . $row['NumberofVotes'] . "</td>";
}
echo "</table>";
mysqli_close($con);
?>
<br>
<br>
<form method = "post" action = "<?php $_PHP_SELF ?>">
<input name = "update" type = "submit" id = "update" value = "Update">
</form>
</center>
</font>
</html>
<?php
if(isset($_POST['update'])) {
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$candidateid = $row['candidateid'];
$sql = "UPDATE candidate_info SET numberofvotes = '1' WHERE candidateid = $candidateid" ;
mysql_select_db('election2016');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);
}
?>
The Output of my Code Here is it will show list of Candidates,Position,Radio Button Number of Votes with a button save.
My error here is that when i select a radio button and click the button update i want to put 1 in numberofvotes field but its not updating. Whats wrong with my code?
Any help would be appreciated.
TY so much
<html>
<center>
<font size="2" face = "century gothic">
<?php
$con=mysqli_connect("localhost","root","","election2016");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM candidate_info");
echo "<table border='1'>
<tr>
<th>Candidate Name</th>
<th>Position</th>
<th>Vote</th>
<th>Number of Votes</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['CandidateName'] . "</td>";
echo "<td>" . $row['Position'] . "</td>";
echo "<td><input type='radio' name='candidateid' value='".$row['candidateid']."' >";
echo "<td>" . $row['NumberofVotes'] . "</td>";
$candidateid=$row['candidateid'];
}
echo "</table>";
mysqli_close($con);
?>
<br>
<br>
<form method = "post" action = "<?php $_PHP_SELF ?>">
<input type="hidden" name="candidateid" value="<?php echo $candidateid;?>">
<input name = "update" type = "submit" id = "update" value = "update">
</form>
</center>
</font>
</html>
<?php
if(isset($_POST['update'])) {
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$candidateid = $_POST['candidateid'];
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$candidateid = $_POST['candidateid'];
$sql = "UPDATE candidate_info SET numberofvotes = 1 WHERE candidateid = '$candidateid'" ;
mysql_select_db('election2016');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);
}
?>

PHP update textbox when value of dropdown is changed

I filled my dropdown menu with data from the database. I can select here the name of the product. I have a second textbox on my page. Here i want to show the price of the selected product. I tried different ways but I o net get it working.
First of all this is my dropdown menu where the product name is showed:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM form";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<select class='form-control select2' id='product1' name='product1' style='width: 100%;'>";
echo "<option selected disabled hidden value=''></option>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option onchange='OnChange()' value='" . $row["id"]. "'>" . $row["name"]. "</option>";
}
echo "</select>";
} else {
echo "0 results";
}
$conn->close();
?>
I want to show the price of the selected value in this textbox:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbsi";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT price FROM form WHERE id='". $product1 ."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<input type='text' class='form-control' name='price1' id='price1' onkeyup='getValues()' value='" . $row["price1"]. "'>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Using this script i only get "0 results" in price1. It doesnt update when i change the selected value of the dropdown menu. How can I ensure that price1 gets updated when i select another value in the dropdown menu?
Update 1:
I tried to add the the following script
<script>
function OnChange(){
UpdatePoints(<?php echo $price1; ?>);
}
echo "<script type='text/javascript'> window.onchange=load; </script>";
</script>
When I use this script i still get "0 results"
Update 2:
The following is still not working:
//filename: test.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM forms";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<select class='form-control select2' id='product1' name='product1' onChange='getstate(this.value);' style='width: 100%;'>";
echo "<option selected disabled hidden value=''></option>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row["id"]. "'>" . $row["name"]. "</option>";
}
echo "</select>";
} else {
echo "0 results";
}
$conn->close();
?>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM forms WHERE id='". $product1 ."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<div id='price-list'>";
echo "<input type='text' class='form-control' name='price1' id='price1' value='" . $row["price"]. "'>";
echo "</div>";
}
} else {
echo "0 results";
}
$conn->close();
?>
<script>
function getprice(val) {
$.ajax({
type: "POST",
url: "test.php",
data:'id='+val,
success: function(data){
$("#price-list").html(data);
}
});
}
</script>
<?php
$product1=$_POST['price1'];
?>
make a ajax call and get the value of using post
<script>
function getprice(val) {
$.ajax({
type: "POST",
url: "index.php",
data:'priceid='+val,
success: function(data){
$("#price-list").html(data);
}
});
}
</script>
<select class='form-control select2' id='product1' name='product1' style='width: 100%;' onChange="getstate(this.value);">
//yourcode
</select>
<div id="price-list">
</div>
index.php
<?php
$product1=$_POST['priceid'];
?>

Getting data to display in a form

I'm trying to use HTML, PHP and MYSQL to pull data from a database and display it in a form (to later be edited). At this point I'm only trying to pull that data and display it in a form. (I'll worry about updating later). I pull the data but nothing displays in my textboxes:
<?php
$con = mysqli_connect("XXXXX"); //removed for privacy
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query="select * from VOLUNTEER";
echo '$query';
$result = mysqli_query($con, $query);
echo "<table>";
if ($result)
{
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo '<form method = "post" action="insertvolunteer.php">';
echo '<tr>';
echo '<td>First Name:</td>';
echo '<td>' . '<input type=text name=FirstName' . $row["FirstName"] . '</td>';
echo '<td>' . '<input type=hidden name=VolunteerId' . $row["VolunteerId"] . '</td>';
echo '</tr>';
}
}
echo "</form>";
echo "</table>";
mysqli_close($con);
?>
Text box data needs to be displayed on value as
echo '<td><input type="text" name="FirstName" value="'.$row["FirstName"].'"></td>';
connect.php
<?php
$server = "server";
$user = "user";
$password = "password";
$bd = "yourbd";
$connect = mysql_connect($server, $user, $password);
$connect = mysql_select_db("$bd", $connect);
if (!$connect){
echo mysql_error(); exit;
}
?>
namefile.php
<?php
include('connect.php');
$select = mysql_query("select * from VOLUNTEER");
while ($show = mysql_fetch_assoc($select)):
echo "<table>";
echo "<form method = 'post' action='insertvolunteer.php'>";
echo '<tr>';
echo '<td>First Name:</td>';
echo '<td><input type="text" name="FirstName" value="'.$show["FirstName"].'"></td>';
echo '<td><input type="text" name="FirstName" value="'.$show["VolunteerId"].'"></td>';
echo '</tr>';
echo "</form";
echo "</table>";
endwhile;
?>
When you create an MySQL query, you need to declare this. How?
$var = "SELECT * FROM SOMEWHERE"; wrong
$var = mysql_query("SELECT * FROM SOMEWHERE"); right
'n in echo '<td>' . '<input type=text name=FirstName' . $row["FirstName"] . '</td>';, you need to close the tag. And also have no need of separate <td> of <input>.
Try something like :)
#update I realized that you've got what was wished. Cheers.

Categories