Whole text won't show in a table, PHP - php

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>";

Related

My php code is not Updating or Adding to database

Basically I am not getting any errors when I press update or add. Can anyone help me out? I want it so when I press add it adds that data to the database, and when I press update it updates the database with that value.
<html>
<head>
<title>Subcontractors Data</title>
</head>
<body>
Logout
Homepage
<?php
//make connection
$con = mysqli_connect("localhost","root","");
if(!$con){
die("Can not connect " . mysqli_error());
}
//select db
mysqli_select_db($con , 'subcontractor');
$sql="SELECT * FROM subcontractors";
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE subcontractors SET ID='$_POST[ID]', Name='$_POST[Name]', Surname='$_POST[Surname]', FPA='$_POST[FPA]', Performance='$_POST[Performance]' WHERE ID='$_POST[hidden]'";
mysqli_query($con, $UpdateQuery);
};
if(isset($_POST['add'])){
$AddQuery = "INSERT INTO subcontractors (ID, Name, Surname, FPA, Performance) VALUES ('$_POST[aID]','$_POST[aName]','$_POST[aSurname]','$_POST[aFPA]','$_POST[aPerformance]')";
mysqli_query($con, $AddQuery);
};
$my_Data=mysqli_query($con,$sql);
echo "<table border=1>";
echo"<tr>";
echo"<th>ID</th>";
echo"<th>Name</th>";
echo"<th>Surname</th>";
echo"<th>FPA</th>";
echo "<th>Performance</th>";
echo "</tr>";
while($record=mysqli_fetch_assoc($my_Data)){
echo "<form action=editsub.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=text name='ID' value=".$record['ID'] ." </td>";
echo "<td>" . "<input type=text name='Name' value=".$record['Name'] . " </td>";
echo "<td>" . "<input type=text name='Surname' value=".$record['Surname'] . " </td>";
echo "<td>" . "<input type=text name='FPA' value=".$record['FPA'] . "% </td>";
echo "<td>" . "<input type=text name='Performance' value=".$record['Performance'] . "% </td>";
echo "<input type=hidden name='hidden' value=" . $record['ID'] . ">";
echo "<input type=submit name='update' value='update'>";
echo "</tr>";
echo "</form>";
}
echo "<form action=editsub.php mehtod=post>";
echo "<tr>";
echo "<td><input type=text name='aID'></td>";
echo "<td><input type=text name='aName'></td>";
echo "<td><input type=text name='aSurname'></td>";
echo "<td><input type=text name='aFPA'></td>";
echo "<td><input type=text name='aPerformance'></td>";
echo "<td>" . "<input type=submit name='add' value='add'" . " </td>";
echo "</form>";
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
This way you can check error.
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

How can I make my html code output the data in a valid way?

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 \

Create a drop down menu in a form being created with php

