I need to add two dropdown lists (from a Mysql query) to a table. I have the table that shows the results of r aquery and then I need to have the cells of the two columns with the dropdowns in them. This is going to be a form that will eventually create files.
Here is my code so far:
<?php
$link = mysql_connect("localhost", "", "") or die ('Error connecting to mysql' . mysql_error());
mysql_select_db("cqadmin");
$sql = "SELECT id , mac FROM phones order by mac;";
$result = mysql_query($sql) or die(mysql_error());
$sql1 = "SELECT id , templatename FROM templates order by templatename;";
$result1 = mysql_query($sql1) or die(mysql_error());
$sql2 = "SELECT extension, secret from extensions;";
$result2 = mysql_query($sql2) or die(mysql_error());
echo "<table border='3'>
<tr>
<th>Extension #</th>
<th>Secret</th>
<th>MAC Address</th>
<th>Template</th>
</tr>";
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['extension'] . "</td>";
echo "<td>" . $row['secret'] . "</td>";
echo "<td>" . $row[''] . "</td>";
echo "<td>" . $row[''] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
<p>
<select name="phone">
<?php
while($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['id'] . '">' . $row['mac'] . '</option>';
}
?>
</select>
<select name="template">
<?php
while($row = mysql_fetch_array($result1)) {
echo '<option value="' . $row['id'] . '">' . $row['templatename'] . '</option>';
}
?>
</select>
</p>
<?php
mysql_close($link);
?>
I have tried to insert the select into the row but the page doesn't load I get an server error.
Any help is much appreciated
Ok, try this:
<?php
error_reporting(E_ALL);
ini_set('display_errors','On');
$link = mysql_connect("localhost", "", "") or die ('Error connecting to mysql' . mysql_error());
mysql_select_db("cqadmin");
$sql2 = "SELECT extension, secret from extensions;";
$result2 = mysql_query($sql2) or die(mysql_error());
echo "<table border='3'>
<tr>
<th>Extension #</th>
<th>Secret</th>
<th>MAC Address</th>
<th>Template</th>
</tr>";
while($row = mysql_fetch_array($result2))
{
$sql = "SELECT id , mac FROM phones order by mac;";
$result = mysql_query($sql) or die(mysql_error());
$sql1 = "SELECT id , templatename FROM templates order by templatename;";
$result1 = mysql_query($sql1) or die(mysql_error());
echo "<tr>";
echo "<td>" . $row['extension'] . "</td>";
echo "<td>" . $row['secret'] . "</td>";
echo "<td> <select name='phone'>";
while($rowA = mysql_fetch_array($result)) {
echo '<option value="' . $rowA['id'] . '">' . $rowA['mac'] . '</option>';
}
echo "</select></td>";
echo "<td><select name='template'>";
while($rowB = mysql_fetch_array($result1)) {
echo '<option value="' . $rowB['id'] . '">' . $rowB['templatename'] . '</option>';
}
echo "</select></td>";
echo "</tr>";
}
echo "</table>";
?>
If you get any errors put them here. Keep in mind that the code above is preety bad, you shouldn't code like that. It's a good start to learn how things work, but later you should refactor it. Write it better, more readable, write your own functions getting data from db, use mysqli functions instead mysql or even library like PDO, separate your logic from the view etc.
Related
I would like to know how can I display my MySQL result in PHP using tables. I have done a lot of research in this question. However I didn't find a way to solve my question. Here's my code.
<html>
<head>
</head>
<body>
<?php
$con = mysql_connect("localhost","root","123456");
if (!$con) {
die("Cannot Connect: " . mysql_error());
}
mysql_select_db("androidlogin",$con);
$sql = "select id, speed, distance FROM result";
$data = mysql_query($sql,$con);
echo "<table border=1>
<tr>
<th>ID</th>
<th>Speed</th>
<th>Distance</th>
</tr>";
while($record = mysql_fetch_array($data)){
echo "<tr>";
echo "<td>" . $record['ID'] . "</td>";
echo "<td>" . $record['Speed'] . "</td>";
echo "<td>" . $record['Distance'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</body>
</html>
It seems that it does not having any problems or I'm just too bad in it.
The output of the web: http://tinypic.com/r/2hdnypd/9
By the way, this code will be change a bit so that it will becomes leaderboard. And I have another problem, how should I do if I want to see where my record ranked, while the code $sql = "select users.username, result.speed, result.distance FROM result, users where result.id = users.id order by result.speed desc limit 10";?
Your db columns are id,speed and distance but while fetching records and displaying you are using it as ID, Speed and Distance.
So please change this part:
while($record = mysql_fetch_array($data)){
echo "<tr>";
echo "<td>" . $record['id'] . "</td>";
echo "<td>" . $record['speed'] . "</td>";
echo "<td>" . $record['distance'] . "</td>";
echo "</tr>";
}
I want to delete full row from a form into a database in php when i click on delete button. But there are "Undefined index on line 39" issues come on my page and when i click on delete button it redirect me on different page and didn't delete a row.
how can i delete a row on one click ??
Please help me.
Thanks,
Nabeel
<body>
<a href="" >delete</a>
<a href="" >create</a>
<label>Read</label>
<?php
$con=mysqli_connect("localhost","root","","firstphp");
if($con)
{
}
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM users`");
echo "<table border='1'>
<tr>
<th>id</th>
<th>username</th>
<th>passward</th>
<th>name</th>
<th>delete</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['passward'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['<th><a href="<?php $_PHP_SELF ?>" >delete</a></th>'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
<label>End Read</label>
<br /><br /><hr />
<label>Delete</label>
<?php
if(isset($_POST['delete']))
{
$con=mysql_connect("localhost","root","");
if(!$con)
{
die("Could not connect: " . mysql_error());
}
$id = $_POST["id"];
$sql = "DELETE FROM users WHERE id = $id";
mysql_select_db('firstphp');
$result = mysql_query($sql,$con);
if(!$result)
{
die("Could not delete data: " . mysql_error());
}
echo "Deleted data successfully\n";
mysql_close($con);
}
else
{}
?></body>
change
echo "<td>" . $row['<th><a href="<?php $_PHP_SELF ?>" >delete</a></th>'] . "</td>";
in
echo '<th><a href="'.$_SERVER['PHP_SELF'].'?id='. $row['id'] .'&delete=true" >delete</a></td>';
and change
if(isset($_POST['delete']))
//...
$id = $_POST["id"];
in
if(isset($_GET['delete']))
//...
$id = $_GET["id"];
I'm trying to display members from my database using PHP, however the last name goes in with the first name and I'm not quite sure why... Could anyone point me in the right direction?
<?php
$mysql_db_hostname = "localhost";
$mysql_db_user = "alex";
$mysql_db_password="";
$mysql_db_database="gym";
$con = mysql_connect($mysql_db_hostname, $mysql_db_user, $mysql_db_password) or die("Could not connect database");
mysql_select_db($mysql_db_database, $con) or die("Could not select database");
$query = mysql_query("select * from users WHERE Category='Member'");
echo "<table border=1>
<tr>
<th>Users ID</th>
<th>First Name</th>
<th>Last Name</th>
</tr>";
while($row =mysql_fetch_assoc($query))
{
echo "<tr>";
echo "<td>" . $row['user_id']."<br>" . "</td>";
echo "<td>" . $row['First_Name']."<br>" . "</td";
echo "<td>" . $row['Last_Name']."<br>" . "</td";
echo "</tr>";
}
echo "</table";
?>
You aren't closing the tags. Syntax highlighter would show the error.
while($row =mysql_fetch_assoc($query))
{
echo "<tr>";
echo "<td>" . $row['user_id']."<br>" . "</td>";
echo "<td>" . $row['First_Name']."<br>" . "</td"; <-----------
echo "<td>" . $row['Last_Name']."<br>" . "</td"; <------------
echo "</tr>";
}
You are not closing your <td> tags properly
echo "<td>" . $row['First_Name']."<br>" . "</td>";
//^here
echo "<td>" . $row['Last_Name']."<br>" . "</td>";
//^and here
So the output is all one cell with both variables printed inside
You forgot to close the td on your first name field.
Just change
</td
to
</td>
I am accessing my database on PhpMyAdmin, all my names etc are correct, but for some reason UserId is not working. Can someone point me in the right direction?
I have tried printing it but nothing displays.
<?php session_start();
$username = $_GET['username'];
$password = $_GET['password'];
// Create connection
$con=mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM user2 where username='$username' and password='$password'");
$row_cnt = mysqli_num_rows($result);
if($row_cnt >0){
while($row = mysqli_fetch_array($result)){
$UserId = $row['UserId'];
}
$sqlQuery2 = "SELECT ProductID, Name, Price, Description FROM product";
echo "Hello ".$username."<br>" .$UserId. "<br> This is a list of products";
$result2 = mysqli_query($con,$sqlQuery2);
echo "<table border='1'>
<tr>
<th>ProductID</th>
<th>Name</th>
<th>Price</th>
<th>Description</th>
<th>View</th>
</tr>";
while($row = mysqli_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['ProductID'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td><a href=\"detailview.php?ProductID=".$row['ProductID']."\"'>Detailed View</a></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Update My Details
<?php } else{
echo "invalid login "; }
?>
use var_dump($var) to see what is in the variable
1st check what is returned in $result and after check $UserId before using it
after the while check if $UserId is set (if the condition is false, ou var is not set..) you should check $row_count too
you should indent your code
here is your code reindented :
<?php session_start();
$username = $_GET['username'];
$password = $_GET['password'];
// Create connection
$con=mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM user2 where username='$username' and password='$password'");
$row_cnt = mysqli_num_rows($result);
if($row_cnt >0){
while($row = mysqli_fetch_array($result)){
$UserId = $row['UserId'];
}
$sqlQuery2 = "SELECT ProductID, Name, Price, Description FROM product";
echo "Hello ".$username."<br>" .$UserId. "<br> This is a list of products";
$result2 = mysqli_query($con,$sqlQuery2);
echo "<table border='1'>
<tr>
<th>ProductID</th>
<th>Name</th>
<th>Price</th>
<th>Description</th>
<th>View</th>
</tr>";
while($row = mysqli_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['ProductID'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td><a href=\"detailview.php?ProductID=".$row['ProductID']."\"'>Detailed View</a></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Update My Details
<?php
}
else
{
echo "invalid login ";
}
?>
How can I insert a drop-down value into a DB? I have a form that selects a list of people. And with them i have their unique id showing. So populating isn't he problem, but inserting is... any ideas?
Code:
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'peter', 'abc123');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ajax_demo", $con);
$sql="SELECT * FROM user WHERE id = '".$q."'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
<option value="1">John Doe</option>
You get the 1 via post, I don't see where's the problem with inserting that into the database.
Update after adding code to the question:
Please show how you generate the options, this code seems ok. One thing though: you should escape $q before passing it to the query like this: mysql_real_escape_string($q). It returns a string, so you can just concatenate it to the query.
Read about SQL injection attacks.