update mysql row with html form and php - php

I've been looking through many threads on here without finding a solution to my problem.
I've created a form that is supposed to show content of a database in input boxes, and when i change the content, it should be updated in the database.
No errors, nothing gets changed.
<?php
$con=mysqli_connect("localhost","root","","frontpage");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM frontpage_left_links")
or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result)){
echo '<form action="" method="post">';
echo '<div style="float:left">';
echo '<table border="1" bordercolor="#000000">';
echo '<tr>';
echo '<td>link</td>';
echo '<td><input type="text" name="linkid" value="'.$row['link'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>img</td>';
echo '<td><input type="text" name="imgid" value="'.$row['img'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>tekst</td>';
echo '<td><input type="text" name="imgid" value="'.$row['name'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="submit" id="update" name="gem" value="Gem"</td></td>';
echo '<td><input type="hidden" name="id" value="'.$row['id'].'"></td>';
echo '</tr>';
echo '</table></div>';
echo '<div style="float:left"><center><img src="img/'.$row['img'].'"><br />'.$row['name'].'</center></div>';
echo '</form><br /><br /><br /><br /><br /><br /><br /><br />';
}
if(isset($_POST['update'])){
$id = $_POST['id'];
$link = $_POST['linkid'];
$img = $_POST['imgid'];
$name = $_POST['nameid'];
$sql = mysqli_query("UPDATE frontpage_left_links SET link = '$link', img = '$img', name = '$name' WHERE id = '$id'");
$retval = mysqli_query( $sql, $con );
if(! $retval ){
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
}
mysqli_close($con);
?>
The form show the database content fine, but nothing happens when changed.
I appreciate any help I can get.
This is what it looks like now.
<?php
$con=mysqli_connect("localhost","root","","frontpage");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['gem']))
{
$id = $_POST['id'];
$link = $_POST['linkid'];
$img = $_POST['imgid'];
$name = $_POST['nameid'];
$sql = mysqli_query("UPDATE frontpage_left_links SET link = '$link', img = '$img', name = '$name' WHERE id = '$id'");
$retval = mysqli_query( $con, $sql );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
}
$result = mysqli_query($con,"SELECT * FROM frontpage_left_links")
or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result))
{
echo '<form action="" method="post">';
echo '<div style="float:left">';
echo '<table border="1" bordercolor="#000000">';
echo '<tr>';
echo '<td>link</td>';
echo '<td><input type="text" name="linkid" value="'.$row['link'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>img</td>';
echo '<td><input type="text" name="imgid" value="'.$row['img'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>tekst</td>';
echo '<td><input type="text" name="nameid" value="'.$row['name'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="submit" id="update" name="gem" value="Gem"</td></td>';
echo '<td><input type="hidden" name="id" value="'.$row['id'].'"></td>';
echo '</tr>';
echo '</table></div>';
echo '<div style="float:left"><center><img src="img/'.$row['img'].'"><br />'.$row['name'].'</center></div>';
echo '</form><br /><br /><br /><br /><br /><br /><br /><br />';
}
mysqli_close($con);
?>
Now i get this error.
Warning: mysqli_query() expects at least 2 parameters, 1 given in /Applications/XAMPP/xamppfiles/htdocs/page/admin.php on line 17
Warning: mysqli_query(): Empty query in /Applications/XAMPP/xamppfiles/htdocs/page/admin.php on line 19
Could not update data:

Because your udate is at the end of the page put it above the rest.
And also change isset($_POST['update'] to isset($_POST['gem']
<?php
$con=mysqli_connect("localhost","root","","frontpage");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['gem']))
{
$id = $_POST['id'];
$link = $_POST['linkid'];
$img = $_POST['imgid'];
$name = $_POST['nameid'];
$sql = "UPDATE frontpage_left_links SET link = '$link', img = '$img', name = '$name' WHERE id = '$id'";
$retval = mysqli_query($con,$sql );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
}
$result = mysqli_query($con,"SELECT * FROM frontpage_left_links")
or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result))
{
echo '<form action="" method="post">';
echo '<div style="float:left">';
echo '<table border="1" bordercolor="#000000">';
echo '<tr>';
echo '<td>link</td>';
echo '<td><input type="text" name="linkid" value="'.$row['link'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>img</td>';
echo '<td><input type="text" name="imgid" value="'.$row['img'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>tekst</td>';
echo '<td><input type="text" name="imgid" value="'.$row['name'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="submit" id="update" name="gem" value="Gem"</td></td>';
echo '<td><input type="hidden" name="id" value="'.$row['id'].'"></td>';
echo '</tr>';
echo '</table></div>';
echo '<div style="float:left"><center><img src="img/'.$row['img'].'"><br />'.$row['name'].'</center></div>';
echo '</form><br /><br /><br /><br /><br /><br /><br /><br />';
}
mysqli_close($con);
?>

try to replace :
echo '<td><input type="submit" id="update" name="gem" value="Gem"</td></td>';
with :
echo '<td><input type="submit" id="update" name="update" value="Gem"</td></td>';

In your form you are having update button name as gem but you are using if(isset($_POST['update'])) to run update query.
Change it to if(isset($_POST['gem']))

The easiest way would be to simply change the name attribute on your submit to the $_POST[] variable you used. name="update"The easiest way would be to simply change the name attribute on your submit to the $_POST[] variable you used. name="update"
Edit :: Answered already.

echo '<td><input type="submit" id="update" name="update" value="Gem"</td></td>';
Just delete the second td from above!!!

Related

How to update MySql data from HTML using PHP?

I am trying to update mysql database from html table with php, but when edit the row that i want to update and press the button to update, field that was suposed to updated becomes empty.
Also when i press update i get this notice:
Notice: Undefined index: status in /storage/ssd1/314/2412314/public_html/status3.php on line 174.
This is my code:
<?php
$db_host='example';
$db_user='example';
$db_pass='example';
$db_name='example';
$con = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['update']))
{
$STATUS = $_POST['status'];
$PK= $_POST['pkvara'];
$sql = "UPDATE Radionica SET status = '$STATUS' WHERE pkvara = '$PK'";
$retval = mysqli_query($con,$sql );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
echo "Status uspešno promenjen\n";
}
$result = mysqli_query($con,"SELECT * FROM Radionica")
or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result))
{
echo '<form action="" method="post">';
echo '<tr>';
echo '<td>'.$row['registracija'].'</td>';
echo '<td>'.$row['status'].'</td>';
echo '<td><input type="text" name="sta" value="'.$row['status'].'"><input type="submit" name="update" value="Promeni" /></td>';
echo'<td><input type="hidden" name="pkvara" value="'.$row['pkvara'].'"></td>';
echo '</tr>';
echo '</form>';
}
mysqli_close($con);
?>
Fix this line and it will work fine.
echo '<td><input type="text" name="status" value="'.$row['status'].'"><input type="submit" name="update" value="Promeni" /></td>';
correct this line
echo '<td><input type="text" name="sta" value="'.$row['status'].'"><input type="submit" name="update" value="Promeni" /></td>';
TO
echo '<td><input type="text" name="STATUS" value="'.$row['status'].'"><input type="submit" name="update" value="Promeni" /></td>';