I have a form created with php to edit records in a MySQL db. I would like to add a drop down menu to this form for the "type" field but am not sure how to create it without losing the data already in the db field.
<?php
require_once '../php/dbconfig.php';
// Create connection
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (isset($_POST['update'])) {
$UpdateQuery = "UPDATE members SET name='$_POST[name]', type='$_POST[type]', physicaladdress='$_POST[physicaladdress]', billingaddress='$_POST[billingaddress]', contact='$_POST[contact]', phone='$_POST[phone]', fax='$_POST[fax]', email='$_POST[email]', web='$_POST[web]', description='$_POST[description]' WHERE id='$_POST[id]'";
mysqli_query($conn, $UpdateQuery);
}
$result = mysqli_query($conn, "SELECT * FROM members ORDER BY name");
echo "<br />";
echo "<table border=0>
<tr>
<th>Name</th>
<th>Type</th>
<th>Physical Address</th>
<th>Billing Address</th>
<th>Contact Name</th>
<th>Phone</th>
<th>Fax</th>
<th>Email</th>
<th>Web</th>
<th>Description</th>
</tr>" ;
while($record = mysqli_fetch_array($result)) {
echo "<form action = admin-update.php method = post>";
echo "<tr>";
echo "<td>" . "<input type=text size=42 name=name value='" . $record['name'] . "' </td>";
echo "<td>" . "<input type=text size=30 name=type value='" . $record['type'] . "' </td>";
echo "<td>" . "<input type=text size=60 name=physicaladdress value='" . $record['physicaladdress'] . "' </td>";
echo "<td>" . "<input type=text size=60 name=billingaddress value='" . $record['billingaddress'] . "' </td>";
echo "<td>" . "<input type=text size=20 name=contact value='" . $record['contact'] . "' </td>";
echo "<td>" . "<input type=text size=10 name=phone value='" . $record['phone'] . "' </td>";
echo "<td>" . "<input type=text size=10 name=fax value='" . $record['fax'] . "' </td>";
echo "<td>" . "<input type=text size=25 name=email value='" . $record['email'] . "' </td>";
echo "<td>" . "<input type=text size=25 name=web value='" . $record['web'] . "' </td>";
echo "<td>" . "<input type=text 50 name=description value='" . $record['description'] . "' </td>";
echo "<td>" . "<input type=hidden name=id value='" . $record['id'] . "' </td>";
echo "<td>" . "<input type=submit name= update value=Update" . " </td>";
echo "</form>";
}
echo "</table>";
$conn->close();
?>
Any assistance would be greatly appreciated. Thank you.
You should be able to change the type from an input to a select with options and include the value from the database as one of the options.
I suggest this:
$selectType = '<select name="type">';
$selectType .= '<option value="'. $record['type'] .'">"'. $record['type'] .'"</option>';
$selectType .= '<option value="Opt1">Option1</option>';
$selectType .= '<option value="Opt2">Option2</option>';
$selectType .= '<option value="Opt3">Option3</option>';
$selectType .= '</select>;
Then, inside your while loop:
echo "<td>" . $selectType . "</td>";
I hope this works for you. Please let me know if it does not or if it does not answer your question.

Update is not working in database

Dont mind the security issues, this is just local testing, but when ever i click the update button none of the changes go through on the page or on the query and i get no erros.
<?php
$link = mysqli_connect("localhost", "root", "", "test") or die("could not connect");
if (isset($_POST['update'])) {
$updateQuery = (" UPDATE `test1` SET f_name = '$_POST[f_name]', l_name='$_POST[l_name]', email='$_POST[email]' WHERE id='$_POST[id]'");
mysqli_query($link, $updateQuery);
};
$query = ("SELECT * FROM `test1`");
$result = mysqli_query($link, $query);
echo "<table border=1
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<form method=post action=update.php>";
echo "<tr>";
echo "<td>" . "<input type=text name=f_name value=" . $row['f_name'] . " </td>";
echo "<td>" . "<input type=text name=l_name value=" . $row['l_name'] . " </td>";
echo "<td>" . "<input type=text name=email value=" . $row['email'] . " </td>";
echo "<td>" . "<input type=hidden name=id value=" . $row['id'] . " </td>";
echo "<td>" . "<input type=submit name=submit value=update" . " </td>";
echo "</tr>";
}
?>
change your form to
while($row = mysqli_fetch_array($result)) {
echo "<form method=post action=update.php>";
echo "<input type=hidden name=update>";
echo "<tr>";
echo "<td>" . "<input type=text name=f_name value=" . $row['f_name'] . " </td>";
echo "<td>" . "<input type=text name=l_name value=" . $row['l_name'] . " </td>";
echo "<td>" . "<input type=text name=email value=" . $row['email'] . " </td>";
echo "<td>" . "<input type=hidden name=id value=" . $row['id'] . " </td>";
echo "<td>" . "<input type=submit name=submit value=update" . " </td>";
echo "</tr>";
}
POST keys should be in quotes. Try this instead:
$updateQuery = "UPDATE test1 SET f_name = ".$_POST['f_name'].", l_name=."$_POST['l_name'].", email=".$_POST['email']." WHERE id=".$_POST['id'];
Try this:
$updateQuery = ("UPDATE `test1` SET f_name = '{$_POST['f_name']}', l_name='{$_POST['l_name']}', email='{$_POST['email']}' WHERE id='{$_POST['id']}'");
Also you can try echoing something inside your if (isset($_POST['update'])) { to make sure it is testing true.
Here is your problem:
if (isset($_POST['submit']) && $_POST['submit'] == 'update') {
The name of the submit button is submit not update, the value is update.

How to make sure all content appear on the cell and not just the first word?

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"/>

Categories