I am using the book "PHP and SQL for dummies 4th edition" to learn web programming. My PetCatalog interface is meant to retrieve and display all petTypes from my Database. It is retrieving the pets in the database but it is doing so without the radio buttons. I do not know where the error is from because everything seems to be in place in the code.
Note: Everything works perfectly except that the radio buttons symbol are not being displayed. PS: There need to be a fictional database for this program to make any sense to anyone trying to help because the database is in my system. Thanks.
<?php
/* Program: PetCatalog.php
* Desc: Displays a list of pet categories from the
* PetType table. Includes descriptions.
* Displays radio buttons for user to check.
*/
?>
<html>
<head><title>Pet Types</title></head>
<body>
<?php
$user="root";
$host="localhost";
$password="";
$database="PetCatalog";
$cxn = mysqli_connect($host,$user,$password,$database)
or die ("couldn't connect to server");
/* Select all categories from PetType table */
$query = "SELECT * FROM PetType ORDER BY petType";
$result = mysqli_query($cxn,$query)
or die ("Couldn't execute query.");
/* Display text before form */
echo "<div style='margin-left: .1in'>\n
<h1 style='text-align: center'>Pet Catalog</h1>\n
<h2 style='text-align: center'>The following animal
friends are waiting for you.</h2>\n
<p style='text-align: center'>Find just what you want
and hurry in to the store to pick up your
new friend.</p>
<h3>Which pet are you interested in?</h3>\n";
/* Create form containing selection list */
echo "<form action='ShowPets.php' method='POST'>\n";
echo "<table cellpadding='5' border='1'>";
$counter=1;
while($row = mysqli_fetch_assoc($result))
{
extract($row);
echo "<tr><td valign='top' width='15%'
style='font-weight: bold;
font-size:1.2em'\n";
echo "<input type='radio' name='interest' value='$petType'\n";
if( $counter == 1 )
{
echo "checked='checked'";
}
echo ">$petType</td>";
echo "<td>$typeDescription</td></tr>";
$counter++;
}
echo "</table>";
echo "<p><input type='submit' value='Select Pet Type'>
</form></p>\n";
?>
</div>
</body>
</html>
The first thing that jumps out at me is the fact that your line with
echo "<tr><td valign='top' width='15%'
style='font-weight: bold;
font-size:1.2em'\n";
does not close the <td> tag. This could interfere with the creation of your <input> tags.
Try
echo "<tr><td valign='top' width='15%'
style='font-weight: bold;
font-size:1.2em'>\n";
and see if that works.
Like justathoughtor2 said close the table data and table row </td></tr> i have tested your without closing the table data and table row
after closing the table data and table row
Related
I have this problem. I am displaying all the animals which is owned by a certain pet owner. Here is my code:
<?php
session_start();
$_SESSION['indicator'] = "";
$conn = mysqli_connect("localhost","root","","appnimal")
or die ("Could not connect to mysql because ".mysql_error());
$sql=mysqli_query($conn,"SELECT * FROM tbl_pet_animal WHERE pet_owner_id='$user'")
or die(mysql_error());
while($row=mysqli_fetch_array($sql))
{echo "<tr class='even pointer record'>
<td class=' '>",$row[1],"</td>
<td class=' '>",$row[2],"</td>
<td class=' '>",$row[3],"</td>
<td class=' '>",$row[4],"</td>
<td class=' '>",$row[5],"</td>
<td class=' '>",$row[6],"</td>
<td class=' '>",$row[7],"</td>
<td class=' '>",$row[8],"</td>
<td class=' '>",$row[9],"</td>
<td class=' '>",$row[10],"</td>
<td><button type='button'
onClick=location.href='try.php?".$_SESSION['indicator']."=$row[1]'>
</td>";
?>
What am I trying to do is let us say, echo the $_SESSION['indicator'] value in another page. This is my code there from another page where I want to echo the value:
<?php
session_start();
$indicator="";
$indicator= $_SESSION['indicator'];
$conn = mysqli_connect("localhost","root","","appnimal")
or die ("Could not connect to mysql because ".mysql_error());
$sql=mysqli_query($conn, "SELECT pet_animal_name FROM tbl_pet_animal WHERE pet_animal_name='$indicator'") or die(mysql_error());
while($row=mysqli_fetch_array($sql)){
echo ($row['pet_animal_name']);
}
?>
But when I go there, it doesn't display anything. Please help me.
It displays nothing because (at least) in this code, your $_SESSION['indicator'] is "" (empty string) and You probably dont have pat with name "".
You can replace this part in first script :
<?php
session_start();
$_SESSION['indicator'] = "";
$conn = mysqli_connect("localhost","root","","appnimal")
With
<?php
session_start();
$_SESSION['indicator'] = "ANY_NAME_FROM_DB"; // N.B.: strings can be case-sensitive.
$conn = mysqli_connect("localhost","root","","appnimal")
And then check if you get something.
UPDATE #1 (from comments)
If you want to assign value on line
onClick=location.href='try.php?".$_SESSION['indicator']."=$row[1]'
You should do this instead:
onClick=location.href='try.php?".$_SESSION['indicator']=$row[1]."'
Because in your example "=$row[1]" is part of string, not php code. It means that .'=whatever' will not have any impact on value of your $_SESSION['indicator'].
I have these table that list all the users of the site. I would like to add a button where in, once that is clicked, the other details of the user will show.
This is my code so far.
echo "<table border=1 align=center><tr class=style2><td>+<td>Student Name<td>Age<td>Address<td>School<td>Email";
$query = mysql_query("SELECT name, age, address, school, email FROM mst_user",$cn) or die(mysql_error());
while($row=mysql_fetch_row($query))
{
echo "<tr class=style8><td>+<td>$row[0]<td align=center> $row[1]<td align=center>$row[2]<td align=center>$row[3]<td align=center>$row[4]";
}
echo "</table>";
The code shows all the user of the site. The <td>+will be a button that when click will show the other details of the user. The other details will show or call a file which is result.php
result.php
$rs=mysql_query("select t.test_name,t.total_que,r.test_date,r.score from mst_test t, mst_result r where
t.test_id=r.test_id and r.login='$login'",$cn) or die(mysql_error());
echo "<h1 class=head1> Result </h1>";
if(mysql_num_rows($rs)<1)
{
echo "<br><br><h1 class=head1> You have not given any quiz</h1>";
exit;
}
echo "<table border=1 align=center><tr class=style2><td width=300>Test Name <td> Total<br> Question <td> Score";
while($row=mysql_fetch_row($rs))
{
echo "<tr class=style8><td>$row[0] <td align=center> $row[1] <td align=center> $row[3]";
}
echo "</table>";
my tables
Any suggestions on how can I achieve it?
I am new to PHP; please help me.
I am trying to align two PHP tables with images in them side by side but they are displaying one below the other. I want two tables side by side under one heading and two tables side by side under second heading. I've seen some solutions in HTML but I am looking for PHP. Please find screenshot of my error and my code below:
Please feel free to ask for any clarifications.
$prodcatSQL="select prodcatid, prodcatname, prodcatimage from prodcat"; // create an $sql variable and store the sql statement
$exeprodcatSQL=mysql_query($prodcatSQL) or die (mysql_error());
while ($arrayprod=mysql_fetch_array($exeprodcatSQL))
{
echo "<strong>Using display: inline-block; </strong><br>\n";
echo "<table border=1 class=\"inlineTable\">\n";
echo "<tr>\n";
echo "<td><p><a href=products.php?u_prodcatid=".$arrayprod['prodcatid'].">";
echo $arrayprod['prodcatname'];
echo "<p><img src=images/".$arrayprod['prodcatimage']."></p>";
echo "</a></p></td>\n";
echo "</tr>\n";
echo "</table>\n";
}
echo "<h3><center>".$subheading."</center></h3>";
$treatcatSQL="select treatcatid, treatcatname, treatcatimage from treatcat"; // create an $sql variable and store the sql statement
$exetreatcatSQL=mysql_query($treatcatSQL) or die (mysql_error());
while ($arrayprod=mysql_fetch_array($exetreatcatSQL))
{
echo "<strong>Using display: inline-block; </strong><br>\n";
echo "<table border=1 class=\"inlineTable\">\n";
echo "<tr>\n";
echo "<td><p><a href=treatmentpackages.php?u_treatcatid=".$arrayprod['treatcatid'].">";
echo $arrayprod['treatcatname'];
echo "<p><img src=images/".$arrayprod['treatcatimage']."></p>";
echo "</a></p></td>\n";
echo "</tr>\n";
echo "</table>\n";
}
You have nested <p> tags which may be introducing unnecessary new lines. Remove the tags and replace them with separate <td> elements and the alignment should be fine.
$prodcatSQL="select prodcatid, prodcatname, prodcatimage from prodcat"; // create an $sql variable and store the sql statement
$exeprodcatSQL=mysql_query($prodcatSQL) or die (mysql_error());
while ($arrayprod=mysql_fetch_array($exeprodcatSQL))
{
echo "<strong>Using display: inline-block; </strong><br>\n";
echo "<table border=1 class=\"inlineTable\">\n";
echo "<tr>\n";
echo "<td><a href=products.php?u_prodcatid=".$arrayprod['prodcatid'].">";
echo $arrayprod['prodcatname'];
echo "</a></td><td><a href=products.php?u_prodcatid=".$arrayprod['prodcatid']."><img src=images/".$arrayprod['prodcatimage'].">";
echo "</td></a>\n";
echo "</tr>\n";
echo "</table>\n";
}
$treatcatSQL="select treatcatid, treatcatname, treatcatimage from treatcat"; // create an $sql variable and store the sql statement
$exetreatcatSQL=mysql_query($treatcatSQL) or die (mysql_error());
while ($arrayprod=mysql_fetch_array($exetreatcatSQL))
{
echo "<strong>Using display: inline-block; </strong><br>\n";
echo "<table border=1 class=\"inlineTable\">\n";
echo "<tr>\n";
echo "<td><a href=treatmentpackages.php?u_treatcatid=".$arrayprod['treatcatid'].">";
echo $arrayprod['treatcatname'];
echo "</a></td><td><a href=treatmentpackages.php?u_treatcatid=".$arrayprod['treatcatid'].">";
echo "<img src=images/".$arrayprod['treatcatimage']."></a>";
echo "</td>\n";
echo "</tr>\n";
echo "</table>\n";
}
If this does not work, please edit the HTML output into the question.
You need to add this to your table, if you don't want external CSS
Replace this in each,
echo "<table border=1 class=\"inlineTable\">\n";
With this
echo "<table border=1 class=\"inlineTable\" style=\"width:50%;float:left;\">\n";
I have table which will be populated (from reading from mysql table). I should provide the user an option to select a row and delete it from the database. Till now I have populated the table (by reading from the mysql). But I dont know how to add a checkbox to each row. This is what I have till now.
<html>
<body>
<?php
$username="root";
$password="root";
$database="test";
mysql_connect(localhost,$username,$password);
#mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM table1";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
?>
<?php
$i=0;
echo "<table width='600' cellpadding='5' cellspacing='5' border='1'>";
while ($i < $num) {
$f1=mysql_result($result,$i,"sno");
$f2=mysql_result($result,$i,"lastname");
$f3=mysql_result($result,$i,"firstname");
?>
<font face="Arial, Helvetica, sans-serif"><?php echo "<tr><td> $f1 </td><td>$f2 </td>
<td> $f3 </td></tr>"; ?></font>
<?php
$i++;
}
echo "</table>";
?>
</body>
</html>
Can anyone please help me how to add a checkbox to each row.
Thanks
Just add the HTML code for rendering a checkbox in your php code e.g
<?php echo "<tr><td> $f1 </td><td>$f2 </td>
<td> $f3 </td><td></td><td><input type=\"checkbox\" name=\"checkbox\" value=\"\" id=\"checkbox\"></td></tr>"; ?>
Note the backslashes before the double quotes.
Sadly, i still cannot comment on answers. But i want to improve Max answered code.
I would use this instead :
<?php echo "<tr><td> $f1 </td><td>$f2 </td>
<td> $f3 </td><td></td><td><input type=\"checkbox\" name=\"checkbox[$f1]\" value=\"\" id=\"checkbox\"></td></tr>"; ?>
please note that i add '$f1' variable after 'checkbox' on name variable, so you can post checked row all at once. You can change '$f1' variable into some unique value that suit your needs. I think you'll need it since you want to add checkbox on your data rows. ;)
I am unable to set focus to my php page dc-test.php (this is a frames page: topFrame and botFrame) dropdown list, which has 2 dropdown lists and are dependent. These dropdown lists values are populated from a table. Unable to set focus for the first dropdown and later after the reload for the second dependent dropdown list. Pl see my code below, need your help ! Sorry if i am repeating this question on this forum, because I was unable to find the right answer.
Pl look at function firstfield(), it does't set the focus to dropdown list in the below code.
<html>
<head>
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='dc-test.php?cat=' + val ;
}
function firstfield()
{
document.topFrame.cat.focus();
}
</script>
</head>
<body onLoad="firstfield();">
<?php
$dbservertype='mysql';
$servername='localhost';
$dbusername='aaa';
$dbpassword='234!#';
$dbname='test';
connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
// End of DB connection
#$cat=$_GET['cat'];
if(strlen($cat) > 0 and !is_numeric($cat)){
echo "Data Error";
exit;
}
$quer2=mysql_query("SELECT DISTINCT Ac_desc, Ac_code FROM ACMAST order by Ac_desc");
/////// Second drop down list we will check if category is selected else we will display all the subcategory/////
if(isset($cat) and strlen($cat) > 0){
$quer=mysql_query("SELECT DISTINCT Add1 FROM ACMAST where Ac_code=$cat order by Add1");
}
echo "<form name='f1'>";
echo "<table width='100%' height='39' border='0' cellpadding='2' cellspacing='0'>";
echo " <tr>";
echo " <td width='12%'>.</td>";
echo " <td width='7%'><span class='sty1'> Customer</span></td>";
echo " <td width='27%'><select name='cat' onchange=\"reload(this.form); showUser(this.value);\">
<option value=''>Select Name</option>";
while($noticia2 = mysql_fetch_array($quer2)) {
if($noticia2['Ac_code']==#$cat){echo "<option selected value='$noticia2[Ac_code]'>$noticia2[Ac_desc]</option>"."<BR>";}
else{echo "<option value='$noticia2[Ac_code]'>$noticia2[Ac_desc]</option>";}
}
echo "</select>";
echo "</td>";
echo " <td width='6%'><span class='sty1'>Address</span></td>";
echo " <td width='48%'> </span><select name='subcat'>";
while($noticia = mysql_fetch_array($quer)) {
echo "<option value='$noticia[Add1]'>$noticia[Add1]</option>";
}
echo "</select>";
echo "</td>";
echo " </tr>";
echo "</table>";
?>
</body>
</html>
why don't you assign an id to your select box and then use
document.getElementById("cat").focus();
to access an element inside a frame use
window.frames[0].getElementById("cat").focus();
frames[0] just change the number 0 to the index of your frame. e.g. if you have 2 frames and you want to access the second one it would be frames[1]