using form textfield name from dynamic table outside whileloop

I got another problem with my Code.
I generate a dynamic table from SQL-content and use textfields in the table to, maybe someday, change the content.
The problems is, I cann't access the textfields from outside the whileloop to save the content, all I get is Undefined index error for every field.
<form method="POST" enctype="text/html">
<?php
require_once ('config.php');
$sql = " SELECT * FROM kassen ORDER BY name ASC ";
$db_erg = mysql_query( $sql );
echo "<tr>";
echo "<td>";
echo '<table border="1" width="80%" align="center">';
echo "<tr> <th>Name</th><th>Stand</th><th>Verbrauch</th><th>Einzahlungen</th></tr>";
while ($zeile = mysql_fetch_assoc($db_erg))
{
echo '<tr>';
echo '<td>'. $zeile['name'] . '</td>';
echo '<td><center>'. $zeile['bier_stand'] . '€</td>';
echo '<td>';
/* in the text below, i set the name to verbrauch"'.$zeile['id'] and
ergebnis"'.$zeile['id'] which should generate a new unique name for every
single text*/
echo '<center><input type="text" name="verbrauch"'.$zeile['id'].' value="0" size="10" />';
echo '</td>';
echo '<td>';
echo '<center><input type="text" name="einzahlung"'.$zeile['id'].' value="0" size="10" />';
echo '</td>';
echo '</tr>';
} echo '</table>';
?>
<center><input type="hidden" name="aktion" value="speichern" />
<center><input type="Submit" name="" value="speichern"/>
</form>
<?php
if (isset ($_POST['aktion']))
{
if ($_POST['aktion'] == "speichern" )
{
require_once ('config.php');
$sql = " SELECT * FROM kassen ORDER BY name ASC ";
$db_erg = mysql_query( $sql );
while ($zeile = mysql_fetch_assoc($db_erg))
{
$standalt = $zeile["bier_stand"];
/* now I try to put the value of the text to the DB, but all i get is
Undefined Index error */
$verbrauch = $_POST['verbrauch'.$zeile['id']];
$einzahlung = $_POST['einzahlung'.$zeile['id']];
$stand = $zeile["bier_stand"] - $verbrauch + $einzahlung;
$id = $zeile["id"];
$sql = "UPDATE kassen SET ";
$sql .= " bier_stand_alt = '$standalt', ";
$sql .= " bier_stand = '$stand', ";
$sql .= " bier_verbrauch = '$verbrauch', ";
$sql .= " bier_einzahlungen = '$einzahlung' ";
$sql .= " WHERE id='$id'";
}
echo '<h2>Änderungen übernommen</h2>';
echo 'zurück zur Bierkasse';
exit;
}
}
?>
Any idea what I'm messing up?
Sorry for questioning,
realy stupid mistake
echo '<center><input type="text" name="einzahlung"'.$zeile['id'].' value="0" size="10" />';
should have been
echo '<center><input type="text" name="einzahlung'.$zeile['id'].'" value="0" size="10" />';
so actually just the " was in wrong place ... stupid stuff that happens at 3am

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.

