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);
}
?>
Related
I had inserted the data from html and it is stored in mysql database, and the data is retrieved from database to html,now I had done how to delete the data that are displayed in html,my task is how to update the displayed.And my deleted code is:
Below is my HTML code:
<html>
<head>
<title>STUDENT_DATA</title>
</head>
<body>
<form action="tab.php" method="post" >
<center>
sname: <input type="text" name="sname" required><br></br>
sno:<input type="text" name="sno"><br></br>
marks:<input type="text" name="marks"><br></br>
class:<input type="text" name="class"><br></br>
phno:<input type="text" name="phno" onkeypress='return event.charCode >
= 48 && event.charCode <= 57'><br></br>
DOB:<input type="date" placeholder="DD-MM-YYYY"
required pattern="(0[1-9]|1[0-9]|2[0-9]|3[01]).(0[1-9]|1[012]).[0-9]{4}"
name="DOB"/><br></br>
<button>submit</button></br>
</center>
</form>
</body>
</html>
Below is my PHP code for displaying the data:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
$connection = mysql_connect('localhost', 'root','');
if (!$connection)
{
die("Database Connection Failed" . mysql_error());
}
$select_db = mysql_select_db( "student",$connection);
if (!$select_db)
{
die("Database Selection Failed" . mysql_error());
}
$sql = "SELECT * FROM hello1 ";
$result = mysql_query($sql) or die(mysql_error());
?>
<table border="2" style= " margin: 0 auto;" id="myTable">
<thead>
<tr>
<th>sname</th>
<th>sno</th>
<th>marks</th>
<th>class</th>
<th>phno</th>
<th>DOB</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['sname'] . "</td>";
echo "<td>" . $row['sno'] . "</td>";
echo "<td>" . $row['marks'] . "</td>";
echo "<td>" . $row['class'] . "</td>";
echo "<td>" . $row['phno'] . "</td>";
echo "<td>" . $row['DOB'] . "</td>";
echo "<td><a href='delete.php?did=".$row['sname']."'>Delete</a></td>";
echo "</tr>";
}
?>
</tbody>
</table>
</body>
</html>
Below is my PHP code for deleting:
<?php
$connection = mysql_connect('localhost', 'root','');
if (!$connection)
{
die("Database Connection Failed" . mysql_error());
}
$select_db = mysql_select_db( "student",$connection);
if (!$select_db)
{
die("Database Selection Failed" . mysql_error());
}
?>
<?php
if(isset($_GET['did'])) {
$delete_id = $_GET['did'];
$sql = mysql_query("DELETE FROM hello1 WHERE sname = '".$delete_id."'");
if($sql) {
echo "<br/><br/><span>deleted successfully...!!</span>";
}
else
{
echo "ERROR";
}
}
?>
Below is what I have tried in update.php
<?php
$connection = mysql_connect('localhost', 'root','');
if (!$connection) { die("Database Connection Failed" . mysql_error());
}
$select_db = mysql_select_db( "emp",$connection);
if (!$select_db) { die("Database Selection Failed" . mysql_error()); } ?>
<?php if(isset($_GET['did']))
{
$update_id = $_GET['did']; $sql = mysql_query("UPDATE FROM venu WHERE id = '".$update_id."'");
if($sql) {
echo "<br/><br/><span>updated successfully...!!</span>";
} else {
echo "ERROR";
}
} ?>
So, I am trying to figure out how do this this and it boggling me. THIS WILL NOT BE USED ONLINE LIVE SO SQL INJECTION I DONT' CARE ABOUT. What am I doing wrong/right?
<?php
$db = mysql_connect("localhost", "root", "root");
if (!$db) {
die("Database connect failed: " . mysql_error());
}
$db_select = mysql_select_db("UNii", $db);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
$comment = $_GET['comment'];
$id = $_GET['id'];
$sql = "UPDATE Dbsaved SET comment = '{$comment}' WHERE id = $id";
$comment1 = mysql_query($sql);
if (!$comment1) {
die("did not save comment: " . mysql_error());
}
echo $sql;
The main problem is with the statement itself, the connection is fine. I am trying to read $comment, and then update that into a MYSQL table and then have it read back in a different file.
EDIT: Mark up for the form I'm taking $comment from.
<!DOCTYPE html>
<html lang="en">
<LINK href="stylesheet.css" rel="stylesheet" type="text/css">
<script src ="js/validateform.js"></script>
<head>
<meta charset="UTF-8">
<title>UniHelp Home</title>
</head>
<body>
<div id="headeruni">
<h1>Welcome <?php echo $_GET["name"]; ?> to UniHelp!</h1>
</div>
<div id ="infouni">
<h3>Welcome to UniHelp. The social Network getting you connected to other people all over the University for any help you require!</h3>
</div>
<div id ="nameandemail">
<form action="formsend.php" method="post">
First name: <br> <input type="text" name="name"><br>
Email: <br> <input type="text" name="email"><br>
Comment: <br> <input type="text" name="message"><br>
<input type="submit" name="submit">
</form>`enter code here`
</div>
<div id="grabphpdiv">
<?php
$db = mysql_connect("localhost", "root", "root");
if (!$db) {
die("Database connect failed: " . mysql_error());
}
$db_select = mysql_select_db("UNii", $db);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
$result = mysql_query("SELECT * FROM Dbsaved", $db);
if (!$result) {
die ("Database query failed: " . mysql_error());
}
$comment = $_POST['$comment'];
while ($row = mysql_fetch_array($result)) {
echo "<div id='posts'>";;
echo "<h2>";
echo $row[1] . "";
echo "</h2>";
echo "<p>";
//echo $timestamp = date('d-m-y G:i:s ');
echo "<br>";
echo "<br>";
echo $row[2] . "";
echo "</p>";
echo "<p>";
echo $row[3] . "";
echo "</p>";
echo 'Delete';
echo "<br>";
echo "<br>";
echo 'Comment: <br>
<input type=text name=comment><br>
<a href=addcomment.php?id=' . $row[0]. '&comment='. $row['$comment'].'>Comment</a>';
echo "<p>";
echo $row['comment'] . "";
echo "</p>";
echo "</div>";
echo "<br>";
}
?>
</div>
</body>
<div id="footer">Copyright © James Taylor 2016</div>
</html>
I just ran this code:
$comment = "Hello World!";
$id = 1;
$sql = "UPDATE Dbsaved SET comment = '{$comment}' WHERE id = {$id}";
echo $sql;
and saw:
UPDATE Dbsaved SET comment = 'Hello World!' WHERE id = 1
which is a correct SQL statement, so if it is not working, you might want to play with SQL directly to get something working. Hope that helps!
SOLUTION:
$comment = $_GET['$comment'];
$id = $_GET['$id'];
while ($row = mysql_fetch_array($result)) {
echo "<div id='posts'>";;
echo "<h2>";
echo $row[1] . "";
echo "</h2>";
echo "<p>";
//echo $timestamp = date('d-m-y G:i:s ');
echo "<br>";
echo "<br>";
echo $row[2] . "";
echo "</p>";
echo "<p>";
echo $row[3] . "";
echo "</p>";
echo 'Delete';
echo "<br>";
echo "<br>";
echo $row[4] . "";
echo "<br>";
echo 'Comment: <br>
<form action="addcomment.php?id=' . $row[0]. '" method="post">
<input type=text name=comment><br>
<input type=submit name="submit">
</form>';
echo "<p>";
echo $row['comment'] . "";
echo "</p>";
echo "</div>";
echo "<br>";
}
?>
and:
<?php
$db = mysql_connect("localhost", "root", "root");
if (!$db) {
die("Database connect failed: " . mysql_error());
}
$db_select = mysql_select_db("UNii", $db);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
$comment = $_POST['comment'];
$id = $_GET['id'];
$sql = "UPDATE Dbsaved SET comment = '$comment' WHERE id = $id ";
$comment1 = mysql_query($sql);
echo $sql;
if (!$comment1) {
die("did not save comment: " . mysql_error());
}
else {
header("location: UniHelpindex.php");
}
It was to do with mainly needing to get the id which was used in $row[0]' in the form created in the while loop. And actually using the correct syntax for the update Dbsaved... bit.
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);
}
?>
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' />";
Im a newbie in PHP and I want to create a simple webpage app for my website, I was able to produce this page base on a tutorial here.
<?php
$con = mysql_connect("localhost","*****","*****");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("*****", $con);
$result = mysql_query("SELECT * FROM products");
echo "<table border='1'>
<tr>
<th>Name</th>
<th>classification</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['classification'] . "</td>";
echo "<td><input type='checkbox' name='{number[]}' value='{$row['prodID']}' /></td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
<?php
?>
<html>
<head>
</head>
<form name="form1" method="post" action="result_page.php">
<input type="submit" name="Submit" value="Submit">
</p>
</form>
<body>
</body>
</html>
but my problem is how to create a result_page.php to show the selected entries or data base on the selected checkbox so i can create a comparison page. I have this as being my result_page.php but nothing is showing up. I know Im doing something wrong but I cant find out.
<?php
error_reporting(E_ALL);
$host = 'localhost';
$user = '******';
$pass = '******';
$dbname = '******';
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);
$sql = "SELECT * FROM products WHERE prodID IN (";
foreach ($_POST['number'] as $product) $sql .= "'" . $product . "',";
$sql = substr($sql,0,-1) . ")";
$result = mysql_query($sql);
while ($myrow = mysql_fetch_array($result))
{
echo "<table border=1>\n";
echo "<tr><td>Name</td><td>Position</td></tr>\n";
do {
printf("<tr><td>%s %s</td><td>%s</tr>\n", $myrow["1"], $myrow["2"], $myrow["3"]);
} while ($myrow = mysql_fetch_array($result));
echo "</table>\n";
}
?>
A quick glance, the section that generates the output is not correct. You have looped two times for no apperant reason.
while ($myrow = mysql_fetch_array($result)) //<========remove this line
{ //<========remove this line
echo "<table border=1>\n";
echo "<tr><td>Name</td><td>Position</td></tr>\n";
do {
printf("<tr><td>%s %s</td><td>%s</tr>\n", $myrow["1"], $myrow["2"], $myrow["3"]);
} while ($myrow = mysql_fetch_array($result));
echo "</table>\n";
} //<========remove this line
This is done by human parse, but should serves as a starting point.
And to recap tadman, no this is not a good tutorial. And normally you won't need to do printf for the output.