I am trying to create a members table, using a PHPmyadmin database, and in the database, there is a 0 & 1 field, and I want to transform the 0 and 1 in a checkbox, for each member. Is an editable table, so the table might be in a form. Here is what I did:
<?php
include('../../config.php');
$result2 = mysql_query("SELECT * FROM acars_users");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Dados Pessoais</th>
<th>Origem</th>
<th>Dados de Redes</th>
<th width='220px'>Base</th>
<th>Patente</th>
<th>Horas</th>
<th>É Ativo?</th>
<th>Dinheiro</th>
<th>Senha</th>
<th>Opções</th>
</tr>";
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td width='300px'>" . "<form action=editar.php method=POST><input name=username size=7 type=text value=".$row['username']." /></br><input name=nome size=10 type=text value=".$row['nome']." /><input name=sobrenome size=20 type=text value=".$row['sobrenome']." /></br><input name=email size=25 type=text value=".$row['email']." />" . "</td>";
echo "<td>" . "<input name=datanascimento size=10 type=text value=".$row['datanascimento']." /></br><input name=pais size=10 type=text value=".$row['pais']." /></br><input name=cidade size=30 type=text value=".$row['cidade']." />" . "</td>";
echo "<td>" . "IVAO: <input name=idivao size=8 type=text value=".$row['idivao']." /></br>VATSIM: <input name=idvatsim size=8 type=text value=".$row['idvatsim']." />" . "</td>";
echo "<td>" . "<input name=base size=8 type=text value=".$row['base']." />" . "</td>";
echo "<td>" . "<input name=rank size=10 type=text value=".$row['rank']." /></br>Admin <input name=admin size=1 type=checkbox value=".$row['admin']." /></br>DOV <input name=dov size=1 type=checkbox value=".$row['dov']." /></br>Checador <input name=checador size=1 type=checkbox value=".$row['checador']." />" . "</td>";
echo "<td>" . "<input name=horas size=6 type=text value=".$row['horas']." />" . "</td>";
echo "<td>" . "<input type=checkbox name='isactive[]' value=".$row['isactive']." />" . "</td>";
echo "<td>" . "<input name=dinheiro size=10 type=text value=".$row['dinheiro']." />" . "</td>";
echo "<td>" . "<input name=password size=10 type=password value=".$row['password']." />" . "</td>";
echo "<td>" . "<input name=edit_id value=".$row['id']." type=hidden><input type=submit value=Editar ></form><form action=deletarexist.php method=POST><input name=delete_id value=".$row['id']." type=hidden><input type=submit value=Demitir></form>" . "</td>";
echo "</tr>";
}
echo "</table>";
?>
$chkd=$row['isactive']?'checked="checked"':''; // 0: false; 1: true
echo "<td><input type='checkbox' name='isactive[]' value='{$row['isactive']}' $chkd></td>";
(Although, you should look into using a templatingg system (such as smarty) rather than echoing html fragments. It seems at bit weird in the start, but as soon as you get used to it, you'll never look back-)
I guess you want the checkbox checked, and in that case you need to add the checked="checked" attribute to the checkbox.
So what you would do would be something like this:
if($row['isactive'] == 1){
echo "<td>" . "<input type='checkbox' name='isactive[]' value=".$row['isactive']. checked='checked' />" . "</td>";
} else {
echo "<td>" . "<input type='checkbox' name='isactive[]' value=".$row['isactive']." />" . "</td>";
}
For the value you probably should have something to identify the checkbox, like a memberid depending on what you are using it for.
Related
I'm trying to output the contents of my MySQL user table, i have the code set up in a way in which administrators can add, edit and delete rows within the table. After looking into the issue a bit more however I found that the html code is outputting the values in an awkward way, as it's only counting the first word in the column as a valid value so it ignores the other words. How can I go about fixing this?
For example say i'm trying to output an album called Skulls & Bones the output on the table will just be Skulls.
Admin_Album_Page.php
<!DOCTYPE HTML>
<html>
<head>
<title>View Album Table</title>
</head>
<body>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
require_once 'ConnectorCode.php';
//mysql_query command is used to select data from Albums table
$result = mysqli_query($conn, "SELECT * FROM tbl_Albums");
//Echos setting established onto the main table
echo "<table border='1'>";
echo "<tr> <th>Album ID</th> <th>Album Name</th> <th>Number Of Tracks</th>
<th> Genre </th> <th>Track ID</th> </tr>";
//Results are looped and then displayed in tables while($row = mysqli_fetch_array($result)) {
echo "<form action=Admin_Album_Page.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=number name=albumid value=" . $row['Album_id'] . " </td>";
echo "<td>" . "<input type=text name=albumname value=" . $row['Album_Name'] . " </td>";
echo "<td>" . "<input type=number name=numberoftracks value=" . $row['Number_Of_Tracks'] . " </td>";
echo "<td>" . "<input type=text name=genre value=" . $row['Genre'] . " </td>" ;
echo "<td>" . "<input type=number name=artistid value=" . $row['Artist_id'] . " </td>";
echo "<td>" . "<input type=hidden name=hidden value=" . $row['Album_id'] . " </td>";
echo "<td>" . "<input type=submit name=update value=Update" . " </td>";
echo "<td>" . "<input type=submit name=delete value=Delete" . " </td>";
echo "</form>";
}
echo "<form action=Admin_Artist_Page.php method=post>";
echo "<tr>";
echo "<td></td>";
echo "<td><input type=text name=ualbumname></td>";
echo "<td><input type=text name=unumberoftracks></td>";
echo "<td><input type=text name=ugenre></td>";
echo "<td><input type=number name=uartistid></td>";
echo "<td>" . "<input type=submit name=add value=Add" ." </td>";
echo "</form>";
echo "</table>";
//Connection is closed
mysqli_close($conn);
?>
<p>Return to main page</p>
</body>
</html>
The problem is in your HTML rendering.
You need to change your code to this:
echo "<form action=\"Admin_Album_Page.php\" method=\"post\">";
echo "<tr>";
echo "<td>" . "<input type=\"number\" name=\"albumid\" value=\"" . $row['Album_id'] . "\" </td>";
echo "<td>" . "<input type=\"text\" name=\"albumname\" value=\"" . $row['Album_Name'] . "\" </td>";
echo "<td>" . "<input type=\"number\" name=\"numberoftracks\" value=\"" . $row['Number_Of_Tracks'] . "\" </td>";
echo "<td>" . "<input type=\"text\" name=genre value=" . $row['Genre'] . " </td>" ;
echo "<td>" . "<input type=\"number\" name=\"artistid\" value=\"" . $row['Artist_id'] . "\" </td>";
echo "<td>" . "<input type=\"hidden\" name=\"hidden\" value=\"" . $row['Album_id'] . "\" </td>";
echo "<td>" . "<input type=\"submit\" name=\"update\" value=\"Update" . "\" </td>";
echo "<td>" . "<input type=\"submit\" name=\"delete\" value=\"Delete" . "\" </td>";
echo "</form>"
echo "<form action=\"Admin_Artist_Page.php\" method=\"post\">";
echo "<tr>";
echo "<td></td>";
echo "<td><input type=\"text\" name=\"ualbumname\"></td>";
echo "<td><input type=\"text\" name=\"unumberoftracks\"></td>";
echo "<td><input type=\"text\" name=\"ugenre\"></td>";
echo "<td><input type=\"number\" name=\"uartistid\"></td>";
echo "<td>" . "<input type=\"submit\" name=\"add\" value=\"Add" ."\" </td>";
echo "</form>";
echo "</table>";
Explaining:
HTML works the following way:
<tag attribute="value"></tag>
You can't simply call it by
<tag attribute=value></tag>
In the code that I've showed you, you want to make sure that the " symbol is part of the string, so you escape it using \
while($record= mysqli_fetch_array($myData)){
echo "<form action=cpoarea1update.php method=post>";
echo "<tr>";
echo "<td><label>". $record['CODE'] . " </td>";
echo "<td>". "<input type=text name=dt value =" . $record['DOCUMENT_TITLE']. " </td>";
echo "<td>". "<input type=text name=rv value=" . $record['REVISION']. " </td>";
echo "<td>". "<input type=date name=dateis value=" . $record['DATE_OF_ISSUE']. " </td>";
echo "<td>". "<input type=hidden name=hidden value=" . $record['CODE']. " </td>";
echo "<td>". "<input type=submit name=update value=update " . " </td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
For example if the data in MySQL is "Hello my name is Maxim", I only see "Hello" in the textbox. What's the problem? Thanks in advance
You have to wrap <input> attribute(s) by quotes. Also, you need to close tags!
echo "<td><input type=text name=dt value =\"" . $record['DOCUMENT_TITLE']. "\"> </td>";
^ ^ ^
Your original code output this not valid HTML:
<td><input type=text name=dt value =document title </td>
Best practice is to always use quotes:
<td><input type="text" name="dt" value="document title"></td>
I'm trying to is have have table to display my database with an update, delete, and add functions.
My problem is when I view it. It doesn't show the full text in the "Description" column and also in "Developer". Also in the "SecondDirectory" column it shows </td instead of just blank if its blank.
<html>
<head>
</head>
<body>
<?php
$con = mysql_connect("$$$$","$$$$","$$$$");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("mydb",$con);
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE table SE ID='$_POST[topic]',Filename='$_POST[filename]', Description='$_POST[description]', TopDirectory='$_POST[topdirectory]', SecondDirectory='$_POST[seconddirectory]', Developer='$_POST[developer]', Date='$_POST[date]' WHERE ID='$_POST[hidden]'";
mysql_query($UpdateQuery, $con);
};
if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM table WHERE ID='$_POST[hidden]'";
mysql_query($DeleteQuery, $con);
};
if(isset($_POST['add'])){
$AddQuery = "INSERT INTO table (ID,Filename,Description,TopDirectory,SecondDirectory,Developer,Date) VALUES ('$_POST[uid]','$_POST[ufilename]','$_POST[udescription]','$_POST[utopdirectory]','$_POST[useconddirectory]','$_POST[udeveloper]','$_POST[udate]')";
mysql_query($AddQuery, $con);
};
$sql = "SELECT * FROM table";
$myData = mysql_query($sql,$con);
echo "<table border=1>
<tr>
<th>ID</th>
<th>Filename</th>
<th>Description</th>
<th>TopDirectory</th>
<th>SecondDirectory</th>
<th>Developer</th>
<th>Date</th>
</tr>";
while($record = mysql_fetch_array($myData)){
echo "<form action=theworks.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=text name=id value=" . $record['ID'] . " </td>";
echo "<td>" . "<input type=text name=filename value=" . $record['Filename'] . " </td>";
echo "<td>" . "<input type=text name=description value=" . $record['Description'] . " </td>";
echo "<td>" . "<input type=text name=topdirectory value=" . $record['TopDirectory'] . " </td>";
echo "<td>" . "<input type=text name=seconddirectory value=" . $record['SecondDirectory'] . " </td>";
echo "<td>" . "<input type=text name=developer value=" . $record['Developer'] . " </td>";
echo "<td>" . "<input type=text name=date value=" . $record['Date'] . " </td>";
echo "<td>" . "<input type=hidden name=hidden value=" . $record['ID'] . " </td>";
echo "<td>" . "<input type=submit name=update value=update" . " </td>";
echo "<td>" . "<input type=submit name=delete value=delete" . " </td>";
echo "</tr>";
echo "</form>";
}
echo "<form action=database.php method=post>";
echo "<tr>";
echo "<td><input type=text name=uid></td>";
echo "<td><input type=text name=ufilename></td>";
echo "<td><input type=text name=udescription><td>";
echo "<td><input type=text name=utopdirectory></td>";
echo "<td><input type=text name=useconddirectory></td>";
echo "<td><input type=text name=udeveloper><td>";
echo "<td><input type=text name=udate></td>";
echo "<td>" . "<input type=submit name=add value=add" . " </td>";
echo "</tr>";
echo "</form>";
echo "</table>";
mysql_close($con);
?>
</body>
</html>
So I figured it out.
echo "<td>" . "<input type=text name=description value=" . $record['Description'] . " </td>";
What I needed to put is a single quotation right after value= and two single quotations in " " so that it also counts the spaces. The code should have been...
echo "<td>" . "<input type=text name=description value='" . $record['Description'] . "'' </td>";
I am trying to update a record in my database which is a blob type, I can insert using this method really easily but when using UPDATE it doesn't seem to work, only for text. Would someone be able to point me in the right direction?
Here is my code:
<?php
$conn=mysqli_connect("localhost","root","root","plantidentify");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$ufamid = mysqli_real_escape_string($conn, $_POST['ufamilyid']);
$ucommonname = mysqli_real_escape_string($conn, $_POST['ucomname']);
$usynname = mysqli_real_escape_string($conn, $_POST['usynonyms']);
$uspecies = mysqli_real_escape_string($conn, $_POST['uspecies']);
$uprop = mysqli_real_escape_string($conn, $_POST['upropagation']);
$uimageData = mysqli_real_escape_string($conn, (file_get_contents($_FILES['uimage']['tmp_name'])));
$uheight = mysqli_real_escape_string($conn, $_POST['uheight']);
$utreeclass = mysqli_real_escape_string($conn, $_POST['utreeclass']);
$ulifecycle = mysqli_real_escape_string($conn, $_POST['ulifecycle']);
$UpdateQuery = "UPDATE plant
SET FamilyID='$ufamid', CommonName='$ucommonname',
SynonymNames='$usynname', Species='$uspecies',
Propagation='$uprop', Image='$uimageData',
Height='$uheight', TreeClass='$utreeclass',
Lifecycle='$ulifecycle'
WHERE PlantID='$_POST[uplantid]'";
if (!mysqli_query($conn,$UpdateQuery)) {
die('Error: ' . mysqli_error($conn));
}
header("Location: plantedit.php");
mysqli_close($conn);
?>
$uimageData is the variable in which contains the uploaded file from my form in another file. The column in my database where I am trying to update the blob is called "Image".
This might help, here is the form code that is in another .php which the update plant.php gets fired from:
<?php //display databse values
while($plant=mysql_fetch_array($retval)) {
echo"<form action=updateplant.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=number name=ufamilyid value='" .$plant['FamilyID']."' ></td>";
echo "<td>" . "<input type=text name=ucomname value='" .$plant['CommonName']."' ></td>";
echo "<td>" . "<input type=text name=usynname value='" .$plant['SynonymNames']."' ></td>";
echo "<td>" . "<input type=text name=uspecies value='" .$plant['Species']."' ></td>";
echo "<td>" . "<input type=text name=uprop value='" .$plant['Propagation']."' ></td>";
echo "<td>" . "<input type=file name=uimage></td>";
echo "<td>" . '<img class="crop2" src="data:image/jpeg;base64,'. base64_encode( $plant['Image'] ).'"/>'." </td>";
echo "<td>" . "<input type=number name=uheight value='" .$plant['Height']."' ></td>";
echo "<td>" . "<input type=text name=utreeclass value='" .$plant['TreeClass']."' ></td>";
echo "<td>" . "<input type=text name=ulifecycle value='" .$plant['Lifecycle']."' ></td>";
echo "<td>" . "<input type=hidden name=uplantid value='" .$plant['PlantID']."' ></td>";
echo "<td>" . "<input type=submit name=update value=Update" . " ></td>";
echo "</tr>";
echo "</form>";
}//end while
mysql_close($conn);
?>
to upload a file you should mention enctype='multipart/form-data' in form . Hope this will fix your problem.
<?php
//display databse values
while($plant=mysql_fetch_array($retval)) {
echo"<form action='updateplant.php' method='post' enctype='multipart/form-data' >";
echo "<tr>";
echo "<td>" . "<input type=number name=ufamilyid value='" .$plant['FamilyID']."' ></td>";
echo "<td>" . "<input type=text name=ucomname value='" .$plant['CommonName']."' ></td>";
echo "<td>" . "<input type=text name=usynname value='" .$plant['SynonymNames']."' ></td>";
echo "<td>" . "<input type=text name=uspecies value='" .$plant['Species']."' ></td>";
echo "<td>" . "<input type=text name=uprop value='" .$plant['Propagation']."' ></td>";
echo "<td>" . "<input type=file name=uimage></td>";
echo "<td>" . '<img class="crop2" src="data:image/jpeg;base64,'. base64_encode( $plant['Image'] ).'"/>'." </td>";
echo "<td>" . "<input type=number name=uheight value='" .$plant['Height']."' ></td>";
echo "<td>" . "<input type=text name=utreeclass value='" .$plant['TreeClass']."' ></td>";
echo "<td>" . "<input type=text name=ulifecycle value='" .$plant['Lifecycle']."' ></td>";
echo "<td>" . "<input type=hidden name=uplantid value='" .$plant['PlantID']."' ></td>";
echo "<td>" . "<input type=submit name=update value=Update" . " ></td>";
echo "</tr>";
echo "</form>";
}//end while
mysql_close($conn);
?>
Looks like you have a typo in the WHERE statement, $_POST[uplantid] should be ". $_POST['uplantid'] ."
I am having a problem displaying content on a table from mysql table in the database to a form in PHP. My problem is that only the first word show on the address field for example.
Please look at my page on: http://www3.londonmet.ac.uk:8008/~iia0014/employeeManager.php
And also the cells are not aligned with the title.
Can anyone help me to solve this?
On my css I have:
table {
table-layout:fixed;
width:180%;
overflow:hidden;
border:1px ;
word-wrap:nowrap;
text-align:left;
}
But even if removing the CSS, just the first word appear.
PHP CODE:
<?php
// Connect to server and select databse.
$con = mysql_connect("$host","$username","$password");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("$db_name",$con);
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE employees SET
Name='$_POST[name]',
DOB='$_POST[dob]',
Tel='$_POST[tel]',
Address='$_POST[address]',
Department='$_POST[department]',
PayRate='$_POST[payrate]',
Skills='$_POST[skills]',
Gender='$_POST[gender]'
WHERE EmpNo='$_POST[hidden]'";
mysql_query($UpdateQuery, $con);
};
if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM employees WHERE EmpNo='$_POST[hidden]'";
mysql_query($DeleteQuery, $con);
};
if(isset($_POST['add'])){
$AddQuery = "INSERT INTO employees (EmpNo, Name, DOB, Tel, Address, Department, PayRate, Skills, Gender) VALUES ('$_POST[uempNo]','$_POST
[uname]','$_POST[udob]', '$_POST[utel]','$_POST[uaddress]','$_POST[udepartment]', '$_POST[upayrate]','$_POST[uskills]','$_POST[ugender]')";
mysql_query($AddQuery, $con);
};
$sql = "SELECT * FROM employees";
$myData = mysql_query($sql,$con);
?>
<table border="1" width="10%">
<?php
echo "<tr>
<th>Number</th>
<th >Employee Name</th>
<th>DOB</th>
<th>Telephone</th>
<th>Address</th>
<th>Department</th>
<th>Pay Rate</th>
<th>Skills</th>
<th>Gender</th>
</tr>";
while($record = mysql_fetch_array($myData)){
echo "<form action=employeeManager.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=hidden name=hidden value=" . $record['EmpNo'] . " </td>";
echo "<td>" . "<input type=text name=name value=" . $record['Name'] . " </td>";
echo "<td>" . "<input type=text name=dob value=" . $record['DOB'] . " </td>";
echo "<td>" . "<input type=text name=tel value=" . $record['Tel'] . " </td>";
echo "<td>" . "<input type=text name=address value=" . $record['Address'] . " </td>";
echo "<td>" . "<input type=text name=department value=" . $record['Department'] . " </td>";
echo "<td>" . "<input type=text name=payrate value=" . $record['PayRate'] . " </td>";
echo "<td>" . "<input type=text name=skills value=" . $record['Skills'] . " </td>";
echo "<td>" . "<input type=text name=gender value=" . $record['Gender'] . " </td>";
echo "<td>" . "<input type=submit name=update value=update" . " </td>";
echo "<td>" . "<input type=submit name=delete value=delete" . " </td>";
echo "</tr>";
echo "</form>";
}
echo "<form action=employeeManager.php method=post>";
echo "<tr>";
echo "<td><input type=text name=uempNo></td>";
echo "<td><input type=text name=uname></td>";
echo "<td><input type=text name=udob></td>";
echo "<td><input type=text name=utel></td>";
echo "<td><input type=text name=uaddress></td>";
echo "<td><input type=text name=udepartment></td>";
echo "<td><input type=text name=upayrate></td>";
echo "<td><input type=text name=uskills></td>";
echo "<td><input type=text name=ugender></td>";
echo "<td>" . "<input type=submit name=add value=add" . " </td></tr>";
echo "</form>";
echo "</table>";
mysql_close($con);
?>
In the below quotes you were missing quotes and the tags were not closed proprely!
while($record = mysql_fetch_array($myData)){
echo "<form action='employeeManager.php' method='post'>";
echo "<tr>";
echo "<td><input type='hidden' name=hidden value='" . $record['EmpNo'] . "'> </td>";
echo "<td><input type='text' name='name' value='" . $record['Name'] . "'> </td>";
echo "<td><input type='text' name='dob' value='" . $record['DOB'] . "'> </td>";
echo "<td><input type='text' name='tel' value='" . $record['Tel'] . "'> </td>";
echo "<td><input type='text' name='address' value='" . $record['Address'] . "'> </td>";
echo "<td><input type='text' name='department' value='" . $record['Department'] . " </td>";
echo "<td><input type='text' name='payrate' value='" . $record['PayRate'] . "'> </td>";
echo "<td><input type='text' name='skills' value='" . $record['Skills'] . "'> </td>";
echo "<td><input type='text' name='gender' value='" . $record['Gender'] . "'> </td>";
echo "<td><input type='submit' name='update' value='update'> </td>";
echo "<td><input type='submit' name='delete' value='delete'> </td>";
echo "</tr>";
echo "</form>";
}
For better understanding of your mistake, notice your code:
echo "<td><input type=text></td>";
It should be like that:
echo "<td><input type='text'></td>";
It works, its just that you don't close the tag, which results in:
<input type=text name=address value=one two thre
The browser reads it as value=one and "two" and "three" like separate arguments.
What you need is a quote like this:
<input type=text name=address value="one two three"/>