0

I am creating an Invitation Card app for my upcoming event which will be held. My code successfully inserts the data into mysql database named booking having table name data. But there is problem with retrieving. When I fill the form and submit, it saves data in db but generates nothing. It gives following error:

Fatal error: Call to a member function query() on resource in C:\xampp\htdocs\booking\index.php on line 44

Here is my code, please tell me how to resolve this issue. I shall be very thankful to you.

<html>
<body>
    <?php 
        if(isset($_POST['add'])){
            $dbhost = 'localhost';
            $dbuser = 'root';
            $dbpass = '';
            $conn = mysql_connect($dbhost, $dbuser, $dbpass);

            if(! $conn){
                die('Could not connect: ' . mysql_error());
            }

            if(! get_magic_quotes_gpc()){
                $emp_name = addslashes($_POST['emp_name']);
                $emp_fname = addslashes($_POST['emp_fname']);   
                $emp_cnic = addslashes($_POST['emp_cnic']); 
                $emp_address = addslashes($_POST['emp_address']);

            } else {

                $emp_name = addslashes($_POST['emp_name']);
                $emp_fname = addslashes($_POST['emp_fname']);   
                $emp_cnic = addslashes($_POST['emp_cnic']); 
                $emp_address = addslashes($_POST['emp_address']);
            }


            $sql = "INSERT INTO data ". "(CNIC, Name, FatherName, PostalAddress) " . 

"VALUES('$emp_cnic', '$emp_name', '$emp_fname', '$emp_address')";

            mysql_select_db('booking');
            $retval = mysql_query($sql, $conn);

            if(! $retval) {die('Could not enter data: ' . mysql_error());}


?>

<table border=2>
    ~~~~~~Your Invitation Card~~~~~
    <tr><td>Your Name</td><td><?php
        $sql = "SELECT name FROM data";
        $result = $conn->query($sql);
        echo $result;
    ?></td></tr><br>

    <tr><td>Your Father Name</td><td>
        $sql = "SELECT fname FROM data";
        $result = $conn->query($sql);
        echo $result;
    ?></td></tr><br>

    <tr><td>Your CNIC Number</td><td>
        $sql = "SELECT cnic FROM data";
        $result = $conn->query($sql);
        echo $result;
    ?></td></tr><br>

    <tr><td>Your Postal Address</td><td>
        $sql = "SELECT address FROM data";
        $result = $conn->query($sql);
        echo $result;
    ?></td></tr><br>

    <tr><td>You are informed to approach Location XA-55 at 1800 Thursday with print of this 

Invitation card to paticipate in the function. </td></tr><br>
</table>



<?php

            mysql_close($conn);

        } else {

        ?>
        <form method = "post" action = "<?php $_PHP_SELF ?>">
            Name: <input type="text" name="emp_name" id="emp_name"><br>
            Father Name: <input type="text" name="emp_fname" id="emp_fname"><br>

            CNIC: <input type="text" name="emp_cnic" id="emp_cnic"><br>

            Address: <input type="text" name="emp_address" id="emp_address"><br>

            <input type="submit" name="add" id="add" value="Submit">
<?php
}

?>

</body></html>
0

3 Answers 3

1

Change

$result = $conn->query($sql);

To

$result = mysql_query($sql);

For more info click here

Sign up to request clarification or add additional context in comments.

Comments

0

I think you should be using mysql_query instead of $conn->query

Comments

0

I thin i spotted two errors in your code.

you should use

 mysql_query($sql,$conn);

instead of (that was mentioned before)

$result = $conn->query($sql);

You missed a couple of opening php tags in your html table.

Try following code and let me know if it works.

        if(! $conn){
            die('Could not connect: ' . mysql_error());
        }

        if(! get_magic_quotes_gpc()){
            $emp_name = addslashes($_POST['emp_name']);
            $emp_fname = addslashes($_POST['emp_fname']);   
            $emp_cnic = addslashes($_POST['emp_cnic']); 
            $emp_address = addslashes($_POST['emp_address']);

        } else {

            $emp_name = addslashes($_POST['emp_name']);
            $emp_fname = addslashes($_POST['emp_fname']);   
            $emp_cnic = addslashes($_POST['emp_cnic']); 
            $emp_address = addslashes($_POST['emp_address']);
        }


        $sql = "INSERT INTO data ". "(CNIC, Name, FatherName, PostalAddress)   " . 

        "VALUES('$emp_cnic', '$emp_name', '$emp_fname', '$emp_address')";

        mysql_select_db('booking');
        $retval = mysql_query($sql, $conn);

        if(! $retval) {die('Could not enter data: ' . mysql_error());}


        ?>

<table border=2>
 ~~~~~~Your Invitation Card~~~~~
<tr><td>Your Name</td><td><?php
    $sql = "SELECT name FROM data";
    $result = mysql_query($sql,$conn);
    echo $result;
?></td></tr><br>

<tr><td>Your Father Name</td><td>
  <?php
    $sql = "SELECT fname FROM data";
    $result = mysql_query($sql,$conn);
    echo $result;
?></td></tr><br>

<tr><td>Your CNIC Number</td><td>
  <?php
    $sql = "SELECT cnic FROM data";
    $result = mysql_query($sql,$conn);
    echo $result;
?></td></tr><br>

<tr><td>Your Postal Address</td><td>
  <?php
    $sql = "SELECT address FROM data";
    $result = mysql_query($sql,$conn);
    echo $result;
?></td></tr><br>

<tr><td>You are informed to approach Location XA-55 at 1800 Thursday with print of this 

  Invitation card to paticipate in the function. </td></tr><br>
</table>



<?php

        mysql_close($conn);

    } else {

    ?>
    <form method = "post" action = "<?php $_PHP_SELF ?>">
        Name: <input type="text" name="emp_name" id="emp_name"><br>
        Father Name: <input type="text" name="emp_fname" id="emp_fname"><br>

        CNIC: <input type="text" name="emp_cnic" id="emp_cnic"><br>

        Address: <input type="text" name="emp_address" id="emp_address"><br>

        <input type="submit" name="add" id="add" value="Submit">
        <?php
      }         

      ?>

    </body></html>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.