My problem is when i want to display someone's information from database using his Username:
i get this error this is Notice: Undefined variable: fname in /opt/lampp/htdocs/aa/hhh.php on line 27 . .
<?php
include 'con_to_db.php';
$result = mysqli_query($dbcon, "SELECT * FROM Members WHERE Username='youba'" );
while($row = mysqli_fetch_array($result))
{
$fname = $row['Username'];
$age = $row['Age'];
}
?>
<html>
<body>
<h1> this is <?php echo $fname?> </h1>
</body>
</html>
but when i use the id i get the information from database without any problems:
<?php
include 'con_to_db.php';
$result = mysqli_query($dbcon, "SELECT * FROM Members WHERE id='70'" );
while($row = mysqli_fetch_array($result))
{
$fname = $row['Username'];
$age = $row['Age'];
}
?>
<html>
<body>
<h1> this is <?php echo $fname?> </h1>
</body>
</html>
First of all, it's not an error but a notice. However, this particular notice does hint at an error in the code, though not a syntax error, but a logical one: the body of your while loop does not get executed - i.e. you forgot to check if you get any result in the first place. (Also, if you would get more than one result, only the last one would be displayed).
Please check your database to see why the username is not there, though you obviously expect it to be.
Also, I assume (hope!) that your id field is numeric in the database, in which case you should not write quotes:
$result = mysqli_query($dbcon, "SELECT * FROM Members WHERE id=70" );
Related
I have retrieve data from lvl2itquepaper and display it with textarea and save the user input to another table call lvl2itresult but when I press save, it save the last user input only.
For example, got 2 question with 2 text area, 1st text area are 'A' and 2nd text area are 'B', it save both user input as 'B'
<?php
include('../dbconnect.php');
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Online Examination System</title>
</head>
<body>
<div id="container">
<h1>Level 2 IT Question Paper</h1>
<h2>Please read the question carefully and answer it confidently. Good Luck All!</h2>
<?php
if(isset($_POST['Submit']))
{
$sql="SELECT * from lvl1itquepaper";
$run_que = mysqli_query($mysqli, $sql);
$check_que = mysqli_num_rows($run_que);
while ($row=$run_que->fetch_assoc())
{
$questionno = $row['questionno'];
$question = $row['question'];
$student_ans = $_POST['studentans'];
$sql="insert into lvl2itresult (questionno, question, studentans, username) values ('.$questionno.', '$question', '$student_ans', '".$_SESSION['login_user']."')";
$submit = $mysqli->query($sql);
}
}
?>
<form method= "post">
<?php
echo "Welcome, ";
$sql="SELECT * from lvl2itstudent WHERE username= '".$_SESSION['login_user']."'";
$find_student = mysqli_query($mysqli, $sql);
$check_student = mysqli_num_rows($find_student);
if ($check_student>0){
while($row = $find_student->fetch_assoc())
{
echo $row['username'];
}
}
echo "<br><br><br><br>";
$sql="SELECT * from lvl2itquepaper";
$run_que = mysqli_query($mysqli, $sql);
$check_que = mysqli_num_rows($run_que);
if($check_que>0){
while ($row=$run_que->fetch_assoc())
{
$questionno = $row['questionno'];
$question = $row['question'];
echo "".$questionno. "." .$question."<br>";
echo "<textarea name='studentans' rows='5' cols='50'></textarea><br><br>";
}
}
else {
echo "there is no data in database";
}
?>
<input type="submit" value = "Submit" name= "Submit" style= "width:60px; height:30px";>
</form>
</div>
</body>
This is pretty easy, but hard to explain in a few words. What's pretty much going on is that only one parameter is being sent in the POST data since no array has been set; furthermore PHP is not treating the information as arrays either, so there's only one value to be interpreted. In any case, if the data was sent correctly, PHP would identify the POST value as an array of 2 dimensions, outputting a SQL error for not being able to parse the data as a string.
To solve this, first change your <textarea> tag as follows:
<textarea name='studentans[]' rows='5' cols='50'></textarea>
The brackets will tell HTML that there will be many elements with the name studentans.
Now breaking into your insert logic, you need to treat the arrays getting the values using indexes. Usually these will start on 0, so we will work with that:
$i = 0; // $i stands for index.
while ($row=$run_que->fetch_assoc()){
$questionno = $row['questionno'];
$question = $row['question'];
$student_ans = $_POST['studentans'][$i]; // this is where the magic happens
$sql="insert into lvl2itresult (questionno, question, studentans, username) values ('$questionno', '$question', '$student_ans', '".$_SESSION['login_user']."')";
// please also notice I deleted 2 concatenating dots near "values ('$questionno',"
$submit = $mysqli->query($sql);
$i++; // increment by 1 so we can access the next value on the next loop
}
And that should do it. I hope I didn't forget any detail.
I want to make a login system, where the user can see all the messages he has received. In my database the insurance_company is the username and that is what I want it to show by. Here is my code. How do I fix it?
- Thanks Prem
<?php
$strSQL = "SELECT * FROM claims WHERE insurance_company = $_SESSION['user']";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {
echo $row['Claim'] . "<br />";
}
?>
PHP code can be written anywhere in HTML code. You need to just place your PHP code between tags. As you can see in the example bellow I have used php code between p tags.
<?php
$strSQL = "SELECT * FROM claims WHERE insurance_company = $_SESSION['user']";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {?>
<p> <?php echo $row['Claim'] . "<br />"; ?> <p>
<?php }
?>
If you want it to show insurance_company, you must echo this value in the response array:
echo $row['insurance_company']
I have got an error message in php like this "undefined index id in line no:", I have using the following code
$id='$_REQUEST[id]';
$sql = "SELECT * FROM country";
$result = $con->query($sql);
$i=1;
foreach($result as $row)
{
?>
<li><?php echo $row['name'] ?> </li>
<?php
$i++;
}
?>
For listing the following code i have used
<?php
if(isset($id)) {
$queryImg = "SELECT * FROM data WHERE country='$_REQUEST[id]'";
$resultImg = mysqli_query($con,$queryImg);
$rowResult = mysqli_num_rows($resultImg);
while($rowsImg = mysqli_fetch_array($resultImg)){ ?>
I do not have enough experience in php, so can you please check this code and tell me how to solve this.
remove single quotes like $id=$_REQUEST['id'];
and here $queryImg = "SELECT * FROM data WHERE country='".$_REQUEST['id']."'";
Just remove the quote from the variable $id, like this:
$id=$_REQUEST['id'];
I am trying to show it all with while loop on a dynamic page, but it wont show anything at all..
if (isset($_GET['id'])) {
$genre = $_GET['id'];
$sql = "SELECT * FROM movstream_genre WHERE ID = '$genre'";
$result = $db->query($sql);
$row = $result->fetch_assoc();
}else{
$sql = "SELECT ID, genre FROM movstream_genre";
$result = $db->query($sql);
}
<html>
<body>
<ul>
<?php while ( $row = $result->fetch_assoc() ): ?>
<li><?=$row['genre'];?></li>
<?php endwhile; ?>
</ul>
</body>
</html>
Anyone know why it wont show anything on the dynamic page, but if the page is not dynamic it works fine :)
Thanks.
If dynamic page means that your sending an id in Url an you are not getting result.
I think that the problem is that you are using $row = $result->fetch_assoc(); in if condition and also in while.
Please use fetching in one place. Better be in while loop.
Hope it helps.
I figured it out
if (isset($_GET['id'])) {
$genre = $_GET['id'];
$sql = "SELECT ID, genre FROM movstream_genre";
$result = $db->query($sql);
}
This it how it needs to look when its a dynamic page :)
well i think it is, it works now.
please echo the result ...
<li><?php echo $row['genre'];?></li>
And in href, it is only genre OR genre.php
In the first query..
$sql = "SELECT * FROM movstream_genre WHERE ID = '$genre'";
$result = $db->query($sql);
$row = $result->fetch_assoc();// this line not required
comment this line
$row = $result->fetch_assoc();
It looks like you haven't enclosed the first part of your PHP code in <?php ?> tags
I've scoured the web for a tutorial about this simple task, but to no avail. And so I turn to you helpful comrades. Here's what I need to do:
I have a MySQL database with an Events table. I need to create a PHP web page with a list of the Event titles, and each title must be a link to the full details of the Event. But I want to avoid having to create a static page for each event, primarily because I don't want the data entry volunteer to have to create these new pages. (Yes, I realize that static pages are more SEO friendly, but I need to forego that in this case for the sake of efficiency.)
I've seen PHP url syntax with something like this:
pagename.php?id=20
but I don't know how to make it work.
Any and all help greatly appreciated.
Thanks!
Kip
This is basic php. You would simply query the DB for the event details before the page headers are written and write the html accordingly.
The first thing I would ask you is if you know how to connect to your database. From there, you query based on the $_GET['id'] value and use the results to populate your html.
Not to be rude, but the question itself suggests you're new to PHP, right? So in order to provide a solution that works we might want to know just how far you got.
Also, you can rewrite your dynamic urls to appear like static ones using apache's mod_rewrite. It's probably a novice level thing if you're interested in "pretty" url's.
MODIFIED ANSWER:
In your loop you would use the id from the query result (assuming your primary key is id)...
while($field = mysql_fetch_array($result)) {
echo "<p class='date'>";
echo $field['month']." ".$field['day'].", ".$field['year'];
echo "</p>";
echo "<h3>";
echo ''.$field['event_name'].'';
echo "</h3>";
}
Then on somepage.php you would use the get var id to pull the relevant info...
$result = mysql_query("SELECT * FROM `calendar` WHERE `id` = '".mysql_real_escape_string($_GET['id'])."');
don't forget to look into mysql_real_escape_string() for cleaning entries.
It's wise to take extra care when you are using $_GETvariables, because them can be easily altered by a malicious user.
Following with the example, you could do:
$foo = (int)$_GET['id'];
So we are forcing here the cast of the variable to a integer so we are sure about the nature of the data, this is commonly used to avoid SQL injections.
lets say you have the php file test.php
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("db", $conn);
$id = $_GET['id'];
$sql = "select * from table where id = $id";
$result = mysql_query($sql, $conn);
if ($result){
$row = mysql_fetch_row($result);
$title = $row[0];
$content = $row[1];
}
?>
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<h1><?php echo $title ?></h1>
<p><?php echo $content ?></p>
</body>
</html>
a dynamic page would be something like that..
Here is the pertinent code for extracting a list of events in November from a table named calendar, with each event having a link to a page called event.php and with the event's id field appended to the end of the url:
$result = mysql_query("SELECT * FROM calendar WHERE sort_month='11'");
while($row = mysql_fetch_array($result))
{echo
"<a href='event.php?id=".$row['id']."'>".$row['event_name']."</a>"
;}
And here is the pertinent code on the event.php page. Note the row numbers in brackets depends on the placement of such in your table, remembering that the first row (field) would have the number 0 inside the brackets:
$id = $_GET['id'];
$sql = "select * from calendar where id = $id";
$result = mysql_query($sql, $con);
if ($result){
$row = mysql_fetch_row($result);
$title = $row[12];
$content = $row[7];
}
?>
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<h1><?php echo $title ?></h1>
<p><?php echo $content ?></p>
</body>
</html>
This works for me, thanks to the help from those above.
$foo=$_GET['id'];
in your example $foo would = 20