PHP function is not showing the HTML data

I am trying to print data from dataBASE with SQL and PHP. all worked fine the issue is when I am trying to to warp all thing to function it not working and the table is not showing.
with out the function loadClient it work fine.
please help...
$("#loadbtn").click(function(){
var s = '
<?php
loadClients()
function loadClients(){
$link = mysql_connect($db_host,$db_user,$db_pass);
if(!$link) die ('Could not connect to database: '.mysql_error());
mysql_select_db($db_name,$link);
mysql_query("SET NAMES 'utf8'");
echo '<table id="clienttable" border="1" cellspacing="0" cellpadding="0" width="100%">';
echo '<tr>';
echo '<td width="80px">שם פרטי</td>';
echo '<td>שם משפחה</td>';
echo '<td>טלפון</td>';
echo '<td>אימייל</td>';
echo '<td>עיר</td>';
echo '<td width="120px">שעת רישום</td>';
echo '<td>מספרי תמונות</td>';
echo '<td>שמור</td>';
echo '</tr>';
$loadQuery="SELECT * FROM `claients` WHERE `eventreg_pictures` is null";
$result=mysql_query($loadQuery);
while($row= mysql_fetch_array($result)){
$client= $row;
$clients[]=$client;
echo '<tr>';
echo '<form id="loadForm" method="post" action="admin.php">';
echo '<td><input type="text" id="lfname" name="lfname" value="'.$client[1].'"/></td>';
echo '<td><input type="text" id="llname" name="llname" value="'.$client[2].'"/></td>';
echo '<td><input type="text" id="lphone" name="lphone" value="'.$client[3].'"/></td>';
echo '<td><input type="text" id="lemail" name="lemail" value="'.$client[4].'"/></td>';
echo '<td><input type="text" id="lcity" name="lcity" value="'.$client[5].'"/></td>';
echo '<td>'.$client[7].' ';
echo '<td><input type="text" id="lphotos" name="lphotos"/></td>';
echo '<td><input type="submit" id="savebtn" name="savebtn" value-"שמור"/></td>';
echo '</form>';
echo '</tr>';
}
echo '</table>';
}
?>';
You should use $.ajax() like
Javascript
$("#loadbtn").click(function(){
$.ajax({
type: "POST",
url: 'page.php',
success: function(d){alert(d);},
});
});
PHP
$link = mysql_connect($db_host,$db_user,$db_pass);
if(!$link) die ('Could not connect to database: '.mysql_error());
mysql_select_db($db_name,$link);
mysql_query("SET NAMES 'utf8'");
echo '<table id="clienttable" border="1" cellspacing="0" cellpadding="0" width="100%">';
echo '<tr>';
echo '<td width="80px">שם פרטי</td>';
echo '<td>שם משפחה</td>';
echo '<td>טלפון</td>';
echo '<td>אימייל</td>';
echo '<td>עיר</td>';
echo '<td width="120px">שעת רישום</td>';
echo '<td>מספרי תמונות</td>';
echo '<td>שמור</td>';
echo '</tr>';
$loadQuery="SELECT * FROM `claients` WHERE `eventreg_pictures` is null";
$result=mysql_query($loadQuery);
while($row= mysql_fetch_array($result)){
$client= $row;
$clients[]=$client;
echo '<tr>';
echo '<form id="loadForm" method="post" action="admin.php">';
echo '<td><input type="text" id="lfname" name="lfname" value="'.$client[1].'"/></td>';
echo '<td><input type="text" id="llname" name="llname" value="'.$client[2].'"/></td>';
echo '<td><input type="text" id="lphone" name="lphone" value="'.$client[3].'"/></td>';
echo '<td><input type="text" id="lemail" name="lemail" value="'.$client[4].'"/></td>';
echo '<td><input type="text" id="lcity" name="lcity" value="'.$client[5].'"/></td>';
echo '<td>'.$client[7].' ';
echo '<td><input type="text" id="lphotos" name="lphotos"/></td>';
echo '<td><input type="submit" id="savebtn" name="savebtn" value-"שמור"/></td>';
echo '</form>';
echo '</tr>';
}
echo '</table>';
}

instead of the input being in a text box, i would like it to have a drop down list where the choices are from the mysql table

<html>
<body>
Organization Name: <input type="text" name="org_name" /><br />
Position:<input type="text" name="position" /><br/><br/>
<input type="submit" /><br><br>
<STRONG>Result</STRONG>
<?php
$con=mysql_connect("localhost","root","");
if(!$con) {
die('could not connect:'.mysql_error());
}
mysql_select_db("final?orgdocs",$con);
$org_name = $_POST["org_name"];
$position = $_POST["position"];
$result = mysql_query("SELECT * FROM directory WHERE org_name = '$org_name' OR position = '$position' ORDER BY org_name");
echo $stmt;
mysql_query($stmt);
echo '<TABLE BORDER = "1">';
$result1 = $result;
echo '<TR>'.'<TD>'.'Name'.'</TD>'.'<TD>'.'Organization Name'.'</TD>'.'<TD>'.'Position'.'</TD>'.'<TD>'.'Cell Number'.'</TD>'.'<TD>'.'Email-Add'.'</TD>';
echo '</TR>';
while ($row = mysql_fetch_array($result1)){
echo '<TR>'.'<TD>'.$row['name'].'</TD>'.'<TD>'.$row['org_name'].'</TD>';
echo '<TD>'.$row['position'].'</TD>'.'<TD>'.$row['cell_num'].'</TD>'.'<TD>'.$row['email_add'].'</TD>';
echo '</TR>';
}
echo '</TABLE>';
if (mysql_num_rows($result)>0) {
// your thing above with mysql_fetch_array($result1) etc
} else {
echo 'Not found';
}
?>
</body>
</html>
It is simple matter of doing this:
echo '<select name="selectName">';
while ($row = mysql_fetch_array($result1)){
echo '<option value="'.$row['fieldName'].'">' . $row['fieldName'] . '</option>';
}
echo '</select>';

Categories