I'm really stuck on this question, I have given it my all and I still cant work out whats wrong, even my friends have attempted to help me, I'm beginning to think its unsolvable... Here's what is wants
Create a PHP script, named task3a.php, that retrieves the names and id of each driver, and outputs the
information differently to the previous two tasks. Instead of displaying a table, the output should contain
an HTML form. The form should contain a submit button and a drop-down list input. The drop-down
input should contain the driver names, and the form should submit via the GET method to task4.php
when the submit button is pressed. Name the select input driver.
And here's what I've got -
<!DOCTYPE HTMl>
<html>
<body>
<?php
try {
$dbhandle = new PDO('mysql:host=<...>.ac.uk;dbname=user','user','pass');
} catch (PDOExeption $e) {
die('Error Connecting to Database: ' . $e->getMessage());
}
$driver = 'SELECT forename, surname, d.nationality, name FROM Drivers d JOIN Teams t ON d.id = t.id';
$query = $dbhandle->prepare($driver);
if ($query->execute() === FALSE ) {
die('Error Running Query: ' . implode($query->errorInfo(), ' '));
}
$query->execute();
$result = $query->fetchAll();
?>
<table>
<tr>
<th>Forename</th>
<th>Surname</th>
<th>Nationality</th>
<th>Team</th>
</tr>
<?php foreach($result as $row) { ?>
<tr>
<td><?php echo $row['forename']; ?></td>
<td><?php echo $row['surname']; ?></td>
<td><?php echo $row['nationality']; ?><td>
<td><?php echo $row['name']; ?></td>
</tr>
<?php } ?>
</table>
<form action='task3a.php' method='GET'>
<select name=''driver>
<?php foreach($results as $row) { ?>
<option value='<?php echo $row['id'];?>' > <?php echo $row['name']; ?> </option>
<?php } ?>
</select>
</form>
</body>
</html>
It's giving me a table with everything in it at the moment and then under it a drop down box with nothing in it, I'm so confused
$result = $query->fetchAll();
^--- no S
<?php foreach($result as $row) { ?>
^---no S
<?php foreach($results as $row) { ?>
^----where did this S come from?
Consider changing this
<select name=''driver>
to
<select name='driver'>
Related
l have created an application using php,html and mysql. The application can store a user's information such as id, name, bio, and date created into the database and display in html table. The id is an auto increment value which increases with every data entered by the user. The insert part of the application works fine but when l try to delete a record nothing happens. An html form is part of the code which l have intentionally decided not to include. Here is a snapshot of my code:
$records = array();
if(!empty($_POST)) {
if(isset($_POST['firstName'],$_POST['lastName'], $_POST['bio'])){
$firstName = trim($_POST['firstName']);
$lastName = trim($_POST['lastName']);
$bio = trim($_POST['bio']);
if(!empty($firstName) && !empty($lastName) && !empty($bio)) {
$insert = $db->prepare("INSERT INTO people (firstName, lastName,
bio, created) VALUES (?, ?,?, NOW())");
$insert->bind_param('sss', $firstName, $lastName, $bio);
if($insert->execute()){
header('Location: addressbook.php');
die();
}
}
}
}
if($results = $db->query("SELECT * FROM people")){
if($results->num_rows){
while($row = $results->fetch_object()){
$records[] = $row;
}
$results->free();
}
}
?>
<!DOCTYPE html>
<html>
<head></head>
<body>
<div class = "container">
<?php
if(!count($records)){
echo 'No records found';
}
else{
?>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Bio</th>
<th>Created</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
foreach ($records as $r) {
?>
<tr>
<td><?php echo escape($r->id);?></td>
<td><?php echo escape($r->firstName); ?></td>
<td><?php echo escape($r->lastName); ?></td>
<td><?php echo escape($r->bio); ?></td>
<td><?php echo escape($r->created); ?></td>
<td>
<a onclick="return confirm('Do you want to delete the
record')" href="addressbook.php?idd=<?php echo $row['id'] ?>"
class="btn btn-
danger">Delete</a></td>
<?php
}
?>
</tr>
//My guess is the problem is with this code down here for deleting
<?php
if(isset($_POST['idd'])){
$idd = $_POST['idd'];
$results = $db->query("DELETE FROM people WHERE id=$idd");
if($results){
header('Location: addressbook.php');
}
}
?>
</tbody>
</table>
<?php
}
?>
you need to use $_GET because by default href tag sends the data with GET method.
your code should be
if(isset($_GET['idd'])){
$idd = $_GET['idd'];
$results = $db->query("DELETE FROM people WHERE id='$idd'");
if($results){
header('Location: addressbook.php');
}
}
NOTE- use prepared statement for avoiding sql injection attack
`
<?php
//database connectivity
$con=mysqli_connect("localhost","root","");
mysqli_select_db($con,"<db_name>");
$idd = $_REQUEST['idd'];
$sql= "DELETE FROM people WHERE id='$idd' ";
$result = mysqli_query($con,$sql) or die(mysql_error());
header("refresh:0.1; addressbook.php");
?>`
if(isset($_GET['idd'])){
$idd = $_GET['idd'];
$results = $db->query("DELETE FROM people WHERE id='{$idd}'");
Try adding a single quote.
If it still doesn't work, please see if the $_POST is actually posting correctly.
Try $results = $db->query("DELETE * FROM people WHERE id=$idd"); instead of $results = $db->query("DELETE FROM people WHERE id=$idd"); in the delete User Function :)
I want to get data from my sql database into a table with html.
I have writed 1st part of code ended with ?>
and started with html table and end with html
My code getting only Empty table without getting data from my Database, here the Code, i dont know how to get data with that
<td>
<?php echo $result['nazwiskoimie'] ?>
</td>
Here's the full code :
<?php
$con=mysqli_connect("localhost","root","","listap");
if (mysqli_connect_errno()) {
echo "Blad przy polaczeniu z baza: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM 'lista'");
?>
<html>
<table border='1'>
<tr>
</tr>
<tr>
<td> <?php echo $result['nazwiskoimie'] ?> </td>
//in this part have problem to get my data from database
</html>
Here's also a screenshot of a result: Final After a Save my file
Please try this:
$result = mysqli_query($con,"SELECT * FROM lista");;
?>
<html>
<table border='1'>
<?php
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row['nazwiskoimie']?></td>
</tr>
<?php
}
} ?>
</table>
</html>
Trying to list the data from mysql to a html table using php in main html file. I've been through all of the other questions on here and I'm sure I have mostly the same methods and code as them.
For some reason (which I suspect has something to do with mysql and not the code) the only result is a blank table with one row and five columns. Each time I try to implement the other codes they just seem to print text onto the site.
I'm very confused as I think I've done the right thing. I've even been able to list the data from mysql through php echo so I know it's there and that I can access it. Really would appreciate some help on this. Thank you.
<table border="1">
<tbody>
<?php
$connect = mysqli_connect("localhost", "daemon", "xampp", "test");
if (!$connect) {
die(mysqli_error());
}
$results = mysqli_query("SELECT title,url,details,file,submission_date FROM input");
while($row = mysqli_fetch_array($results)) {
?>
<td><?php echo $row['title']?></td>
<td><?php echo $row['url']?></td>
<td><?php echo $row['details']?></td>
<td><?php echo $row['file']?></td>
<td><?php echo $row['submission_date']?></td>
<?php
}
?>
</tbody>
</table>
You say this code is in your mail html file? And it is printing out onto the screen? Try changing your file to .php not .html! Php code won't run in a .html file, and will likely output your code directly onto the page.
Mysqli_query expect connection link as first parameter.
$results = mysqli_query($connect, "SELECT title,url,details,file,submission_date FROM input");
Just a quick not so related improvement. You can avoid inserting most of the php opening and closing clauses:
...
while($row = mysqli_fetch_array($results)){
echo "<td>".$row['title']."/td>";
echo "<td>".$row['url']"</td>";
...
}
?>
Use <tr> because table must have atleast one row(<tr>) and one column(<td>)
<table border="1">
<tbody>
<tr>
<?php
$connect = mysqli_connect("localhost", "daemon", "xampp", "test");
if (!$connect) {
die(mysqli_error());
}
$results = mysqli_query("SELECT title,url,details,file,submission_date FROM input");
while($row = mysqli_fetch_array($results)) {
?>
<td><?php echo $row['title']?></td>
<td><?php echo $row['url']?></td>
<td><?php echo $row['details']?></td>
<td><?php echo $row['file']?></td>
<td><?php echo $row['submission_date']?></td>
<?php
}
?>
</tr>
</tbody>
<table border="1">
<tbody>
<?php
$connect = mysqli_connect("localhost", "daemon", "xampp", "test");
if (!$connect) {
die(mysqli_error());
}
$results = mysqli_query($connect, "SELECT title,url,details,file,submission_date FROM input");
if (!$results) {
mysqli_error($results);
die();
}
while ($row = mysqli_fetch_array($results)) {
?>
<tr>
<td><?php echo $row['title'] ?></td>
<td><?php echo $row['url'] ?></td>
<td><?php echo $row['details'] ?></td>
<td><?php echo $row['file'] ?></td>
<td><?php echo $row['submission_date'] ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
I have a PHP form that shows data from a MySQL table. Each row obviously has different data, what I want to do is have a drop down list that displays data relevant to the data entry in each row.
for example, lets say I have two tables. Fruit and Fruit_Colors, as below:
so if my PHP Form was displayed as below, the MySQL data named fruits would display the data in the Fruit column. The color would then be fetched from the Fruit_Colors table depending on the PHP form output value in field 'Fruit'. so the drop down list for each row will be different.
my PHP form table syntax is:
<table id="hor-minimalist-a">
<tr>
<th>ID</th>
<th>Fruit</th>
<th>Color</th>
</tr>
<? while($row = $fruits->fetch(PDO::FETCH_ASSOC)) { ?>
<tr>
<td><? echo $row['id']; ?></td>
<td><? echo $row['fruit']; ?></td>
<td><SELECT NAME="fruitcolor" id="fruitcolor">
<OPTION VALUE=0 >
*// what goes here???*
</option>
</SELECT>
</td>
</tr>
<? } ?>
</table>
Any advice how I can complete this would be appreciated. remember this table could be up to 50 rows so need a dynamic way of passing the 'fruit' value to the drop down list.
Syntax I know for the drop down list populations is:
function fruitcolor_dropdown($db)
{
$result = $db->query("select color from Fruit_Color where Fruit=*'outputted value'*");
return $result;
}
$colors= fruitcolor_dropdown($db);
while($row = $colors->fetch(PDO::FETCH_ASSOC)) {
$color=$row["color"];
$optionsfruitcolors.="<OPTION VALUE=\"$color\">".$color;
}
Advice appreciated as always. Thanks and Regards.
It will be bad idea to connect and fire query two time when you can do it in single query-
you can see demo
$query = "select f.*,group_concat(color SEPARATOR '|') as fcolor from fruit F Left join fruit_color fc using (fruit) group by fc.fruit";
Above will be your query and you will loop it like below:
<? while($row = $fruits->fetch(PDO::FETCH_ASSOC)) { ?>
<tr>
<td><? echo $row['id']; ?></td>
<td><? echo $row['fruit']; ?></td>
<td><SELECT NAME="fruitcolor" id="fruitcolor">
<OPTION VALUE=0 >
<?php
$array = explode("|", $fcolor);
$count = count($array);
for($loop=0;$loop<$count;$loop) {
echo "<option>".$array[$loop]."</option>";
}
?>
</option>
</SELECT>
</td>
</tr>
<? } ?>
Check this out. We create a function to generate the dropdown options. It accepts the DB and the fruit as parameters --> loops through and makes the DOM --> outputs it to the browser.
PHP Function
function getColors($db, $fruit)
{
$result = $db->query(
sprintf("select color from Fruit_Color where Fruit = '%s'",
$fruit
)
);
$output = '';
while($row = $result->fetch(PDO::FETCH_ASSOC))
{
$output .= sprintf(
'<option value="%s">%s</option>',
$row['color'],
$row['color']
);
}
return $output;
}
Template
<? while($row = $fruits->fetch(PDO::FETCH_ASSOC)) { ?>
<tr>
<td><? echo $row['id']; ?></td>
<td><? echo $row['fruit']; ?></td>
<td><SELECT NAME="fruitcolor" id="fruitcolor">
<?php echo getColors($db, $row['fruit']); ?>
</SELECT>
</td>
</tr>
<? } ?>
use this in selectbox
function fruitcolor_dropdown($db)
{
$result = $db->query("select color from Fruit_Color where Fruit=*'outputted value'*");
return $result;
while($row = $colors->fetch(PDO::FETCH_ASSOC)) {
$color=$row["color"];
$optionsfruitcolors.="<OPTION VALUE=\"$color\">".$color;
}
}
<select name="fruitcolor" id="fruitcolor">
<?php $colors= fruitcolor_dropdown($db); ?>
</select>
I'm creating a form where users can view and edit their account information and I'd like to streamline this as much as possible since there are a lot of form fields. I've selected all the customer's data without specifying the column names, but I'm having trouble displaying in the correct place in an HTML table without having to call each one individually.
I've used mysql_fetch_array in the code below, but it would be quite cumbersome to do for 300+ fields. I know that mysql_fetch_assoc can be used, but I haven't figured out how to then print in the correct place.
mysql_fetch_assoc (Pardon my ignorance, but I couldn't get this to work without using WHILE and FOREACH)
while ($row = mysql_fetch_assoc($result)) {
foreach($row as $key=>$value){
echo "$key: $value<br />\n ";
}}
This works but requires specifying each field individually
<?php
//Database connection and Open database
require_once('config.php');
$result = mysql_query("SELECT customer_info.*, style.* FROM customer_info, style WHERE customer_info.user_id = style.user_id AND customer_info.user_id=$user_id ") or die(mysql_error());
$row = mysql_fetch_array($result );
?>
<table border="0" cellspacing="2" cellpadding="2">
<tr><td name="fname">First Name: <?php echo $row['fname']; ?></td>
<td name="lname">Last Name: <?php echo $row['lname']; ?></td>
<td name="email">Email: <?php echo $row['email']; ?></td>
<td name="colors_love">Colors Love: <?php echo $row['colors_love']; ?></td>
</tr></table>
Is there a way to essentially reverse the way I achieve this with my INSERT INTO code? if so, I'm assuming that will simplify my next task (allowing users to edit their information)
$fieldlist=$vallist='';
foreach ($_POST as $key => $value) {
$fieldlist.=$key.',';
$vallist.='\''.($value).'\',';
}
$result = mysql_query('INSERT INTO taste ('.$fieldlist.') VALUES ('.$vallist.')');
Leaving out the fact that using the mysql library is a terrible idea, you're actually most of the way there:
<table>
<?php while ($row = mysql_fetch_assoc($result)): ?>
<tr>
<?php foreach($row as $key=>$value) {
echo "<td>$key: $value</td>\n ";
} ?>
</tr>
<?php endwhile; ?>
</table>
You obviously would want to clean this up for a real form, since echoing db field names isn't exactly user-friendly.
kind of this?
<?php
//Database connection and Open database
require_once('config.php');
$result = mysql_query("SELECT customer_info.*, style.* FROM customer_info, style WHERE customer_info.user_id = style.user_id AND customer_info.user_id=$user_id ") or die(mysql_error());
$row = mysql_fetch_assoc($result );
?>
<table border="0" cellspacing="2" cellpadding="2">
<tr><?php foreach($row as $field => $content): ?>
<td><?php echo $field; ?>: <?php echo $content; ?></td>
<?php endforeach; ?></tr>
</table>