-1

I am working on a appointment management website. I have designed a page for arranging an appointment (inserting in database) and its working fine. Now i want to view a existing appointment (view/fetch from database) and even modify if needed (updating). And i am facing an error in existing appointment.

here is the code for existing appointment:

<?php
mysql_connect("localhost", "root", "") or die("Could not connect.");

mysql_select_db("appointo") or die("could not find db.");

if(isset($_POST['uname'])){
$searchq = $_POST['uname'];

$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);

$query = mysql_query("SELECT * FROM appointments WHERE username LIKE '$searchq'") or die("could not search.");
$count = mysql_num_rows($query);
while($row = mysql_fetch_array($query)){
        $name = $row['name'];
        $doctorname = $row['doctorname'];
        $purpose = $row['purpose'];

        //$oname = '<div>'.$name.'</div>';
        //$odname = '<div>'.$doctorname.'</div>';
        //$opurpose = '<div>'.$purpose.'</div>';
    }

}
?>

<form method="POST">

<input type="text" name="uname" placeholder="Seach using Name.." />
<input type="search" name="search" value="Search" /><br><br>

Output: 
<?php

//$name=$_SESSION['name'];
//$doctorname=$_SESSION['doctorname'];
//$purpose=$_SESSION['purpose'];

echo "'Name:''.$name'";
echo "Doctor Name:".$doctorname;
echo "Purpose:".$purpose;
?>
</form>

Still not able to fetch data. I want that if someone enters a username (say, nichani) which i already have stored in my database. And clicks on search button, he should see below output as:

Name: Nishant Nichani
Doctor Name: Dr. A
Purpose: General Checkup

help me. .fetch it from database.

  • There is **no more support** for `mysql_*` functions, they are [**officially deprecated**](https://wiki.php.net/rfc/mysql_deprecation), **no longer maintained** and were [**removed in PHP 7**](http://php.net/manual/en/function.mysql-connect.php#warning). You should update your code with [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) to ensure the functionality of your project in the future. – j08691 Oct 12 '16 at 14:51
  • The issue is because your those variables were not defined in the scope where you are trying to use them, try to run your query against your DB and ensure you can fetch data out, then move the echo to inside the first tag – Babajide Apata Oct 12 '16 at 14:54
  • What if i store it in another variable and use it outside tags, for the output. – Nishant Nichani Oct 12 '16 at 15:00

2 Answers2

0
  1. You should add uname input feild to your form.

  2. You Should check if form is submitted or it's just first time page load.

  3. You should also check for empty result.

  4. And to do a LIKE search you should use % to specify any string with any length.

    mysql_select_db("appointo") or die("could not find db.");
    
    if(isset($_POST['uname'])){
    $searchq = $_POST['uname'];
    
    $searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
    
    $query = mysql_query("SELECT * FROM appointments WHERE username LIKE '%$searchq%'") or die("could not search.");
    $count = mysql_num_rows($query);
    while($row = mysql_fetch_array($query)){
            $name = $row['name'];
            $doctorname = $row['doctorname'];
            $purpose = $row['purpose'];
    
            //$oname = '<div>'.$name.'</div>';
            //$odname = '<div>'.$doctorname.'</div>';
            //$opurpose = '<div>'.$purpose.'</div>';
        }
    
    }
    ?>
    
    <form method="POST">
    Uname:
    <input name="uname" value="<?php echo @$_POST['uname']; ?>" placeholder="search name"/>
    
    Output: 
    <?php
    //$name=$_SESSION['name'];
    //$doctorname=$_SESSION['doctorname'];
    //$purpose=$_SESSION['purpose'];
    if(isset($_POST['uname'])){
     if(!empty(isset($query))){
       echo "'Name:''.$name'";
       echo "Doctor Name:".$doctorname;
       echo "Purpose:".$purpose;
    }else{
      echo "NOT FOUND";
    }
    
    }
    ?>
    </form>
    
MSS
  • 3,520
  • 24
  • 29
0

Define a blank varible before entering if statement, as $doctorname = ''; $purpose = ''; It should get your work done.